Search code examples
javaimagejthreshold

Imagej getting a summary to be displayed and extracting its values


I am trying to build a simple java app, which would count particles and then display their number. I am using imagej (ij.jar) library, everything works perfect until the little box with result is supposed to be shown. COuld anyone pease explain to me how can I display that. And the second thing, how do I extract that value to an int.

my current code: button action, which leads to:

ImagePlus imp = IJ.openImage("C:\\Users\\bibaleze69\\Desktop\\imageprocessing\\chromosomes2.jpg");
imp1.show();
IJ.run(imp, "Color Threshold...", "");       
IJ.run(imp, "Analyze Particles...", "show=[Overlay Outlines] display summarize");

Solution

  • how do I extract that value to an int.

    I believe you want to use the Results Table API. ResultsTable.getResultsTable will get the active results after your Analyze Particles command, and you can then interrogate it with the getValue methods.

    COuld anyone pease explain to me how can I display that

    I think your Analyze Particles isn't running because running Color Threshold... doesn't actually apply the threshold to create a mask or 8-bit image, it just opens the thresholding interface. I actually had a lot of trouble trying to get the Color Threshold to apply. I think you have a couple options though:

    • If you run Color Threshold in Fiji, and open the Macro Recorder, you can press the "Macro" button in the Color Threshold dialog to dump a ton of IJ1 Macro code to the recorder. This code will actually apply the selected threshold to your image and create an image that can then be input to Analyze Particles. But you would have to replicate this process on the Java side and I don't know how flexible it will be.

    • Alternately, you can adapt the following IJ1 Macro code to just threshold the image as 8-bit.

      run("8-bit"); setAutoThreshold("Default"); //run("Threshold..."); setOption("BlackBackground", false); run("Convert to Mask");

      If you can get the desired threshold from just operating on an 8-bit version of your image then this is a much simpler option to get your data to Analyze Particles.

    If these options don't fully work for you, I would also recommend writing to the ImageJ mailing list, which is read by the Threshold Colour author, among many others.