void showResults() { int count = rt.getCounter(); // if (count==0) return; boolean lastSlice = !processStack || slice == imp.getStackSize(); if ((showChoice == OVERLAY_OUTLINES || showChoice == OVERLAY_MASKS) && slice == 1 && count > 0) imp.setOverlay(overlay); else if (outlines != null && lastSlice) { String title = imp != null ? imp.getTitle() : "Outlines"; String prefix; if (showChoice == MASKS) prefix = "Mask of "; else if (showChoice == ROI_MASKS) prefix = "Count Masks of "; else prefix = "Drawing of "; outlines.update(drawIP); outputImage = new ImagePlus(prefix + title, outlines); if (inSituShow) { if (imp.getStackSize() == 1) Undo.setup(Undo.TRANSFORM, imp); imp.setStack(null, outputImage.getStack()); } else if (!hideOutputImage) outputImage.show(); } if (showResults && !processStack) { TextPanel tp = IJ.getTextPanel(); if (beginningCount > 0 && tp != null && tp.getLineCount() != count) rt.show("Results"); Analyzer.firstParticle = beginningCount; Analyzer.lastParticle = Analyzer.getCounter() - 1; } else Analyzer.firstParticle = Analyzer.lastParticle = 0; }
void createMask(ImagePlus imp) { Roi roi = imp.getRoi(); boolean useInvertingLut = Prefs.useInvertingLut; Prefs.useInvertingLut = false; if (roi == null || !(roi.isArea() || roi.getType() == Roi.POINT)) { createMaskFromThreshold(imp); Prefs.useInvertingLut = useInvertingLut; return; } ImagePlus maskImp = null; Frame frame = WindowManager.getFrame("Mask"); if (frame != null && (frame instanceof ImageWindow)) maskImp = ((ImageWindow) frame).getImagePlus(); if (maskImp == null) { ImageProcessor ip = new ByteProcessor(imp.getWidth(), imp.getHeight()); if (!Prefs.blackBackground) ip.invertLut(); maskImp = new ImagePlus("Mask", ip); maskImp.show(); } ImageProcessor ip = maskImp.getProcessor(); ip.setRoi(roi); ip.setValue(255); ip.fill(ip.getMask()); maskImp.updateAndDraw(); Prefs.useInvertingLut = useInvertingLut; }
void scale(ImageProcessor ip) { if (newWindow) { Rectangle r = ip.getRoi(); ImagePlus imp2 = imp.createImagePlus(); imp2.setProcessor(title, ip.resize(newWidth, newHeight)); Calibration cal = imp2.getCalibration(); if (cal.scaled()) { cal.pixelWidth *= 1.0 / xscale; cal.pixelHeight *= 1.0 / yscale; } imp2.show(); imp.trimProcessor(); imp2.trimProcessor(); imp2.changes = true; } else { if (processStack && imp.getStackSize() > 1) { Undo.reset(); StackProcessor sp = new StackProcessor(imp.getStack(), ip); sp.scale(xscale, yscale, bgValue); } else { ip.snapshot(); Undo.setup(Undo.FILTER, imp); ip.setSnapshotCopyMode(true); ip.scale(xscale, yscale); ip.setSnapshotCopyMode(false); } imp.killRoi(); imp.updateAndDraw(); imp.changes = true; } }
/** Opens a stack of images. */ ImagePlus openStack(ColorModel cm, boolean show) { ImageStack stack = new ImageStack(fi.width, fi.height, cm); long skip = fi.getOffset(); Object pixels; try { ImageReader reader = new ImageReader(fi); InputStream is = createInputStream(fi); if (is == null) return null; IJ.resetEscape(); for (int i = 1; i <= fi.nImages; i++) { if (!silentMode) IJ.showStatus("Reading: " + i + "/" + fi.nImages); if (IJ.escapePressed()) { IJ.beep(); IJ.showProgress(1.0); silentMode = false; return null; } pixels = reader.readPixels(is, skip); if (pixels == null) break; stack.addSlice(null, pixels); skip = fi.gapBetweenImages; if (!silentMode) IJ.showProgress(i, fi.nImages); } is.close(); } catch (Exception e) { IJ.log("" + e); } catch (OutOfMemoryError e) { IJ.outOfMemory(fi.fileName); stack.trim(); } if (!silentMode) IJ.showProgress(1.0); if (stack.getSize() == 0) return null; if (fi.sliceLabels != null && fi.sliceLabels.length <= stack.getSize()) { for (int i = 0; i < fi.sliceLabels.length; i++) stack.setSliceLabel(fi.sliceLabels[i], i + 1); } ImagePlus imp = new ImagePlus(fi.fileName, stack); if (fi.info != null) imp.setProperty("Info", fi.info); if (show) imp.show(); imp.setFileInfo(fi); setCalibration(imp); ImageProcessor ip = imp.getProcessor(); if (ip.getMin() == ip.getMax()) // find stack min and max if first slice is blank setStackDisplayRange(imp); if (!silentMode) IJ.showProgress(1.0); silentMode = false; return imp; }
void createNewStack(ImagePlus imp, ImageProcessor ip) { int nSlices = imp.getStackSize(); int w = imp.getWidth(), h = imp.getHeight(); ImagePlus imp2 = imp.createImagePlus(); Rectangle r = ip.getRoi(); boolean crop = r.width != imp.getWidth() || r.height != imp.getHeight(); ImageStack stack1 = imp.getStack(); ImageStack stack2 = new ImageStack(newWidth, newHeight); ImageProcessor ip1, ip2; int method = interpolationMethod; if (w == 1 || h == 1) method = ImageProcessor.NONE; for (int i = 1; i <= nSlices; i++) { IJ.showStatus("Scale: " + i + "/" + nSlices); ip1 = stack1.getProcessor(i); String label = stack1.getSliceLabel(i); if (crop) { ip1.setRoi(r); ip1 = ip1.crop(); } ip1.setInterpolationMethod(method); ip2 = ip1.resize(newWidth, newHeight, averageWhenDownsizing); if (ip2 != null) stack2.addSlice(label, ip2); IJ.showProgress(i, nSlices); } imp2.setStack(title, stack2); Calibration cal = imp2.getCalibration(); if (cal.scaled()) { cal.pixelWidth *= 1.0 / xscale; cal.pixelHeight *= 1.0 / yscale; } IJ.showProgress(1.0); int[] dim = imp.getDimensions(); imp2.setDimensions(dim[2], dim[3], dim[4]); if (imp.isComposite()) { imp2 = new CompositeImage(imp2, ((CompositeImage) imp).getMode()); ((CompositeImage) imp2).copyLuts(imp); } if (imp.isHyperStack()) imp2.setOpenAsHyperStack(true); if (newDepth > 0 && newDepth != oldDepth) imp2 = (new Resizer()).zScale(imp2, newDepth, interpolationMethod); if (imp2 != null) { imp2.show(); imp2.changes = true; } }
void createNewStack(ImagePlus imp, ImageProcessor ip) { Rectangle r = ip.getRoi(); boolean crop = r.width != imp.getWidth() || r.height != imp.getHeight(); int nSlices = imp.getStackSize(); ImageStack stack1 = imp.getStack(); ImageStack stack2 = new ImageStack(newWidth, newHeight); ImageProcessor ip1, ip2; boolean interp = interpolate; if (imp.getWidth() == 1 || imp.getHeight() == 1) interp = false; for (int i = 1; i <= nSlices; i++) { IJ.showStatus("Scale: " + i + "/" + nSlices); ip1 = stack1.getProcessor(i); String label = stack1.getSliceLabel(i); if (crop) { ip1.setRoi(r); ip1 = ip1.crop(); } ip1.setInterpolate(interp); ip2 = ip1.resize(newWidth, newHeight); if (ip2 != null) stack2.addSlice(label, ip2); IJ.showProgress(i, nSlices); } ImagePlus imp2 = imp.createImagePlus(); imp2.setStack(title, stack2); Calibration cal = imp2.getCalibration(); if (cal.scaled()) { cal.pixelWidth *= 1.0 / xscale; cal.pixelHeight *= 1.0 / yscale; } int[] dim = imp.getDimensions(); imp2.setDimensions(dim[2], dim[3], dim[4]); IJ.showProgress(1.0); if (imp.isComposite()) { imp2 = new CompositeImage(imp2, 0); ((CompositeImage) imp2).copyLuts(imp); } if (imp.isHyperStack()) imp2.setOpenAsHyperStack(true); imp2.show(); imp2.changes = true; }
void createMask(ImagePlus imp) { Roi roi = imp.getRoi(); boolean useInvertingLut = Prefs.useInvertingLut; Prefs.useInvertingLut = false; boolean selectAll = roi != null && roi.getType() == Roi.RECTANGLE && roi.getBounds().width == imp.getWidth() && roi.getBounds().height == imp.getHeight() && imp.isThreshold(); if (roi == null || !(roi.isArea() || roi.getType() == Roi.POINT) || selectAll) { createMaskFromThreshold(imp); Prefs.useInvertingLut = useInvertingLut; return; } ImagePlus maskImp = null; Frame frame = WindowManager.getFrame("Mask"); if (frame != null && (frame instanceof ImageWindow)) maskImp = ((ImageWindow) frame).getImagePlus(); if (maskImp == null) { ImageProcessor ip = new ByteProcessor(imp.getWidth(), imp.getHeight()); if (!Prefs.blackBackground) ip.invertLut(); maskImp = new ImagePlus("Mask", ip); maskImp.show(); } ImageProcessor ip = maskImp.getProcessor(); ip.setRoi(roi); ip.setValue(255); ip.fill(ip.getMask()); Calibration cal = imp.getCalibration(); if (cal.scaled()) { Calibration cal2 = maskImp.getCalibration(); cal2.pixelWidth = cal.pixelWidth; cal2.pixelHeight = cal.pixelHeight; cal2.setUnit(cal.getUnit()); } maskImp.updateAndRepaintWindow(); Prefs.useInvertingLut = useInvertingLut; }
/** Opens the image. Displays it if 'show' is true. Returns an ImagePlus object if successful. */ public ImagePlus open(boolean show) { ImagePlus imp = null; Object pixels; ProgressBar pb = null; ImageProcessor ip; ColorModel cm = createColorModel(fi); if (fi.nImages > 1) { return openStack(cm, show); } switch (fi.fileType) { case FileInfo.GRAY8: case FileInfo.COLOR8: case FileInfo.BITMAP: pixels = readPixels(fi); if (pixels == null) return null; ip = new ByteProcessor(width, height, (byte[]) pixels, cm); imp = new ImagePlus(fi.fileName, ip); break; case FileInfo.GRAY16_SIGNED: case FileInfo.GRAY16_UNSIGNED: case FileInfo.GRAY12_UNSIGNED: pixels = readPixels(fi); if (pixels == null) return null; ip = new ShortProcessor(width, height, (short[]) pixels, cm); imp = new ImagePlus(fi.fileName, ip); break; case FileInfo.GRAY32_INT: case FileInfo.GRAY32_UNSIGNED: case FileInfo.GRAY32_FLOAT: case FileInfo.GRAY24_UNSIGNED: case FileInfo.GRAY64_FLOAT: pixels = readPixels(fi); if (pixels == null) return null; ip = new FloatProcessor(width, height, (float[]) pixels, cm); imp = new ImagePlus(fi.fileName, ip); break; case FileInfo.RGB: case FileInfo.BGR: case FileInfo.ARGB: case FileInfo.ABGR: case FileInfo.BARG: case FileInfo.RGB_PLANAR: pixels = readPixels(fi); if (pixels == null) return null; ip = new ColorProcessor(width, height, (int[]) pixels); imp = new ImagePlus(fi.fileName, ip); break; case FileInfo.RGB48: case FileInfo.RGB48_PLANAR: boolean planar = fi.fileType == FileInfo.RGB48_PLANAR; Object[] pixelArray = (Object[]) readPixels(fi); if (pixelArray == null) return null; ImageStack stack = new ImageStack(width, height); stack.addSlice("Red", pixelArray[0]); stack.addSlice("Green", pixelArray[1]); stack.addSlice("Blue", pixelArray[2]); imp = new ImagePlus(fi.fileName, stack); imp.setDimensions(3, 1, 1); if (planar) imp.getProcessor().resetMinAndMax(); imp.setFileInfo(fi); int mode = CompositeImage.COMPOSITE; if (fi.description != null) { if (fi.description.indexOf("mode=color") != -1) mode = CompositeImage.COLOR; else if (fi.description.indexOf("mode=gray") != -1) mode = CompositeImage.GRAYSCALE; } imp = new CompositeImage(imp, mode); if (!planar && fi.displayRanges == null) { for (int c = 1; c <= 3; c++) { imp.setPosition(c, 1, 1); imp.setDisplayRange(minValue, maxValue); } imp.setPosition(1, 1, 1); } break; } imp.setFileInfo(fi); setCalibration(imp); if (fi.info != null) imp.setProperty("Info", fi.info); if (fi.sliceLabels != null && fi.sliceLabels.length == 1 && fi.sliceLabels[0] != null) imp.setProperty("Label", fi.sliceLabels[0]); if (show) imp.show(); return imp; }
public void run(ImageProcessor ip) { String[] imageNames = getOpenImageNames(); if (imageNames[0] == "None") { IJ.error("need at least 2 binary open images"); return; } double previousMinOverlap = Prefs.get("BVTB.BinaryFeatureExtractor.minOverlap", 0); boolean previousCombine = Prefs.get("BVTB.BinaryFeatureExtractor.combine", false); GenericDialog gd = new GenericDialog("Binary Feature Extractor"); gd.addChoice("Objects image", imageNames, imageNames[0]); gd.addChoice("Selector image", imageNames, imageNames[1]); gd.addNumericField("Object_overlap in % (0=off)", previousMinOverlap, 0, 9, ""); gd.addCheckbox("Combine objects and selectors", previousCombine); gd.addCheckbox("Count output", true); gd.addCheckbox("Analysis tables", false); gd.showDialog(); if (gd.wasCanceled()) { return; } String objectsImgTitle = gd.getNextChoice(); String selectorsImgTitle = gd.getNextChoice(); double minOverlap = gd.getNextNumber(); boolean combineImages = gd.getNextBoolean(); boolean showCountOutput = gd.getNextBoolean(); boolean showAnalysis = gd.getNextBoolean(); if (gd.invalidNumber() || minOverlap < 0 || minOverlap > 100) { IJ.error("invalid number"); return; } Prefs.set("BVTB.BinaryFeatureExtractor.minOverlap", minOverlap); Prefs.set("BVTB.BinaryFeatureExtractor.combine", combineImages); if (objectsImgTitle.equals(selectorsImgTitle)) { IJ.error("images need to be different"); return; } ImagePlus objectsImp = WindowManager.getImage(objectsImgTitle); ImageProcessor objectsIP = objectsImp.getProcessor(); ImagePlus selectorsImp = WindowManager.getImage(selectorsImgTitle); ImageProcessor selectorsIP = selectorsImp.getProcessor(); if (!objectsIP.isBinary() || !selectorsIP.isBinary()) { IJ.error("works with 8-bit binary images only"); return; } if ((objectsImp.getWidth() != selectorsImp.getWidth()) || objectsImp.getHeight() != selectorsImp.getHeight()) { IJ.error("images need to be of the same size"); return; } // close any existing RoiManager before instantiating a new one for this analysis RoiManager oldRM = RoiManager.getInstance2(); if (oldRM != null) { oldRM.close(); } RoiManager objectsRM = new RoiManager(true); ResultsTable objectsRT = new ResultsTable(); ParticleAnalyzer analyzeObjects = new ParticleAnalyzer(analyzerOptions, measurementFlags, objectsRT, 0.0, 999999999.9); analyzeObjects.setRoiManager(objectsRM); analyzeObjects.analyze(objectsImp); objectsRM.runCommand("Show None"); int objectNumber = objectsRT.getCounter(); Roi[] objectRoi = objectsRM.getRoisAsArray(); ResultsTable measureSelectorsRT = new ResultsTable(); Analyzer overlapAnalyzer = new Analyzer(selectorsImp, measurementFlags, measureSelectorsRT); ImagePlus outputImp = IJ.createImage("output", "8-bit black", objectsImp.getWidth(), objectsImp.getHeight(), 1); ImageProcessor outputIP = outputImp.getProcessor(); double[] measuredOverlap = new double[objectNumber]; outputIP.setValue(255.0); for (int o = 0; o < objectNumber; o++) { selectorsImp.killRoi(); selectorsImp.setRoi(objectRoi[o]); overlapAnalyzer.measure(); measuredOverlap[o] = measureSelectorsRT.getValue("%Area", o); if (minOverlap != 0.0 && measuredOverlap[o] >= minOverlap) { outputIP.fill(objectRoi[o]); finalCount++; } else if (minOverlap == 0.0 && measuredOverlap[o] > 0.0) { outputIP.fill(objectRoi[o]); finalCount++; } } // measureSelectorsRT.show("Objects"); selectorsImp.killRoi(); RoiManager selectorRM = new RoiManager(true); ResultsTable selectorRT = new ResultsTable(); ParticleAnalyzer.setRoiManager(selectorRM); ParticleAnalyzer analyzeSelectors = new ParticleAnalyzer(analyzerOptions, measurementFlags, selectorRT, 0.0, 999999999.9); analyzeSelectors.analyze(selectorsImp); selectorRM.runCommand("Show None"); int selectorNumber = selectorRT.getCounter(); if (combineImages) { outputImp.updateAndDraw(); Roi[] selectorRoi = selectorRM.getRoisAsArray(); ResultsTable measureObjectsRT = new ResultsTable(); Analyzer selectorAnalyzer = new Analyzer(outputImp, measurementFlags, measureObjectsRT); double[] selectorOverlap = new double[selectorNumber]; outputIP.setValue(255.0); for (int s = 0; s < selectorNumber; s++) { outputImp.killRoi(); outputImp.setRoi(selectorRoi[s]); selectorAnalyzer.measure(); selectorOverlap[s] = measureObjectsRT.getValue("%Area", s); if (selectorOverlap[s] > 0.0d) { outputIP.fill(selectorRoi[s]); } } selectorRoi = null; selectorAnalyzer = null; measureObjectsRT = null; } // selectorRT.show("Selectors"); outputImp.killRoi(); String outputImageTitle = WindowManager.getUniqueName("Extracted_" + objectsImgTitle); outputImp.setTitle(outputImageTitle); outputImp.show(); outputImp.changes = true; if (showCountOutput) { String[] openTextWindows = WindowManager.getNonImageTitles(); boolean makeNewTable = true; for (int w = 0; w < openTextWindows.length; w++) { if (openTextWindows[w].equals("BFE_Results")) { makeNewTable = false; } } TextWindow existingCountTable = ResultsTable.getResultsWindow(); if (makeNewTable) { countTable = new ResultsTable(); countTable.setPrecision(0); countTable.setValue("Image", 0, outputImageTitle); countTable.setValue("Objects", 0, objectNumber); countTable.setValue("Selectors", 0, selectorNumber); countTable.setValue("Extracted", 0, finalCount); countTable.show("BFE_Results"); } else { IJ.renameResults("BFE_Results", "Results"); countTable = ResultsTable.getResultsTable(); countTable.setPrecision(0); countTable.incrementCounter(); countTable.addValue("Image", outputImageTitle); countTable.addValue("Objects", objectNumber); countTable.addValue("Selectors", selectorNumber); countTable.addValue("Extracted", finalCount); IJ.renameResults("Results", "BFE_Results"); countTable.show("BFE_Results"); } } if (showAnalysis) { ResultsTable extractedRT = new ResultsTable(); ParticleAnalyzer analyzeExtracted = new ParticleAnalyzer( ParticleAnalyzer.CLEAR_WORKSHEET | ParticleAnalyzer.RECORD_STARTS, measurementFlags, extractedRT, 0.0, 999999999.9); analyzeExtracted.analyze(outputImp); objectsRT.show("Objects"); selectorRT.show("Selectors"); extractedRT.show("Extracted"); } else { objectsRT = null; selectorRT = null; } objectsRM = null; measureSelectorsRT = null; analyzeObjects = null; overlapAnalyzer = null; objectRoi = null; selectorRM = null; objectsImp.killRoi(); objectsImp.changes = false; selectorsImp.changes = false; }