On May 15, 2013, at 11:07 AM, Street, Jonathan (NIH/NIDDK) [F] wrote:
> Hello everyone,
>
> I'm running into some issues iterating over the pixels in a roi and I'm hoping someone here can help me out. What I think is happening is roi.contains is returning true if the pixel is inside the bounding box for the roi.
>
> I'm using Fiji and working on a script in Python. My versions are ImageJ 1.47n; Java 1.6.0_45 [64-bit].
>
> I am running
http://pastebin.com/yc0Xd9e4 on
http://i.imgur.com/CCDnBPm.png with
http://pastebin.com/kn4guT30 as the roi.
>
> The roi is a right-sided triangle with a side length of 80 so the area should be 3200. Using pixelCount from the getStatistics function gives 3160 which is reasonable. When I iterate over all the pixels in the image and count those for which roi.contains returns true the value I get is 6400 which is the area for a square with a side length of 80.
>
> I don't know if I'm just going about this wrong or whether this is a genuine bug but any thoughts on this would be appreciated.
Change
roi = imp.getProcessor().getRoi()
to
roi = imp.getRoi()
and the script will work as expected. It's not what you would expect, but ImageProcessor.getRoi() returns a Rectangle, not an Roi. Change the script to use a mask instead of roi.contains() and it will run ~6 times faster. Here is an example Python script that uses a mask to calculate the pixel count and mean pixel value of a non-rectangular selection.
imp = IJ.getImage()
roi = imp.getRoi()
mask = roi.getMask()
ip = imp.getProcessor()
r = roi.getBounds()
total = sum = 0
for y in range(r.height):
for x in range(r.width):
if mask.get(x,y)!=0:
total += 1
sum += ip.getf(r.x+x,r.y+y);
print "count=", total
print "mean=", sum/total
-wayne
--
ImageJ mailing list:
http://imagej.nih.gov/ij/list.html