Search in sources :

Example 1 with EqualizeHistogram

use of ini.trakem2.imaging.filters.EqualizeHistogram in project TrakEM2 by trakem2.

the class ContrastEnhancerWrapper method apply.

public boolean apply(final Collection<Displayable> patches_) {
    if (null == patches_)
        return false;
    // Create appropriate patch list
    ArrayList<Patch> patches = new ArrayList<Patch>();
    for (final Displayable d : patches_) {
        if (d.getClass() == Patch.class)
            patches.add((Patch) d);
    }
    if (0 == patches.size())
        return false;
    // Check that all images are of the same size and type
    Patch firstp = (Patch) patches.get(0);
    final int ptype = firstp.getType();
    final double pw = firstp.getOWidth();
    final double ph = firstp.getOHeight();
    for (final Patch p : patches) {
        if (p.getType() != ptype) {
            // can't continue
            Utils.log("Can't homogenize histograms: images are not all of the same type.\nFirst offending image is: " + p);
            return false;
        }
        if (!equalize && 0 == stats_mode && p.getOWidth() != pw || p.getOHeight() != ph) {
            Utils.log("Can't homogenize histograms: images are not all of the same size.\nFirst offending image is: " + p);
            return false;
        }
    }
    try {
        if (equalize) {
            for (final Patch p : patches) {
                if (Thread.currentThread().isInterrupted())
                    return false;
                p.appendFilters(new IFilter[] { new EqualizeHistogram() });
                /*
					p.getProject().getLoader().releaseToFit(p.getOWidth(), p.getOHeight(), p.getType(), 3);
					ImageProcessor ip = p.getImageProcessor().duplicate(); // a throw-away copy
					if (this.from_existing_min_and_max) {
						ip.setMinAndMax(p.getMin(), p.getMax());
					}
					ce.equalize(ip);
					p.setMinAndMax(ip.getMin(), ip.getMax());
					*/
                // submit for regeneration
                p.getProject().getLoader().decacheImagePlus(p.getId());
                regenerateMipMaps(p);
            }
            return true;
        }
        // Else, call stretchHistogram with an appropriate stats object
        final ImageStatistics stats;
        if (1 == stats_mode) {
            // use each image independent stats
            stats = null;
        } else if (0 == stats_mode) {
            // use stack statistics
            final ArrayList<Patch> sub = new ArrayList<Patch>();
            if (use_full_stack) {
                sub.addAll(patches);
            } else {
                // build stack statistics, ordered by stdDev
                final SortedMap<Stats, Patch> sp = Collections.synchronizedSortedMap(new TreeMap<Stats, Patch>());
                Process.progressive(patches, new TaskFactory<Patch, Stats>() {

                    public Stats process(final Patch p) {
                        if (Thread.currentThread().isInterrupted())
                            return null;
                        ImagePlus imp = p.getImagePlus();
                        p.getProject().getLoader().releaseToFit(p.getOWidth(), p.getOHeight(), p.getType(), 2);
                        Stats s = new Stats(imp.getStatistics());
                        sp.put(s, p);
                        return s;
                    }
                });
                if (Thread.currentThread().isInterrupted())
                    return false;
                final ArrayList<Patch> a = new ArrayList<Patch>(sp.values());
                final int count = a.size();
                if (count < 3) {
                    sub.addAll(a);
                } else if (3 == count) {
                    // the middle one
                    sub.add(a.get(1));
                } else if (4 == count) {
                    sub.addAll(a.subList(1, 3));
                } else if (count > 4) {
                    int first = (int) (count / 4.0 + 0.5);
                    int last = (int) (count / 4.0 * 3 + 0.5);
                    sub.addAll(a.subList(first, last));
                }
            }
            stats = new StackStatistics(new PatchStack(sub.toArray(new Patch[sub.size()]), 1));
        } else {
            stats = reference_stats;
        }
        final Calibration cal = patches.get(0).getLayer().getParent().getCalibrationCopy();
        Process.progressive(patches, new TaskFactory<Patch, Object>() {

            public Object process(final Patch p) {
                if (Thread.currentThread().isInterrupted())
                    return null;
                p.getProject().getLoader().releaseToFit(p.getOWidth(), p.getOHeight(), p.getType(), 3);
                // a throw-away copy
                ImageProcessor ip = p.getImageProcessor().duplicate();
                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;
            }
        });
    } catch (Exception e) {
        IJError.print(e);
        return false;
    }
    return true;
}
Also used : Displayable(ini.trakem2.display.Displayable) ArrayList(java.util.ArrayList) StackStatistics(ij.process.StackStatistics) Calibration(ij.measure.Calibration) TreeMap(java.util.TreeMap) ImagePlus(ij.ImagePlus) ImageProcessor(ij.process.ImageProcessor) ImageStatistics(ij.process.ImageStatistics) SortedMap(java.util.SortedMap) TaskFactory(ini.trakem2.parallel.TaskFactory) EqualizeHistogram(ini.trakem2.imaging.filters.EqualizeHistogram) Patch(ini.trakem2.display.Patch)

Aggregations

ImagePlus (ij.ImagePlus)1 Calibration (ij.measure.Calibration)1 ImageProcessor (ij.process.ImageProcessor)1 ImageStatistics (ij.process.ImageStatistics)1 StackStatistics (ij.process.StackStatistics)1 Displayable (ini.trakem2.display.Displayable)1 Patch (ini.trakem2.display.Patch)1 EqualizeHistogram (ini.trakem2.imaging.filters.EqualizeHistogram)1 TaskFactory (ini.trakem2.parallel.TaskFactory)1 ArrayList (java.util.ArrayList)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1