Exclude edge particles of non-rectangular selection with ParticleAnalyzer in Jython ImageJ script

Hello everyone,

I am trying to find all the particles that fall within a custom non-rectangular selection by using the ‘exclude edge particles’ option of the Particle Analyzer in a Jython script. However, it seems to me that I get different results when I do this manually or macro-recorded compared to when I use a Jython script, although the parameters look the same to me. As an example, the following recorded macro gives me only the particles within the selection (which is what I want):

run("Blobs (25K)");
setOption("BlackBackground", false);
run("Convert to Mask");
makePolygon(54,131,184,48,165,225);
run("Analyze Particles...", "  show=Overlay exclude in_situ");

whereas the following jython code gives me all the particles within the bounding box of the selection instead (which is not what I want).

from ij import IJ, ImagePlus
from ij.plugin.filter import ParticleAnalyzer
from ij.gui import Roi, PolygonRoi
from ij.measure import Measurements, ResultsTable

imp = IJ.openImage("http://wsr.imagej.net/images/blobs.gif")
IJ.run(imp, "Convert to Mask", "")
imp.show()
imp.setRoi(PolygonRoi([54,184,165], [131,48,225], Roi.POLYGON))

pa = ParticleAnalyzer((ParticleAnalyzer.SHOW_OVERLAY_OUTLINES + ParticleAnalyzer.EXCLUDE_EDGE_PARTICLES),(Measurements.AREA),ResultsTable(),0,1000000,0,1)
pa.analyze(imp) 

imp.setRoi(PolygonRoi([54,184,165], [131,48,225], Roi.POLYGON)) # set the roi again for visibility only

Does anyone have an idea how to fix this? Unfortunately, I cannot simply use the macro for my analysis, because what I am trying to do is part of a bigger Jython script. My apologies if I am simply overlooking something silly.

Many thanks for your help!

Anniek

jython macro