Login  Register

Re: Python iteration through Pixel/ROI to slow ... ?

Posted by Street, Jonathan (NIH/NIDDK) [F] on Oct 22, 2013; 4:17pm
URL: http://imagej.273.s1.nabble.com/Python-iteration-through-Pixel-ROI-to-slow-tp5005274p5005272.html

This is another issue I've encountered and previously received help with from people on the fiji IRC channel.

The issue is that converting a ROI into a mask does not operate on the entire image but on the bounding box for the ROI. If you have a 100by100 image with a circular ROI centered at 10,10 with a radius of 5 the mask will be only 10by10 (the size of the bounding box) so when you reach column or row 11 you'll get the out of bounds error.

I suggest instead of iterating over each pixel in the outer loops first iterate over each ROI. I don't know what your images look like but in my case this reduced the number of pixels I was checking by about 30%.

For each ROI iterate over the pixels with
r = roi.getBounds()
for y in range(r.height):
    for x in range(r.width):
        if mask.get(x,y) != 0:
            # Process

Here x and y will not match the location of the pixel in the original image so we need to offset them using r.x and r.y

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



On Oct 22, 2013, at 12:00 PM, Andreas Rzepecki <[hidden email]<mailto:[hidden email]>> wrote:

Dear Mr. Street,

thank you very much for the mask-hint. I tried to change my code to avoid roi.contains() and instead use mask.get:


for j in range(x):
 IJ.showProgress(j, x)
 sumpix_sap = 0
 for i in range(y):
   sap_row = 0
   for roi in RoiManager.getInstance().getRoisAsArray():
     mask = roi.getMask()
     if mask.get(j, i)!= 0:
       pix_sap = rt.getValue("Sapflow per Pixel", sap_row)
       sumpix_sap = sumpix_sap + pix_sap
     else:
      sap_row = sap_row + 1
 table.incrementCounter()
 table.addValue("Sapflow sum", sumpix_sap * 1000000)
IJ.showProgress(1)
table.show("IASF-Results")


This unfortunately leads to the following Java-error:

Traceback (most recent call last):
 File "<iostream>", line 44, in <module>
at ij.process.ByteProcessor.get(ByteProcessor.java:262)
at sun.reflect.GeneratedMethodAccessor21.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
java.lang.ArrayIndexOutOfBoundsException: java.lang.ArrayIndexOutOfBoundsException: 30

So I'm at this point  not sure if I have completely misunderstood the mask command.

Best regards

Andreas Rzepecki

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html