ImagePlus im = new ImagePlus(imagePathHere); ContrastEnhancer enh = new ContrastEnhancer(); enh.stretchHistogram(plus, i);
@Override public ImageProcessor process(ImageProcessor ip) { new ContrastEnhancer().equalize(ip); return ip; }
void normalize(ImageProcessor ip, double min, double max) { int min2 = 0; int max2 = 255; int range = 256; if (ip instanceof ShortProcessor) {max2 = 65535; range=65536;} else if (ip instanceof FloatProcessor) normalizeFloat(ip, min, max); int[] lut = new int[range]; for (int i=0; i<range; i++) { if (i<=min) lut[i] = 0; else if (i>=max) lut[i] = max2; else lut[i] = (int)(((double)(i-min)/(max-min))*max2); } applyTable(ip, lut); }
public void run(String arg) { ImagePlus imp = IJ.getImage(); stackSize = imp.getStackSize(); imp.trimProcessor(); if (!showDialog(imp)) return; Roi roi = imp.getRoi(); if (roi!=null) roi.endPaste(); if (stackSize==1) Undo.setup(Undo.TRANSFORM, imp); else Undo.reset(); if (equalize) equalize(imp); else stretchHistogram(imp, saturated); if (normalize) { ImageProcessor ip = imp.getProcessor(); ip.setMinAndMax(0,ip.getBitDepth()==32?1.0:ip.maxValue()); } imp.updateAndDraw(); }
ContrastEnhancer ce = new ContrastEnhancer(); ce.setNormalize(true); // Needs to be true for correct normalization ce.stretchHistogram(Meanimp, 0.0); ImageConverter ic = new ImageConverter(Meanimp); ic.convertToGray32(); ce.stretchHistogram(Orimp, 0.0); ic = new ImageConverter(Orimp); ic.convertToGray32(); ce.stretchHistogram(Varimp, 0.0); ic = new ImageConverter(Varimp); ic.convertToGray32();
public void stretchHistogram(ImagePlus imp, double saturated) { ImageStatistics stats = null; if (useStackHistogram) stats = new StackStatistics(imp); if (processStack) { ImageStack stack = imp.getStack(); for (int i=1; i<=stackSize; i++) { IJ.showProgress(i, stackSize); ImageProcessor ip = stack.getProcessor(i); ip.setRoi(imp.getRoi()); if (!useStackHistogram) stats = ImageStatistics.getStatistics(ip, MIN_MAX, null); stretchHistogram(ip, saturated, stats); } } else { ImageProcessor ip = imp.getProcessor(); ip.setRoi(imp.getRoi()); if (stats==null) stats = ImageStatistics.getStatistics(ip, MIN_MAX, null); if (imp.isComposite()) stretchCompositeImageHistogram((CompositeImage)imp, saturated, stats); else stretchHistogram(ip, saturated, stats); } }
public void stretchHistogram(ImageProcessor ip, double saturated) { useStackHistogram = false; stretchHistogram(new ImagePlus("", ip), saturated); }
/** Changes the tone curves of images. It should bring up the detail in the flat regions of your image. Histogram Equalization can enhance meaningless detail and hide important but small high-contrast features. This method uses a similar algorithm, but uses the square root of the histogram values, so its effects are less extreme. Hold the alt key down to use the standard histogram equalization algorithm. This code was contributed by Richard Kirk (rak@cre.canon.co.uk). */ public void equalize(ImageProcessor ip) { equalize(ip, ip.getHistogram()); }
public void stretchHistogram(ImageProcessor ip, double saturated, ImageStatistics stats) { int[] a = getMinAndMax(ip, saturated, stats); int hmin=a[0], hmax=a[1]; if (hmax>hmin) { double min = stats.histMin+hmin*stats.binSize; double max = stats.histMin+hmax*stats.binSize; if (stats.histogram16!=null && ip instanceof ShortProcessor) { min = hmin; max = hmax; } if (!updateSelectionOnly) ip.resetRoi(); if (normalize) normalize(ip, min, max); else { if (updateSelectionOnly) { ImageProcessor mask = ip.getMask(); if (mask!=null) ip.snapshot(); ip.setMinAndMax(min, max); if (mask!=null) ip.reset(mask); } else ip.setMinAndMax(min, max); } } }
private void equalize(ImageProcessor ip, int[] histogram) { ip.resetRoi(); if (ip instanceof ShortProcessor) { // Short max = 65535; range = 65535; } else { //bytes max = 255; range = 255; } double sum; sum = getWeightedValue(histogram, 0); for (int i=1; i<max; i++) sum += 2 * getWeightedValue(histogram, i); sum += getWeightedValue(histogram, max); double scale = range/sum; int[] lut = new int[range+1]; lut[0] = 0; sum = getWeightedValue(histogram, 0); for (int i=1; i<max; i++) { double delta = getWeightedValue(histogram, i); sum += delta; lut[i] = (int)Math.round(sum*scale); sum += delta; } lut[max] = max; applyTable(ip, lut); }
void stretchCompositeImageHistogram(CompositeImage imp, double saturated, ImageStatistics stats) { ImageProcessor ip = imp.getProcessor(); int[] a = getMinAndMax(ip, saturated, stats); int hmin=a[0], hmax=a[1]; if (hmax>hmin) {
public void run(String arg) { ImagePlus imp = IJ.getImage(); stackSize = imp.getStackSize(); imp.trimProcessor(); if (!showDialog(imp)) return; Roi roi = imp.getRoi(); if (roi!=null) roi.endPaste(); if (stackSize==1) Undo.setup(Undo.TRANSFORM, imp); else Undo.reset(); if (equalize) equalize(imp); else stretchHistogram(imp, saturated); if (normalize) { ImageProcessor ip = imp.getProcessor(); ip.setMinAndMax(0,ip.getBitDepth()==32?1.0:ip.maxValue()); } imp.updateAndDraw(); }
public void stretchHistogram(ImagePlus imp, double saturated) { ImageStatistics stats = null; if (useStackHistogram) stats = new StackStatistics(imp); if (processStack) { ImageStack stack = imp.getStack(); for (int i=1; i<=stackSize; i++) { IJ.showProgress(i, stackSize); ImageProcessor ip = stack.getProcessor(i); ip.setRoi(imp.getRoi()); if (!useStackHistogram) stats = ImageStatistics.getStatistics(ip, MIN_MAX, null); stretchHistogram(ip, saturated, stats); } } else { ImageProcessor ip = imp.getProcessor(); ip.setRoi(imp.getRoi()); if (stats==null) stats = ImageStatistics.getStatistics(ip, MIN_MAX, null); if (imp.isComposite()) stretchCompositeImageHistogram((CompositeImage)imp, saturated, stats); else stretchHistogram(ip, saturated, stats); } }
public void stretchHistogram(ImageProcessor ip, double saturated) { useStackHistogram = false; stretchHistogram(new ImagePlus("", ip), saturated); }
/** Changes the tone curves of images. It should bring up the detail in the flat regions of your image. Histogram Equalization can enhance meaningless detail and hide important but small high-contrast features. This method uses a similar algorithm, but uses the square root of the histogram values, so its effects are less extreme. Hold the alt key down to use the standard histogram equalization algorithm. This code was contributed by Richard Kirk (rak@cre.canon.co.uk). */ public void equalize(ImageProcessor ip) { equalize(ip, ip.getHistogram()); }
public void stretchHistogram(ImageProcessor ip, double saturated, ImageStatistics stats) { int[] a = getMinAndMax(ip, saturated, stats); int hmin=a[0], hmax=a[1]; if (hmax>hmin) { double min = stats.histMin+hmin*stats.binSize; double max = stats.histMin+hmax*stats.binSize; if (stats.histogram16!=null && ip instanceof ShortProcessor) { min = hmin; max = hmax; } if (!updateSelectionOnly) ip.resetRoi(); if (normalize) normalize(ip, min, max); else { if (updateSelectionOnly) { ImageProcessor mask = ip.getMask(); if (mask!=null) ip.snapshot(); ip.setMinAndMax(min, max); if (mask!=null) ip.reset(mask); } else ip.setMinAndMax(min, max); } } }
private void equalize(ImageProcessor ip, int[] histogram) { ip.resetRoi(); if (ip instanceof ShortProcessor) { // Short max = 65535; range = 65535; } else { //bytes max = 255; range = 255; } double sum; sum = getWeightedValue(histogram, 0); for (int i=1; i<max; i++) sum += 2 * getWeightedValue(histogram, i); sum += getWeightedValue(histogram, max); double scale = range/sum; int[] lut = new int[range+1]; lut[0] = 0; sum = getWeightedValue(histogram, 0); for (int i=1; i<max; i++) { double delta = getWeightedValue(histogram, i); sum += delta; lut[i] = (int)Math.round(sum*scale); sum += delta; } lut[max] = max; applyTable(ip, lut); }
void stretchCompositeImageHistogram(CompositeImage imp, double saturated, ImageStatistics stats) { ImageProcessor ip = imp.getProcessor(); int[] a = getMinAndMax(ip, saturated, stats); int hmin=a[0], hmax=a[1]; if (hmax>hmin) {
@Override public ImageProcessor process(ImageProcessor ip) { // Will not alter ip, no need to duplicate new ContrastEnhancer().stretchHistogram(ip, s, ImageStatistics.getStatistics(ip, Measurements.MIN_MAX, null)); return ip; }
public Object process(final Patch p) { if (Thread.currentThread().isInterrupted()) return null; p.getProject().getLoader().releaseToFit(p.getOWidth(), p.getOHeight(), p.getType(), 3); ImageProcessor ip = p.getImageProcessor().duplicate(); // a throw-away copy if (ContrastEnhancerWrapper.this.from_existing_min_and_max) { ip.setMinAndMax(p.getMin(), p.getMax()); } ImageStatistics st = stats; if (null == stats) { Utils.log2("Null stats, using image's self"); st = ImageStatistics.getStatistics(ip, Measurements.MIN_MAX, cal); } ce.stretchHistogram(ip, saturated, st); // This is all we care about from stretching the histogram: p.setMinAndMax(ip.getMin(), ip.getMax()); regenerateMipMaps(p); return null; } });