Posted by
Street, Jonathan (NIH/NIDDK) [F] on
Oct 22, 2013; 2:00pm
URL: http://imagej.273.s1.nabble.com/Python-iteration-through-Pixel-ROI-to-slow-tp5005274p5005266.html
I posted a similar script to the mailing list a while ago for help with an unrelated problem and received the following tip from Wayne Rasband.
Even at their best Jython iterations are slow. I eventually moved to using a combination of masks, the image calculator and getStatistics. I only had one ROI for each image so such a solution may not be as useful for your situation.
> 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
Jonathan Street
[hidden email]
On Oct 22, 2013, at 8:06 AM, Andreas Rzepecki <
[hidden email]> wrote:
> Hello together,
>
> I wrote a more or less complex plugin in python for Fiji, which helps me to analyse every pixel of an image (row by row) in combination with some ROI calculation. The iteration I use is:
>
> 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():
> if roi.contains(j, i):
> pix_sap = rt.getValue("Sapflow per Pixel", sap_row)
> sumpix_sap = sumpix_sap + pix_sap
> break
> else:
> sap_row = sap_row + 1
> table.incrementCounter()
> table.addValue("Sapflow sum", sumpix_sap * 1000000)
> IJ.showProgress(1)
>
>
> For a test picture (620x90 pixel, 197 ROIs) it needs some seconds to calculate my result values.
>
> For another test picture (592x301, 1172 ROIs) it needs around 6 minutes.
>
> Unfortunatly I need to analyse pictures with 6000x1000+ pixel and 100000+ ROIs. I tried it but stopped it after around 30 minutes with the progress bar not moving once.
>
> Is this a problem with python iterations or did I something wrong with my code, which slows down the progress unnecessarily?
>
> I would appreciate any hint/help. Thank you very much in advance.
>
> Andreas
>
> --
> ImageJ mailing list:
http://imagej.nih.gov/ij/list.html--
ImageJ mailing list:
http://imagej.nih.gov/ij/list.html