Improper behavior of the roi.contains function?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Improper behavior of the roi.contains function?

Street, Jonathan (NIH/NIDDK) [F]
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.

Cheers

Jonathan Street
[hidden email]<mailto:[hidden email]>


p.s. I am aware that using roi.contains to achieve what this script currently does is not the right way. If I can overcome this issue with the roi I hope to develop it further in a direction that I don't think is going to be supported by getStatistics.


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Improper behavior of the roi.contains function?

Rasband, Wayne (NIH/NIMH) [E]
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