Scripting maxima finder in Jython returns x, y coordinates as 0 (always)

Hello!

I have run into a strange issue which I am not sure if anyone else is facing. I use the MaximumFinder to find maxima using jython script. When I run it using imagej GUI I detect the maxima but using the same parameters via scripting, I get maxima whose coordinates are always 0.

A code snippet to illustrate this:

#@ImagePlus imp 

from ij import IJ
from ij import ImagePlus
from ij.plugin.filter import MaximumFinder

noiseLevel = 50
ip = imp.getProcessor()
excludeOnEdges= True
maxima = MaximumFinder().getMaxima(ip, noiseLevel, excludeOnEdges)

x_coordinates = maxima.xpoints
y_coordinates = maxima.ypoints

print "x = ", x_coordinates
print "y = ", y_coordinates

I used the Cell_Colony as well as blobs image (both inverted) to test this and the only output I get is:

#Output
x =  array('i', [0, 0, 0, 0])
y =  array('i', [0, 0, 0, 0])

If I use Process > Find Maxima on the same images with same parameters, I get 518 and 53 Maxima in Cell_Colony and blobs images respectively. Related post: Scripting findMaxima in Jython

Any help is appreciated!

I think this is a known issue (that I’ve also been confused by in the recent past). Updating to the daily IJ build fixed it!

I tried your script on cell colony and I dont get any max even with the last ImageJ.
However using noiseLevel=10 it works.

Still to be on the safe side, the best is always to check if there are indeed points in maxima.
Because maxima.xpoints and ypoints are anyway initialised to [0,0,0,0] even when no maxima were found.
maxima.npoints though will correctly be 0 if there are no maxima detected.

so something like

if maxima.npoints:

    x_coordinates = maxima.xpoints
    y_coordinates = maxima.ypoints

    print "x = ", x_coordinates
    print "y = ", y_coordinates

else:
    print "Found no maxima"
1 Like

I will give this a try!

Woah, I did not know about the initialisation. Thanks for the tip!

It’s not obvious indeed, and it’s kind of a misfeature, unfortunately we can’t do much about it as it has to deal with the Polygon class, not ImageJ/Fiji as explained in this post: MaximumFinder finds maxima in black images

But maybe the Javadoc for getMaxima could be more clear about this behaviour when no maxima is detected.
What do you think @Wayne ?

1 Like

I updated the Javadocs of the MaximumFinder.getMaxima() methods to make it clearer that the npoints variable of the returned Polygon is the number of maxima found, not the length of the xpoints variable.

3 Likes

Thanks Wayne!

For the record, here’s the relevant commit on GitHub:

1 Like