Search in sources :

Example 1 with FloodFiller

use of ij.process.FloodFiller in project bacmman by jeanollion.

the class FillHoles2D method fillHoles.

protected static void fillHoles(ImageProcessor ip, int foreground, int background) {
    int width = ip.getWidth();
    int height = ip.getHeight();
    FloodFiller ff = new FloodFiller(ip);
    // intermediate color
    ip.setColor(127);
    for (int y = 0; y < height; y++) {
        if (ip.getPixel(0, y) == background)
            ff.fill(0, y);
        if (ip.getPixel(width - 1, y) == background)
            ff.fill(width - 1, y);
    }
    for (int x = 0; x < width; x++) {
        if (ip.getPixel(x, 0) == background)
            ff.fill(x, 0);
        if (ip.getPixel(x, height - 1) == background)
            ff.fill(x, height - 1);
    }
    ImageInteger im = (ImageInteger) Image.createImageFrom2DPixelArray("", ip.getPixels(), width);
    int n = width * height;
    for (int xy = 0; xy < n; ++xy) {
        if (im.getPixelInt(xy, 0) == 127)
            im.setPixel(xy, 0, background);
        else
            im.setPixel(xy, 0, foreground);
    }
}
Also used : FloodFiller(ij.process.FloodFiller) ImageInteger(bacmman.image.ImageInteger)

Example 2 with FloodFiller

use of ij.process.FloodFiller in project BioVoxxel-Toolbox by biovoxxel.

the class Extended_Particle_Analyzer method borderCountCorrection.

// ------------------------------------------------------------------------------------------------------------------------
public ImageProcessor borderCountCorrection(ImageProcessor inputIP, String correctionPosition) {
    if (!correctionPosition.equals("None")) {
        Prefs.set("resizer.zero", false);
        ij.Prefs.blackBackground = true;
        IJ.setBackgroundColor(255, 255, 255);
        CanvasResizer resizeCanvas = new CanvasResizer();
        int xOff = 0;
        int yOff = 0;
        if (correctionPosition.equals("Top-Left")) {
            xOff = 0;
            yOff = 0;
        } else if (correctionPosition.equals("Top-Right")) {
            xOff = 1;
            yOff = 0;
        } else if (correctionPosition.equals("Bottom-Left")) {
            xOff = 0;
            yOff = 1;
        } else if (correctionPosition.equals("Bottom-Right")) {
            xOff = 1;
            yOff = 1;
        }
        ImageProcessor intermediateIP = resizeCanvas.expandImage(inputIP, (width + 1), (height + 1), xOff, yOff);
        FloodFiller bcFF = new FloodFiller(intermediateIP);
        intermediateIP.setValue(0);
        // intermediateIMP.show();	//test output
        if (correctionPosition.equals("Top-Left") || correctionPosition.equals("Top-Right") || correctionPosition.equals("Bottom-Left")) {
            bcFF.fill8(width, height);
        } else if (correctionPosition.equals("Bottom-Right")) {
            bcFF.fill8(0, 0);
        }
        ImageProcessor ip2 = resizeCanvas.expandImage(intermediateIP, (width), (height), (-xOff), (-yOff));
        return ip2;
    } else {
        ImageProcessor ip2 = inputIP.duplicate();
        return ip2;
    }
}
Also used : ImageProcessor(ij.process.ImageProcessor) FloodFiller(ij.process.FloodFiller) CanvasResizer(ij.plugin.CanvasResizer)

Example 3 with FloodFiller

use of ij.process.FloodFiller in project BioVoxxel-Toolbox by biovoxxel.

the class Extended_Particle_Analyzer method particleAnalysis.

public void particleAnalysis(ImageProcessor ip, ImagePlus imp2, String originalImageTitle) {
    if (!Area.equalsIgnoreCase("0-Infinity")) {
        AreaMin = Double.parseDouble(Area.substring(0, Area.indexOf("-")));
        String AreaInterMax = Area.substring(Area.indexOf("-") + 1);
        if (AreaInterMax.equalsIgnoreCase("Infinity")) {
            AreaMax = Double.POSITIVE_INFINITY;
        } else {
            AreaMax = Double.parseDouble(Area.substring(Area.indexOf("-") + 1));
        }
    }
    if (!usePixel) {
        AreaMin = AreaMin / squaredPixel;
        AreaMax = AreaMax / squaredPixel;
    }
    if (!Circularity.equals("0.00-1.00")) {
        CircularityMin = Double.parseDouble(Circularity.substring(0, Circularity.indexOf("-")));
        CircularityMax = Double.parseDouble(Circularity.substring(Circularity.indexOf("-") + 1));
    }
    // makes sure that renaming of results tables is not recorded
    Recorder rec = Recorder.getInstance();
    if (rec != null) {
        Recorder.record = false;
    }
    // read in existing results table
    TextWindow existingResultsTableWindow = ResultsTable.getResultsWindow();
    if (existingResultsTableWindow != null) {
        IJ.renameResults("oldResultsTable");
    }
    ResultsTable initialResultsTable = new ResultsTable();
    // define the new particle analyzer
    ParticleAnalyzer initialPA = new ParticleAnalyzer(currentPAOptions, measurementFlags, initialResultsTable, AreaMin, AreaMax, CircularityMin, CircularityMax);
    initialPA.setHideOutputImage(true);
    // perform the initial analysis of the image
    initialPA.analyze(imp2);
    int initialResultNumber = initialResultsTable.getCounter();
    // resultsTable.show("Results"); //keep for test output
    // IJ.renameResults("Results", "initial Results Table");
    X = new int[initialResultNumber];
    Y = new int[initialResultNumber];
    keptResults = new int[initialResultNumber];
    for (int coord = 0; coord < initialResultNumber; coord++) {
        X[coord] = (int) initialResultsTable.getValue("XStart", coord);
        Y[coord] = (int) initialResultsTable.getValue("YStart", coord);
    }
    ImagePlus tempImg = initialPA.getOutputImage();
    if (!Redirect.equals("None") && !usePixelForOutput) {
        tempImg.setCalibration(WindowManager.getImage(Redirect).getCalibration());
    } else if (Redirect.equals("None") && !usePixelForOutput) {
        tempImg.setCalibration(calibImg);
    } else if (usePixelForOutput) {
        Calibration newCal = new Calibration();
        newCal.setUnit("pixels");
        tempImg.setCalibration(null);
    }
    // tempImg.show();
    ImageProcessor tempIP = tempImg.getProcessor();
    if (tempIP.isInvertedLut()) {
        tempIP.invertLut();
    }
    if (!Redirect.equals("None")) {
        IJ.selectWindow(Redirect);
        initialResultsTable.reset();
        redirectRoiManager.runCommand("Measure");
        initialResultsTable = Analyzer.getResultsTable();
    // redirectedResultsTable.show("Results");
    // IJ.renameResults("ROIs");
    }
    // Calculate additional values not present in original results table from the normal particle analyzer
    double[] compactness = new double[initialResultNumber];
    double[] FAR = new double[initialResultNumber];
    double[] extent = new double[initialResultNumber];
    double[] cov = new double[initialResultNumber];
    double[] originalMeanValue = new double[initialResultNumber];
    double[] originalMedian = new double[initialResultNumber];
    double[] originalMode = new double[initialResultNumber];
    double[] originalStdDev = new double[initialResultNumber];
    double[] originalIntDen = new double[initialResultNumber];
    double[] originalRawIntDen = new double[initialResultNumber];
    double[] originalMin = new double[initialResultNumber];
    double[] originalMax = new double[initialResultNumber];
    double[] originalSkewness = new double[initialResultNumber];
    double[] originalKurtosis = new double[initialResultNumber];
    for (int calc = 0; calc < initialResultNumber; calc++) {
        originalMeanValue[calc] = initialResultsTable.getValue("Mean", calc);
        originalMedian[calc] = initialResultsTable.getValue("Median", calc);
        originalMode[calc] = initialResultsTable.getValue("Mode", calc);
        originalStdDev[calc] = initialResultsTable.getValue("StdDev", calc);
        originalIntDen[calc] = initialResultsTable.getValue("IntDen", calc);
        originalRawIntDen[calc] = initialResultsTable.getValue("RawIntDen", calc);
        originalMin[calc] = initialResultsTable.getValue("Min", calc);
        originalMax[calc] = initialResultsTable.getValue("Max", calc);
        originalSkewness[calc] = initialResultsTable.getValue("Skew", calc);
        originalKurtosis[calc] = initialResultsTable.getValue("Kurt", calc);
        FAR[calc] = ((initialResultsTable.getValue("Feret", calc)) / (initialResultsTable.getValue("MinFeret", calc)));
        compactness[calc] = (Math.sqrt((4 / Math.PI) * initialResultsTable.getValue("Area", calc)) / initialResultsTable.getValue("Major", calc));
        extent[calc] = (initialResultsTable.getValue("Area", calc) / ((initialResultsTable.getValue("Width", calc)) * (initialResultsTable.getValue("Height", calc))));
        cov[calc] = ((initialResultsTable.getValue("StdDev", calc)) / (initialResultsTable.getValue("Mean", calc)));
    }
    // elimination process of particles
    FloodFiller filledImage = new FloodFiller(tempIP);
    tempIP.setValue(0.0);
    Boolean continueProcessing;
    int KeptResultsCount = 0;
    for (int n = 0; n < initialResultNumber; n++) {
        continueProcessing = true;
        if (!Extent.equals("0.00-1.00") && continueProcessing) {
            double ExtentMin = Double.parseDouble(Extent.substring(0, Extent.indexOf("-")));
            double ExtentMax = Double.parseDouble(Extent.substring(Extent.indexOf("-") + 1));
            if (extent[n] < ExtentMin || extent[n] > ExtentMax) {
                filledImage.fill8(X[n], Y[n]);
                continueProcessing = true;
            // IJ.log("Extent");
            }
        }
        if ((!Perimeter.equalsIgnoreCase("0-infinity")) && continueProcessing) {
            double PerimeterMin = Double.parseDouble(Perimeter.substring(0, Perimeter.indexOf("-")));
            double PerimeterMax = Double.POSITIVE_INFINITY;
            String PerimeterInterMax = Perimeter.substring(Perimeter.indexOf("-") + 1);
            if (PerimeterInterMax.equalsIgnoreCase("infinity")) {
                PerimeterMax = Double.POSITIVE_INFINITY;
            } else {
                PerimeterMax = Double.parseDouble(Perimeter.substring(Perimeter.indexOf("-") + 1));
            }
            double currentPerimeter = initialResultsTable.getValue("Perim.", n);
            if (!usePixel) {
                currentPerimeter = currentPerimeter * pixelWidth;
            }
            if (currentPerimeter < PerimeterMin || currentPerimeter > PerimeterMax) {
                filledImage.fill8(X[n], Y[n]);
                continueProcessing = true;
            // IJ.log("Perimeter");
            }
        }
        if (!Roundness.equals("0.00-1.00") && continueProcessing) {
            double RoundnessMin = Double.parseDouble(Roundness.substring(0, Roundness.indexOf("-")));
            double RoundnessMax = Double.parseDouble(Roundness.substring(Roundness.indexOf("-") + 1));
            if (initialResultsTable.getValue("Round", n) < RoundnessMin || initialResultsTable.getValue("Round", n) > RoundnessMax) {
                filledImage.fill8(X[n], Y[n]);
                continueProcessing = true;
            // IJ.log("Roundness");
            }
        }
        if (!Solidity.equals("0.00-1.00") && continueProcessing) {
            double SolidityMin = Double.parseDouble(Solidity.substring(0, Solidity.indexOf("-")));
            double SolidityMax = Double.parseDouble(Solidity.substring(Solidity.indexOf("-") + 1));
            if (initialResultsTable.getValue("Solidity", n) < SolidityMin || initialResultsTable.getValue("Solidity", n) > SolidityMax) {
                filledImage.fill8(X[n], Y[n]);
                continueProcessing = true;
            // IJ.log("Solidity");
            }
        }
        if (!Compactness.equals("0.00-1.00") && continueProcessing) {
            double CompactnessMin = Double.parseDouble(Compactness.substring(0, Compactness.indexOf("-")));
            double CompactnessMax = Double.parseDouble(Compactness.substring(Compactness.indexOf("-") + 1));
            if (compactness[n] < CompactnessMin || compactness[n] > CompactnessMax) {
                filledImage.fill8(X[n], Y[n]);
                continueProcessing = true;
            // IJ.log("Compactness");
            }
        }
        if ((!AR.equalsIgnoreCase("0.00-infinity")) && continueProcessing) {
            double ARMin = Double.parseDouble(AR.substring(0, AR.indexOf("-")));
            double ARMax = 999999999;
            String ARInterMax = AR.substring(AR.indexOf("-") + 1);
            if (ARInterMax.equalsIgnoreCase("infinity")) {
                ARMax = 999999999;
            } else {
                ARMax = Double.parseDouble(AR.substring(AR.indexOf("-") + 1));
            }
            if (initialResultsTable.getValue("AR", n) < ARMin || initialResultsTable.getValue("AR", n) > ARMax) {
                filledImage.fill8(X[n], Y[n]);
                continueProcessing = true;
            // IJ.log("AR");
            }
        }
        if ((!FeretAR.equalsIgnoreCase("0.00-infinity")) && continueProcessing) {
            double FARMin = Double.parseDouble(FeretAR.substring(0, FeretAR.indexOf("-")));
            double FARMax = Double.POSITIVE_INFINITY;
            String FARInterMax = FeretAR.substring(FeretAR.indexOf("-") + 1);
            if (FARInterMax.equalsIgnoreCase("infinity")) {
                FARMax = Double.POSITIVE_INFINITY;
            } else {
                FARMax = Double.parseDouble(FeretAR.substring(FeretAR.indexOf("-") + 1));
            }
            if (FAR[n] < FARMin || FAR[n] > FARMax) {
                filledImage.fill8(X[n], Y[n]);
                continueProcessing = true;
            // IJ.log("FeretAR");
            }
        }
        if (!EllipsoidAngle.equals("0-180") && continueProcessing) {
            double EllipsoidAngleMin = Double.parseDouble(EllipsoidAngle.substring(0, EllipsoidAngle.indexOf("-")));
            double EllipsoidAngleMax = Double.parseDouble(EllipsoidAngle.substring(EllipsoidAngle.indexOf("-") + 1));
            if (initialResultsTable.getValue("Angle", n) < EllipsoidAngleMin || initialResultsTable.getValue("Angle", n) > EllipsoidAngleMax) {
                filledImage.fill8(X[n], Y[n]);
                continueProcessing = true;
            // IJ.log("EllipsoidAngle");
            }
        }
        if ((!MaxFeret.equalsIgnoreCase("0.00-infinity")) && continueProcessing) {
            double MaxFeretMin = Double.parseDouble(MaxFeret.substring(0, MaxFeret.indexOf("-")));
            double MaxFeretMax = Double.POSITIVE_INFINITY;
            String MaxFeretInterMax = MaxFeret.substring(MaxFeret.indexOf("-") + 1);
            if (MaxFeretInterMax.equalsIgnoreCase("infinity")) {
                MaxFeretMax = Double.POSITIVE_INFINITY;
            } else {
                MaxFeretMax = Double.parseDouble(MaxFeret.substring(MaxFeret.indexOf("-") + 1));
            }
            double currentMaxFeret = initialResultsTable.getValue("Feret", n);
            if (!usePixel) {
                currentMaxFeret = currentMaxFeret * pixelWidth;
            }
            if (currentMaxFeret < MaxFeretMin || currentMaxFeret > MaxFeretMax) {
                filledImage.fill8(X[n], Y[n]);
                continueProcessing = true;
            // IJ.log("MaxFeret");
            }
        }
        if ((!MinFeret.equalsIgnoreCase("0.00-infinity")) && continueProcessing) {
            double MinFeretMin = Double.parseDouble(MinFeret.substring(0, MinFeret.indexOf("-")));
            double MinFeretMax = Double.POSITIVE_INFINITY;
            String MinFeretInterMax = MinFeret.substring(MinFeret.indexOf("-") + 1);
            if (MinFeretInterMax.equalsIgnoreCase("infinity")) {
                MinFeretMax = Double.POSITIVE_INFINITY;
            } else {
                MinFeretMax = Double.parseDouble(MinFeret.substring(MinFeret.indexOf("-") + 1));
            }
            double currentMinFeret = initialResultsTable.getValue("MinFeret", n);
            if (!usePixel) {
                currentMinFeret = currentMinFeret * pixelWidth;
            }
            if (currentMinFeret < MinFeretMin || currentMinFeret > MinFeretMax) {
                filledImage.fill8(X[n], Y[n]);
                continueProcessing = true;
            // IJ.log("MinFeret");
            }
        }
        if (!FeretAngle.equals("0-180") && continueProcessing) {
            double FeretAngleMin = Double.parseDouble(FeretAngle.substring(0, FeretAngle.indexOf("-")));
            double FeretAngleMax = Double.parseDouble(FeretAngle.substring(FeretAngle.indexOf("-") + 1));
            if (initialResultsTable.getValue("FeretAngle", n) < FeretAngleMin || initialResultsTable.getValue("FeretAngle", n) > FeretAngleMax) {
                filledImage.fill8(X[n], Y[n]);
                continueProcessing = true;
            // IJ.log("FeretAngle");
            }
        }
        if (!COV.equals("0.00-1.00") && continueProcessing) {
            double COVMin = Double.parseDouble(COV.substring(0, COV.indexOf("-")));
            double COVMax = Double.parseDouble(COV.substring(COV.indexOf("-") + 1));
            if (cov[n] < COVMin || cov[n] > COVMax) {
                filledImage.fill8(X[n], Y[n]);
                continueProcessing = true;
            // IJ.log("COV");
            }
        }
        if (continueProcessing = true) {
            keptResults[KeptResultsCount] = n;
            KeptResultsCount++;
        }
    }
    initialResultsTable = null;
    if (existingResultsTableWindow != null) {
        IJ.renameResults("oldResultsTable", "Results");
        outputResultsTable = ResultsTable.getResultsTable();
    } else {
        outputResultsTable = new ResultsTable();
    }
    if (rec != null) {
        Recorder.record = true;
    }
    int existingResultsCounter = 0;
    ResultsTable resultsTable = new ResultsTable();
    int currentResultCount = 0;
    String newLabel = "";
    if (existingResultsTableWindow != null && !ClearResults) {
        ParticleAnalyzer outputPA = new ParticleAnalyzer(outputOptions, measurementFlags, resultsTable, AreaMin, AreaMax);
        outputPA.analyze(tempImg);
        outputImg = outputPA.getOutputImage();
        currentResultCount = resultsTable.getCounter();
        // int currentColumnCount = resultsTable.getLastColumn();
        String[] tableHeadings = resultsTable.getHeadings();
        existingResultsCounter = outputResultsTable.getCounter();
        for (int row = 0; row < currentResultCount; row++) {
            for (int column = 0; column < tableHeadings.length; column++) {
                if (column == 0) {
                    outputResultsTable.incrementCounter();
                    // outputResultsTable.addValue(tableHeadings[column], resultsTable.getStringValue(tableHeadings[column], row));
                    outputResultsTable.addLabel(originalImageTitle);
                } else {
                    try {
                        outputResultsTable.getStringValue(tableHeadings[column], row);
                    } catch (Exception e) {
                        outputResultsTable.setValue(tableHeadings[column], row, 0);
                    }
                    try {
                        outputResultsTable.setValue(tableHeadings[column], (row + existingResultsCounter), resultsTable.getValue(tableHeadings[column], row));
                    } catch (Exception e) {
                        // e.printStackTrace();
                        outputResultsTable.setValue(tableHeadings[column], (row + existingResultsCounter), 0);
                    }
                }
            }
        }
    } else if (existingResultsTableWindow == null || ClearResults) {
        ParticleAnalyzer outputPA = new ParticleAnalyzer(outputOptions, measurementFlags, outputResultsTable, AreaMin, AreaMax);
        outputPA.analyze(tempImg);
        outputImg = outputPA.getOutputImage();
        for (int l = 0; l < outputResultsTable.getCounter(); l++) {
            outputResultsTable.setLabel(originalImageTitle, l);
        }
    }
    if (Output.equals("Nothing")) {
        imp2.close();
    } else if (Output.equals("Overlay Outlines")) {
        IJ.selectWindow(outputImgID);
        IJ.run("Invert");
        IJ.run("Red");
        IJ.selectWindow(originalImgID);
        IJ.run("Add Image...", "image=[" + outputImgTitle + "] x=0 y=0 opacity=75 zero");
        outputImg.changes = false;
        outputImg.close();
    } else if (Output.equals("Overlay Masks")) {
        IJ.selectWindow(outputImgID);
        IJ.run("Cyan");
        IJ.selectWindow(originalImgID);
        IJ.run("Add Image...", "image=[" + outputImgTitle + "] x=0 y=0 opacity=75 zero");
        outputImg.changes = false;
        outputImg.close();
    } else if (!Output.equals("Overlay Outlines") && !Output.equals("Overlay Masks") && !Output.equals("Nothing")) {
        // ImageProcessor outputIP = outputImg.getProcessor();
        outputImgTitle = outputImg.getTitle();
        outputImgID = outputImg.getID();
        outputImg.updateAndDraw();
    }
    // potentially include convexity calculation here
    int finalResultNumber = outputResultsTable.getCounter();
    int keptResultsCounter = 0;
    for (int writeNew = existingResultsCounter; writeNew < finalResultNumber; writeNew++) {
        outputResultsTable.setValue("FeretAR", writeNew, FAR[keptResults[keptResultsCounter]]);
        outputResultsTable.setValue("Compact", writeNew, compactness[keptResults[keptResultsCounter]]);
        outputResultsTable.setValue("Extent", writeNew, extent[keptResults[keptResultsCounter]]);
        if (!Redirect.equals("None")) {
            outputResultsTable.setValue("COV", writeNew, cov[keptResults[keptResultsCounter]]);
            outputResultsTable.setValue("Mean", writeNew, originalMeanValue[keptResults[keptResultsCounter]]);
            outputResultsTable.setValue("Median", writeNew, originalMedian[keptResults[keptResultsCounter]]);
            outputResultsTable.setValue("Mode", writeNew, originalMode[keptResults[keptResultsCounter]]);
            outputResultsTable.setValue("StdDev", writeNew, originalStdDev[keptResults[keptResultsCounter]]);
            outputResultsTable.setValue("IntDen", writeNew, originalIntDen[keptResults[keptResultsCounter]]);
            outputResultsTable.setValue("RawIntDen", writeNew, originalRawIntDen[keptResults[keptResultsCounter]]);
            outputResultsTable.setValue("Min", writeNew, originalMin[keptResults[keptResultsCounter]]);
            outputResultsTable.setValue("Max", writeNew, originalMax[keptResults[keptResultsCounter]]);
            outputResultsTable.setValue("Skew", writeNew, originalSkewness[keptResults[keptResultsCounter]]);
            outputResultsTable.setValue("Kurt", writeNew, originalKurtosis[keptResults[keptResultsCounter]]);
        }
        keptResultsCounter++;
    }
    if (DisplayResults) {
        outputResultsTable.show("Results");
    }
    // Default value definition
    if (Reset) {
        resetDialogEntries();
    }
}
Also used : FloodFiller(ij.process.FloodFiller) ParticleAnalyzer(ij.plugin.filter.ParticleAnalyzer) Recorder(ij.plugin.frame.Recorder) Calibration(ij.measure.Calibration) ImagePlus(ij.ImagePlus) ImageProcessor(ij.process.ImageProcessor) TextWindow(ij.text.TextWindow) ResultsTable(ij.measure.ResultsTable)

Example 4 with FloodFiller

use of ij.process.FloodFiller in project BioVoxxel-Toolbox by biovoxxel.

the class Speckle_Inspector method run.

public void run(ImageProcessor ip) {
    String[] imageNames = getOpenImageNames();
    String[] roiManagerList = { "none", "primary", "secondary" };
    // legacy macro compatibility regarding object naming
    String macroParameters = Macro.getOptions();
    if (macroParameters != null && macroParameters.indexOf("big=") >= 0 && macroParameters.indexOf("small=") >= 0) {
        oldMacro = true;
        String modernMacroParameters = macroParameters.replace("big=", "primary=");
        modernMacroParameters = modernMacroParameters.replace("small=", "secondary=");
        modernMacroParameters = modernMacroParameters.replace("min_object=", "min_primary_size=");
        modernMacroParameters = modernMacroParameters.replace("max_object=", "max_primary_size=");
        modernMacroParameters = modernMacroParameters.replace("min_object_circularity=", "min_primary_circularity=");
        modernMacroParameters = modernMacroParameters.replace("max_object_circularity=", "max_primary_circularity=");
        modernMacroParameters = modernMacroParameters.replace("min_speckle_number=", "lower_secondary_count=");
        modernMacroParameters = modernMacroParameters.replace("max_speckle_number=", "upper_secondary_count=");
        modernMacroParameters = modernMacroParameters.replace("min_speckle_size=", "min_secondary_size=");
        modernMacroParameters = modernMacroParameters.replace("max_speckle_size=", "max_secondary_size=");
        modernMacroParameters = modernMacroParameters.replace(" roi ", " show=primary ");
        modernMacroParameters = modernMacroParameters.replace("individual_roi", "secondary_object");
        Macro.setOptions(modernMacroParameters);
    }
    GenericDialog gd = new GenericDialog("Speckle Inspector");
    gd.addChoice("Primary objects (binary)", imageNames, imageNames[0]);
    gd.addChoice("Secondary objects (binary)", imageNames, imageNames[0]);
    gd.addChoice("Redirect measurements to", imageNames, imageNames[0]);
    gd.addNumericField("min_primary_size: ", 0, 0, 9, "pixel");
    gd.addNumericField("max_primary_size: ", Double.POSITIVE_INFINITY, 0, 9, "pixel");
    gd.addNumericField("min_primary_circularity: ", 0.00, 2, 9, "");
    gd.addNumericField("max_primary_circularity: ", 1.00, 2, 9, "");
    gd.addNumericField("lower_secondary_count: ", 0, 0, 9, "");
    gd.addNumericField("upper_secondary_count: ", Double.POSITIVE_INFINITY, 0, 9, "");
    gd.addNumericField("min_secondary_size: ", 0, 0, 9, "pixel");
    gd.addNumericField("max_secondary_size: ", Double.POSITIVE_INFINITY, 0, 9, "pixel");
    gd.addNumericField("min_secondary_circularity", 0.00, 2, 9, "");
    gd.addNumericField("max_secondary_circularity", 1.00, 2, 9, "");
    gd.addChoice("show ROI Manager", roiManagerList, roiManagerList[0]);
    gd.addCheckbox("exclude objects on edges", true);
    // gd.addCheckbox("roi manager visible", false);
    gd.addCheckbox("speckle list", false);
    gd.addCheckbox("statistic log", false);
    gd.addCheckbox("secondary_object analysis", false);
    gd.addNumericField("font size (label)", 10, 0, 9, "");
    gd.addHelp("http://fiji.sc/BioVoxxel_Toolbox#Speckle_Inspector");
    gd.showDialog();
    gd.setSmartRecording(true);
    if (gd.wasCanceled()) {
        return;
    }
    primaryObjects = gd.getNextChoice();
    secondaryObjects = gd.getNextChoice();
    redirectToImage = gd.getNextChoice();
    minPrimarySize = gd.getNextNumber();
    maxPrimarySize = gd.getNextNumber();
    minPrimaryCirc = gd.getNextNumber();
    maxPrimaryCirc = gd.getNextNumber();
    minSecondaryNumber = gd.getNextNumber();
    maxSecondaryNumber = gd.getNextNumber();
    minSecondarySize = gd.getNextNumber();
    maxSecondarySize = gd.getNextNumber();
    minSecondaryCircularity = gd.getNextNumber();
    maxSecondaryCircularity = gd.getNextNumber();
    showRoiManager = gd.getNextChoice();
    excludeEdge = gd.getNextBoolean();
    if (excludeEdge == true) {
        primaryAnalyzerOptions |= ParticleAnalyzer.EXCLUDE_EDGE_PARTICLES;
    } else {
        primaryAnalyzerOptions &= ~ParticleAnalyzer.EXCLUDE_EDGE_PARTICLES;
    }
    showSpeckleList = gd.getNextBoolean();
    showStatisticsLog = gd.getNextBoolean();
    secondaryObjectAnalysis = gd.getNextBoolean();
    fontSize = gd.getNextNumber();
    if (gd.invalidNumber()) {
        IJ.error("invalid number entered");
        return;
    }
    if (primaryObjects.equals(secondaryObjects)) {
        IJ.error("images need to be different");
        return;
    }
    ImagePlus primaryObjectImp = WindowManager.getImage(primaryObjects);
    primaryObjectImp.killRoi();
    ImageProcessor bigObjectIP = primaryObjectImp.getProcessor();
    ImagePlus secondaryObjectImp = WindowManager.getImage(secondaryObjects);
    secondaryObjectImp.killRoi();
    ImageProcessor secondaryObjectIP = secondaryObjectImp.getProcessor();
    if (!bigObjectIP.isBinary() || !secondaryObjectIP.isBinary()) {
        IJ.error("works with 8-bit binary images only");
        return;
    }
    if ((primaryObjectImp.getWidth() != secondaryObjectImp.getWidth()) || primaryObjectImp.getHeight() != secondaryObjectImp.getHeight()) {
        IJ.error("images need to be of the same size");
        return;
    }
    ResultsTable primaryResultsTable = new ResultsTable();
    ParticleAnalyzer analyzeObjects = new ParticleAnalyzer(primaryAnalyzerOptions, measurementFlags, primaryResultsTable, minPrimarySize, maxPrimarySize, minPrimaryCirc, maxPrimaryCirc);
    analyzeObjects.analyze(primaryObjectImp);
    int objectNumber = primaryResultsTable.getCounter();
    // clear an existing RoiManager before instantiating a new one for this analysis
    RoiManager oldRM = RoiManager.getInstance2();
    if (oldRM != null) {
        oldRM.close();
    }
    if (showRoiManager.equals("none")) {
        primaryRoiManager = new RoiManager(true);
        secondaryRoiManager = new RoiManager(true);
    } else if (showRoiManager.equals("primary")) {
        primaryRoiManager = new RoiManager();
        secondaryRoiManager = new RoiManager(true);
    } else if (showRoiManager.equals("secondary")) {
        primaryRoiManager = new RoiManager(true);
        secondaryRoiManager = new RoiManager();
    }
    int[] x = new int[objectNumber];
    int[] y = new int[objectNumber];
    int[] cX = new int[objectNumber];
    int[] cY = new int[objectNumber];
    Calibration cal = primaryObjectImp.getCalibration();
    Recorder rec = Recorder.getInstance();
    if (rec != null) {
        Recorder.record = false;
    }
    for (int po = 0; po < objectNumber; po++) {
        x[po] = (int) primaryResultsTable.getValue("XStart", po);
        y[po] = (int) primaryResultsTable.getValue("YStart", po);
        cX[po] = (int) cal.getRawX(primaryResultsTable.getValue("X", po));
        cY[po] = (int) cal.getRawY(primaryResultsTable.getValue("Y", po));
        IJ.doWand(primaryObjectImp, x[po], y[po], 0.0, "8-connected");
        primaryObjectImp.getRoi().setName(Integer.toString(po + 1));
        primaryRoiManager.addRoi(primaryObjectImp.getRoi());
    }
    if (rec != null) {
        Recorder.record = true;
    }
    // analyze speckles
    // primaryResultsTable.reset();
    int[] secondaryCountPerPrimaryObject = new int[objectNumber];
    if (!redirectToImage.equalsIgnoreCase("None")) {
        Analyzer.setRedirectImage(WindowManager.getImage(redirectToImage));
    }
    ResultsTable secondaryResultsTable = new ResultsTable();
    int secondaryX;
    int secondaryY;
    for (int primaryObjectIndex = 0; primaryObjectIndex < objectNumber; primaryObjectIndex++) {
        ParticleAnalyzer analyzeSpeckles = new ParticleAnalyzer(secondaryAnalyzerOptions, measurementFlags, secondaryResultsTable, minSecondarySize, maxSecondarySize, minSecondaryCircularity, maxSecondaryCircularity);
        secondaryObjectImp.setRoi(primaryRoiManager.getRoi(primaryObjectIndex), false);
        analyzeSpeckles.analyze(secondaryObjectImp, secondaryObjectIP);
        secondaryCountPerPrimaryObject[primaryObjectIndex] = secondaryResultsTable.getCounter();
        secondaryObjectImp.killRoi();
        if (showRoiManager.equals("secondary")) {
            for (int so = 0; so < secondaryCountPerPrimaryObject[primaryObjectIndex]; so++) {
                secondaryX = (int) secondaryResultsTable.getValue("XStart", so);
                secondaryY = (int) secondaryResultsTable.getValue("YStart", so);
                // System.out.println(secondaryX + " / " + secondaryY);
                // secondaryCenterX[so] = (int) cal.getRawX(secondaryResultsTable.getValue("X", so));
                // secondaryCenterY[so] = (int) cal.getRawY(secondaryResultsTable.getValue("Y", so));
                IJ.doWand(secondaryObjectImp, secondaryX, secondaryY, 0.0, "8-connected");
                Roi secondaryObjectRoi = secondaryObjectImp.getRoi();
                secondaryObjectRoi.setName((primaryObjectIndex + 1) + "-" + (so + 1));
                secondaryRoiManager.addRoi(secondaryObjectRoi);
                secondaryObjectImp.killRoi();
            }
        }
        secondaryResultsTable.reset();
        analyzeSpeckles = null;
    }
    // create output image
    primaryObjectImp.killRoi();
    ImagePlus outputImp = primaryObjectImp.duplicate();
    outputImp.setTitle(WindowManager.getUniqueName("Inspector of " + primaryObjects));
    ImageConverter outputImgConverter = new ImageConverter(outputImp);
    outputImgConverter.convertToRGB();
    ImageProcessor outputIP = outputImp.getProcessor();
    Font font = new Font("Sans Serif", java.awt.Font.BOLD, (int) fontSize);
    outputIP.setFont(font);
    FloodFiller outputFloodFiller = new FloodFiller(outputIP);
    for (int c = 0; c < objectNumber; c++) {
        if (secondaryCountPerPrimaryObject[c] >= minSecondaryNumber && secondaryCountPerPrimaryObject[c] <= maxSecondaryNumber) {
            outputIP.setColor(Color.magenta);
            outputFloodFiller.fill8(x[c], y[c]);
            positive = positive + 1;
            PosPart = PosPart + secondaryCountPerPrimaryObject[c];
        } else if (secondaryCountPerPrimaryObject[c] < minSecondaryNumber) {
            outputIP.setColor(Color.blue);
            outputFloodFiller.fill8(x[c], y[c]);
            less = less + 1;
            NegLess = NegLess + secondaryCountPerPrimaryObject[c];
        } else if (secondaryCountPerPrimaryObject[c] > maxSecondaryNumber) {
            Color drawColor = new Color(0, 93, 0);
            outputIP.setColor(drawColor);
            outputFloodFiller.fill8(x[c], y[c]);
            more = more + 1;
            NegMore = NegMore + secondaryCountPerPrimaryObject[c];
        }
        outputIP.setColor(Color.white);
        outputIP.drawString("" + (c + 1) + "-(" + secondaryCountPerPrimaryObject[c] + ")", cX[c] - ((int) (fontSize * 1.5)), cY[c] + ((int) (fontSize / 2)));
    }
    outputFloodFiller = null;
    outputImp.show();
    if (showRoiManager.equals("primary")) {
        primaryRoiManager.setVisible(true);
    } else if (showRoiManager.equals("secondary")) {
        secondaryRoiManager.setVisible(true);
    }
    // write speckle counts in speckle list (a results table)
    if (showSpeckleList == true) {
        ResultsTable speckleList = new ResultsTable();
        speckleList.setPrecision(0);
        speckleList.setValue("Object", 0, 1);
        speckleList.setValue("Speckles", 0, secondaryCountPerPrimaryObject[0]);
        // TODO
        speckleList.incrementCounter();
        for (int r = 1; r < objectNumber; r++) {
            speckleList.addValue("Object", r + 1);
            speckleList.setValue("Speckles", r, secondaryCountPerPrimaryObject[r]);
            if (r < objectNumber - 1) {
                speckleList.incrementCounter();
            }
        }
        speckleList.showRowNumbers(false);
        speckleList.show("Speckle List " + primaryObjects);
    }
    // calculate statistics and IJ.log to log window
    if (objectNumber == 0) {
        IJ.log("No Objects detected in " + primaryObjectImp.getTitle());
    } else if ((PosPart + NegLess + NegMore) == 0) {
        IJ.log("No Speckles detected in " + secondaryObjectImp.getTitle());
    } else if (showStatisticsLog) {
        IJ.log("Object size limit min: " + minPrimarySize + " / max: " + maxPrimarySize);
        IJ.log("Circularity limit min: " + minPrimaryCirc + " / max: " + maxPrimaryCirc);
        IJ.log("Speckle no. limit min: " + minSecondaryNumber + " / max: " + maxSecondaryNumber);
        IJ.log("Speckle size limit min: " + minSecondarySize + " / max: " + maxSecondarySize);
        IJ.log("----------------------------------------------------");
        IJ.log("White features are excluded from the analysis");
        IJ.log("All Objects: " + objectNumber);
        IJ.log("All Speckles: " + (PosPart + NegLess + NegMore));
        IJ.log("Aver. speckle no/feature (all): " + ((PosPart + NegLess + NegMore) / objectNumber));
        IJ.log("----------------------------------------------------");
        IJ.log("Selected Features (magenta): " + positive);
        IJ.log("   All speckles in pos. features: " + PosPart);
        if (positive != 0) {
            IJ.log("   Aver. speckle no/pos feature: " + (PosPart / positive));
        }
        IJ.log("");
        IJ.log("Features with speckle no. smaller min (blue): " + less);
        IJ.log("   All speckles: " + NegLess);
        if (less != 0) {
            IJ.log("   Aver. speckle no/feature: " + (NegLess / less));
        }
        IJ.log("");
        IJ.log("Features with speckle no. higher max (green): " + more);
        IJ.log("   All speckles: " + NegMore);
        if (more != 0) {
            IJ.log("   Aver. speckle no/feature: " + (NegMore / more));
        }
        IJ.log("----------------------------------------------------");
    // IJ.log(" Speckle Inspector plugin by BioVoxxel/Dr. Jan Brocher, 2014, " + version);
    }
    if (secondaryObjectAnalysis) {
        analyzeRoiSet(primaryRoiManager, secondaryObjectImp);
    }
    analyzeObjects = null;
    // rm = null;
    Analyzer.setRedirectImage(null);
}
Also used : FloodFiller(ij.process.FloodFiller) ParticleAnalyzer(ij.plugin.filter.ParticleAnalyzer) Color(java.awt.Color) Recorder(ij.plugin.frame.Recorder) ImageConverter(ij.process.ImageConverter) Calibration(ij.measure.Calibration) ImagePlus(ij.ImagePlus) Roi(ij.gui.Roi) RoiManager(ij.plugin.frame.RoiManager) Font(java.awt.Font) ImageProcessor(ij.process.ImageProcessor) GenericDialog(ij.gui.GenericDialog) ResultsTable(ij.measure.ResultsTable)

Example 5 with FloodFiller

use of ij.process.FloodFiller in project jipipe by applied-systems-biology.

the class FloodFillMaskDrawerTool method onMouseClick.

@Subscribe
public void onMouseClick(MouseClickedEvent event) {
    if (!isActive())
        return;
    if (SwingUtilities.isLeftMouseButton(event)) {
        Point point = getViewerPanel().getCanvas().getMouseModelPixelCoordinate(true);
        if (point == null) {
            return;
        }
        ImageProcessor processor = getMaskDrawerPlugin().getCurrentMaskSlice();
        if (getMaskDrawerPlugin().getCurrentColor() == MaskDrawerPlugin.MaskColor.Foreground) {
            if (processor.get(point.x, point.y) > 0)
                return;
            FloodFiller filler = new FloodFiller(processor);
            try (BusyCursor cursor = new BusyCursor(getViewerPanel())) {
                processor.setValue(255);
                filler.fill(point.x, point.y);
            }
        } else {
            if (processor.get(point.x, point.y) == 0)
                return;
            processor.invert();
            FloodFiller filler = new FloodFiller(processor);
            try (BusyCursor cursor = new BusyCursor(getViewerPanel())) {
                processor.setValue(255);
                filler.fill(point.x, point.y);
            }
            processor.invert();
        }
        getMaskDrawerPlugin().recalculateMaskPreview();
        postMaskChangedEvent();
    }
}
Also used : ImageProcessor(ij.process.ImageProcessor) FloodFiller(ij.process.FloodFiller) BusyCursor(org.hkijena.jipipe.utils.ui.BusyCursor) Subscribe(com.google.common.eventbus.Subscribe)

Aggregations

FloodFiller (ij.process.FloodFiller)13 ImageProcessor (ij.process.ImageProcessor)6 ImagePlus (ij.ImagePlus)3 Calibration (ij.measure.Calibration)2 ResultsTable (ij.measure.ResultsTable)2 ParticleAnalyzer (ij.plugin.filter.ParticleAnalyzer)2 Recorder (ij.plugin.frame.Recorder)2 ShortProcessor (ij.process.ShortProcessor)2 Font (java.awt.Font)2 ImageInteger (bacmman.image.ImageInteger)1 Subscribe (com.google.common.eventbus.Subscribe)1 ImageStack (ij.ImageStack)1 GenericDialog (ij.gui.GenericDialog)1 ImageWindow (ij.gui.ImageWindow)1 Roi (ij.gui.Roi)1 Wand (ij.gui.Wand)1 CanvasResizer (ij.plugin.CanvasResizer)1 Analyzer (ij.plugin.filter.Analyzer)1 RoiManager (ij.plugin.frame.RoiManager)1 ByteProcessor (ij.process.ByteProcessor)1