MaximumFinder finds maxima in black images

I think I found a bug with the MaximumFinder class.
In empty images (all pixels to 0 or 1) it still finds 4 maxima, all of coordinates (0,0) !

It was messing up a pipeline in which I was detecting maxima in “partially” thresholded images*, which were eventually black when the threshold is too high.
*(“TO_ZERO” threshold of opencv that keeps initial pixel values above threshold, assign 0 value below)

Try it yourself (jython)

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

# Create a blank image
imp = IJ.createImage("Untitled", "8-bit white", 20, 20, 1) # white or black, -> find 4 peaks at 0,0 !?
#imp = IJ.createImage("Untitled", "8-bit noise", 20, 20, 1) # -> this is fine

# Detect maxima
tolerance = 10
excludeOnEdge = False
Polygon = MaximumFinder().getMaxima(imp.getProcessor(), tolerance, excludeOnEdge)

# Print x,y of maxima
for x,y in zip(Polygon.xpoints, Polygon.ypoints):
	print x,y

Related issue on GitHub

1 Like

I cannot reproduce this bug. The following JavaScript code outputs “polygon.npoints=0” when I run it using the latest version of ImageJ (1.52m).

  imp = IJ.createImage("Untitled", "8-bit black", 20, 20, 1);
  tolerance = 10;
  excludeOnEdge = false;
  polygon = new MaximumFinder().getMaxima(imp.getProcessor(), tolerance, excludeOnEdge);
  print("polygon.npoints="+polygon.npoints);

For the record, as posted on the GitHub issue as well:

the “problem” is that the public fields xpoints and ypoints of an empty java.awt.Polygon get initialized as int[4] (likely for performance reasons), as illustrated by this Groovy script:

import java.awt.Polygon

p = new Polygon()
println p.xpoints // [0, 0, 0, 0]
println p.ypoints // [0, 0, 0, 0]

So indeed you’ll first have to use npoints to check how many points you can query.

2 Likes