Skip to content

Commit

Permalink
perf: auto-estimate display range
Browse files Browse the repository at this point in the history
  • Loading branch information
bogovicj committed May 15, 2024
1 parent e5121ad commit 48367b1
Showing 1 changed file with 58 additions and 2 deletions.
60 changes: 58 additions & 2 deletions src/main/java/org/janelia/saalfeldlab/n5/ij/N5Importer.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
import ij.io.FileInfo;
import ij.plugin.PlugIn;
import ij.plugin.frame.Recorder;
import ij.process.ImageStatistics;
import net.imglib2.FinalInterval;
import net.imglib2.Interval;
import net.imglib2.RandomAccessibleInterval;
Expand Down Expand Up @@ -523,7 +524,7 @@ public static <T extends NumericType<T> & NativeType<T>, M extends N5DatasetMeta
if (datasetMetaArg != null && datasetMetaArg instanceof AxisMetadata) {

// this permutation will be applied to the image whose dimensions
// are padded to 5d with a canoni
// are padded to 5d with a canonical axis order
final int[] p = AxisUtils.findImagePlusPermutation((AxisMetadata)datasetMetaArg);

final Pair<RandomAccessibleInterval<T>, M> res = AxisUtils.permuteImageAndMetadataForImagePlus(p, imgC, datasetMetaArg);
Expand Down Expand Up @@ -857,8 +858,18 @@ public static List<ImagePlus> process(final N5Reader n5,

record(n5Url, asVirtual, cropInterval);
imgList.add(imp);
if (show)
if (show) {
// set the display min and max with a heuristic:
// set the min of the range to the min value and the max range to the 98th
// percentile
final ImageStatistics stats = ImageStatistics.getStatistics(imp.getProcessor());
final double[] hist = stats.histogram();
toCumulativeHistogram(hist);
final double min = stats.histMin;
final double max = min + (stats.binSize * nthPercentile(hist, 0.98));
imp.setDisplayRange(min, max);
imp.show();
}

} catch (final IOException e) {
IJ.error("failed to read n5");
Expand All @@ -869,6 +880,51 @@ public static List<ImagePlus> process(final N5Reader n5,
return imgList;
}

/**
* Turns a histogram into a cumulative histogram, in place and returns the total sum.
* <p>
* After running this method, the ith element of the array will contain the sum of the elements
* of the 0th through ith elements of the input array.
*
* @param histogram
* a histogram
* @return the total sum
*/
private static double toCumulativeHistogram(final double[] histogram) {

double total = 0;
for (int i = 0; i < histogram.length; i++) {
total += histogram[i];
histogram[i] = total;
}

return total;
}

/**
*
*
* @param cumulativeHistogram
* a cumulative histogram
* @param percentile
* a percentile in the range [0,1]
* @return the bin corresponding the the given percentile
*
*/
private static int nthPercentile(final double[] cumulativeHistogram, final double percentile) {

final int N = cumulativeHistogram.length - 1;
final double total = cumulativeHistogram[N];

for (int i = N; i >= 0; i--) {
if (cumulativeHistogram[i] <= percentile * total)
return i + 1;
}

return 0;
}


/*
* Convenience method to process using the current state of this object. Can
* not be used directly when this plugin shows the crop dialog.
Expand Down

0 comments on commit 48367b1

Please sign in to comment.