Getting amount of pixels above a certain number in rectangle moving across the screen

jython
imagej
#1

Hello, below i’ve provided the code for getting the mean value of a drawn rectangle in a for loop. i tried cropping the image and then using IJ.getImage and running the statistics on that. However this doesn’t work.

Is it possible to call the mean value just from the rectangle?

from ij import IJ, ImagePlus
imp = IJ.getImage()
width = imp.width
ip = imp.getProcessor().convertToFloat()
pixels = ip.getPixels()
xc = .2 #start making boxes from this pixel location.

for i in range(len(pixels)):

start = xc * width; #pixel to start with
v = pixels[start + i]
xcc = v % width
ycc = v / width

IJ.setTool("rectangle");
IJ.makeRectangle(xcc, ycc, 20, 20);
b = IJ.run("Crop");


options = IS.MEAN | IS.MEDIAN | IS.MIN_MAX  

def getStatistics(impp):  
  
  global options  
  ip = impp.getProcessor()  
  stats = IS.getStatistics(ip, options, impp.getCalibration())  
  return stats.mean, stats.median, stats.min, stats.max  	
	
mean, median, min, max = getStatistics(b)  	 
if mean > 10: 
	break
b.close()
#2

Hi,
yes it is possible, you don’t have to crop to get the mean value in the roi. There is probably a number of different ways this can be done. Here is what I did recently:

 rect = Roi(point.x-1, point.y-1, 3, 3)
 imp.setRoi(rect)
 stats = imp.getStatistics()
 if (stats.mean > 56 and stats.mean<57):
       ...

Best regards,
Volker

#3

Thanks Volker, I appreciate the response. it worked as expected. :slight_smile: