Login  Register

Results table issue in plugin

Posted by gankaku on May 08, 2015; 3:51pm
URL: http://imagej.273.s1.nabble.com/Results-table-issue-in-plugin-tp5012781.html

Dear List,

after some improvement from Thorsten Wagner on the "Watershed Irregular
Features" function of my Toolbox (thanks Thorsten) and some additional
modifications by myself, I encounter the following issue after a batch
processing test and running it on stacks.

The watershed plugin is designed in a way that internally it is working
with a results table which works well. However, as soon as a results table
is open and contains results during batch processing it was reset after
each watershed function.
I therefore considered simply to rename the table during the watershed
process and retrieve it afterwards since I am not aware of any more elegant
solution to circumvent that problem. What is inconvenient in this approach
is that an existing results table is constantly renewed or popping up if
different values are keyed in during preview mode or after watershedding a
single slide when running it on a stack.

This version is still not available as an update, since I first wanted to
solve this issue. Below you can find the corresponding java code for those
how want to have a look or test the described behaviour.

I am grateful for any suggestions on more elegant solutions.

Thanks,
Jan


//----------------------------------
import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.filter.*;
import ij.measure.Measurements;
import ij.measure.ResultsTable;
import ij.text.TextWindow;
import ij.plugin.frame.PlugInFrame;
import ij.gui.DialogListener;
import ij.gui.GenericDialog;
import ij.plugin.filter.EDM;
import ij.plugin.ImageCalculator;
import ij.plugin.filter.ParticleAnalyzer;

/**
 * Watershed Irregular Features
 *
 * The plugin might serve to reduce artifacts created by the normal
 * watershed algorithm implicated in ImageJ when applied to objects
 * of irregular shape containing relatively small connections between them.
 *
 * Copyright (C), 2014, Jan Brocher / BioVoxxel
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * IN NO EVENT WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES
 * AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR
DAMAGES,
 * INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
ARISING
 * OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT
LIMITED TO
 * LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR
 * THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS),
 * EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY
OF
 * SUCH DAMAGES.
 *
 * Please cite BioVoxxel and Jan Brocher when you publish results
 * obtained by usage of this plugin or a modified version of it
 *
 * Thank you
 *
 * May 04, 2015: Bug fix: Now correctly works with stacks (Thorsten Wagner,
[hidden email])
 * May 08, 2015: Bug fix: when used during batch analysis results table
were reset. This behaviour corrected
 *
 */


public class Watershed_Irregular_Features implements ExtendedPlugInFilter,
DialogListener {
ImagePlus imp;
private double erosions = 1;
private PlugInFilterRunner pfr;
private int nPasses = 1;
private int pass;
private int flags = DOES_8G|KEEP_PREVIEW|SNAPSHOT;

public int setup(String arg, ImagePlus imp) {
this.imp = imp;
if(!imp.getProcessor().isBinary()) {
IJ.error("works only on 8-bit binary images");
return DONE;
} else {
return DOES_8G | DOES_STACKS;
}
 }

public int showDialog(ImagePlus imp, String command, PlugInFilterRunner
pfr) {
GenericDialog gd = new GenericDialog("Watershed Irregular Features");
gd.addNumericField("erosion cycle number:", erosions, 1, 5, "");
                gd.addPreviewCheckbox(pfr); // passing pfr makes the filter
ready for preview
                gd.addDialogListener(this); // the DialogItemChanged method
will be called on user input
                gd.addHelp("
http://fiji.sc/BioVoxxel_Toolbox#Watershed_Irregular_Structures");
                gd.showDialog(); // display the dialog; preview runs in the
background now
                if (gd.wasCanceled()) {
                 return DONE;
                }
               IJ.register(this.getClass()); // protect static class
variables (filter parameters) from garbage collection
        this.pfr = pfr;
        return IJ.setupDialog(imp, flags); // ask whether to process all
slices of stack (if a stack)
}

public boolean dialogItemChanged(GenericDialog gd, AWTEvent e) {
erosions = (int) gd.getNextNumber();
if (gd.invalidNumber() || erosions<1) {
IJ.error("invalid number");
return false;
} else {
return true;
}
}

public void run(ImageProcessor ip) {
// ip.snapshot();
Prefs.blackBackground = true;
TextWindow rttw = ResultsTable.getResultsWindow();
ResultsTable rt;
if(rttw!=null) {
rt = ResultsTable.getResultsTable();
IJ.renameResults("Results", "intermediateResults");
}
 boolean invertedLut = ip.isInvertedLut();
if(invertedLut) {
ip.invertLut();
}
ImagePlus origImp = new ImagePlus("",ip.duplicate());
ImageProcessor erosionIP = ip.duplicate();
ImagePlus erosionImp = new ImagePlus("", erosionIP);

ImageProcessor watershedIP = ip.duplicate();
ImagePlus watershedImp = new ImagePlus("", watershedIP);

//dilate objects according to erosion cycle number entered by user
for(int n=0; n<=erosions; n++) {
erosionIP.dilate();
}

//separate original objects with the normal watershed algorithm from IJ
EDM watershedEDM = new EDM();
watershedEDM.toWatershed(watershedIP);

//first, the watershed separation lines are extracted
//then, the second image calculation keeps only those separators
//which overlap with an eroded particle
ImageCalculator calculateImages = new ImageCalculator();
ImagePlus extractedSeparatorsImp = calculateImages.run("XOR create",
watershedImp, origImp);
 ImagePlus remainingSeparatorsImp = calculateImages.run("AND create",
extractedSeparatorsImp, erosionImp);

//the remaining separator lines are analyzed to get their starting position
//for later selection
int options =
ParticleAnalyzer.CLEAR_WORKSHEET|ParticleAnalyzer.RECORD_STARTS;
int measurements = Measurements.CENTROID;
ResultsTable resultsTable = new ResultsTable();
ParticleAnalyzer pa = new ParticleAnalyzer(options, measurements,
resultsTable, 0.0, 999999999.9);

pa.analyze(remainingSeparatorsImp);
int xStart, yStart;

watershedIP.setValue(255.0);
 //the remaining separation lines in their original size and orientation
//are copied into the watersheded image to close undesired separations
for(int r=0; r<resultsTable.getCounter(); r++) {
xStart = (int)resultsTable.getValue("XStart", r);
yStart = (int)resultsTable.getValue("YStart", r);
IJ.doWand(extractedSeparatorsImp, xStart, yStart, 0.0, "8-connected");
Roi selectedSeparator = extractedSeparatorsImp.getRoi();
watershedIP.fill(selectedSeparator);
 }
//watershedImp.show();
//the corrected watersheded image is copied into the original to be able to
//undo the watershed if undesired
//origImp.getProcessor().setPixels(watershedImp.getProcessor().getPixels());
 if(invertedLut) {
ip.invertLut();
}
for(int i = 0; i < ip.getWidth(); i++){
for(int j =0; j < ip.getHeight(); j++){
ip.putPixel(i, j, watershedImp.getProcessor().getPixel(i, j));
}
}
 /*
//prepare all unused ImagePlus, ImageProcessor and Constructors for carbage
collection
watershedImp.close();
watershedEDM = null;
erosionImp.close();
extractedSeparatorsImp.close();
remainingSeparatorsImp.close();
resultsTable = null;
pa = null;
*/
 if(rttw!=null) {
IJ.renameResults("intermediateResults", "Results");
}
 }


public void setNPasses (int nPasses) {
this.nPasses = nPasses;
pass = 0;
}

void showProgress(double percent) {
percent = (double)(pass-1)/nPasses + percent/nPasses;
IJ.showProgress(percent);
}

}

//----------------------------------









--

CEO: Dr. rer. nat. Jan Brocher
phone:  +49 (0)6234 917 03 39
mobile: +49 (0)176 705 746 81
e-mail: [hidden email]
info: [hidden email]
inquiries: [hidden email]
web: www.biovoxxel.de

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html