Analyzer vs ParticleAnalyzer - Setting Redirectimage

I am writing a Java plugin to measure the size, location and grey level of “particles” in an image.

In manual mode i have found that the Yen threshold gives good results, but during the particle analysis I need to redirect the measurements to the original image to get the grey level.

Trying to do this in Java is stumping me. I have found that ij.plugin.filter.Analyzer has setRedirectImage(ImagePlus imp), and that ParticleAnalyser does tests for whether RedirectImage has been set, but I cannot see how to set the RedirectImage for ParticleAnalyser.

I am sure I am missing something obvious, but I have been looking all day and I am stuck. All help appreciated.

ParticleAnalyzer uses the static methods Analyzer.isRedirectImage() and Analyzer.getRedirectImage() from ij.plugin.filter.Analyzer:

So setting the image via Analyzer.setRedirectImage(ImagePlus imp) should work. In case it doesn’t work for you, can you show some minimal example illustrating the issue?

1 Like

Thanks, that works. I hadn’t realised that Analyzer was always available, and when I did try it I spelled it the UK way (Analy s er) and so it threw an error.

For reference, I am now using this to get the size, grey level and position of the blob in my image, where img1 is the original greyscale image and img2 is the thresholded image:

ImagePlus img1 = WindowManager.getCurrentImage();
ImagePlus img2 = new Duplicator().run(img1);
IJ.setAutoThreshold(img2, "Yen dark");
IJ.run(img2, "Convert to Mask", "");

int opts = PA_EXCLUDE_EDGE_PARTICLES | PA_SHOW_PROGRESS | PA_CLEAR_WORKSHEET | PA_INCLUDE_HOLES;
int meas = MEAS_AREA | MEAS_MEAN | MEAS_CENTER_OF_MASS;

double minSize = Math.PI*Math.pow((10.0/2),2.0);
double maxSize = Math.PI*Math.pow((150.0/2),2.0);

ResultsTable rt1 = new ResultsTable();
ParticleAnalyzer pa = new ParticleAnalyzer(opts, meas, rt1, minSize, maxSize);
Analyzer.setRedirectImage(img1);
pa.analyze(img2);

//code to set i to the blob of interest

double dot_area=-1, dot_x=-1, dot_y=-1, dot_mean=-1;
dot_area = rt1.getValueAsDouble(rt1.getColumnIndex("Area"), i);
dot_mean = rt1.getValueAsDouble(rt1.getColumnIndex("Mean"), i);
dot_x = rt1.getValueAsDouble(rt1.getColumnIndex("XM"), i);
dot_y = rt1.getValueAsDouble(rt1.getColumnIndex("YM"), i);

If there are any gotchas I have missed in this code, please let me know.

I want to change the IJ.setAutoThreshold and IJ.run(img2, “Convert to Mask”, “”) to direct Java calls as well, but that hasn’t happened yet.

It’s really worth checking out an IDE like Eclipse when developping Java plugins. It immediately tells you about errors like this (‘Analyser cannot be resolved’) and will suggest corrections.


These are the (ImageJ1) low-level equivalents (unless you want to use ImageJ-Ops some day):

// IJ.setAutoThreshold(img2, "Yen dark");
img2.getProcessor().setAutoThreshold("Yen", true, 0);

// IJ.run(img2, "Convert to Mask", "");
thr = new Thresholder(); // ij.plugin.Thresholder
WindowManager.setTempCurrentImage(img2);
thr.run("mask");
WindowManager.setTempCurrentImage(null);

2 Likes

Yes, I have found the web page with instructions for setting up Eclipse, and I think I will make time to do that very soon.

Thanks for the hints on the other functions.

A post was split to a new topic: Converting ACR-NEMA (DICOM) files