Re: to Michal Schmid or other re Getting the number of pixels which values are below (between, now!) a certain threshold
Posted by Rasband, Wayne (NIH/NIMH) [E] on
URL: http://imagej.273.s1.nabble.com/Getting-the-number-of-pixels-which-values-are-below-a-certain-threshold-tp3688975p3688983.html
On Mar 14, 2010, at 4:29 PM, Steven Vanek wrote:
> Hi, this is a related question so Michal, I think your expertise or others may be helpful:
> I'm also a very new user though I think I can handle learning some macro code (is there a little tutorial for coding somewhere?)
>
> question is making a color-sensing function to return, within an rgb (jpeg in my case, though could save as a difft format) image or selection, the number of pixels with red in range (between) (r1,r2) AND green in range (g1g2) AND blue in range (b1,b2) -- essentially, triple thresholding in rgb color, and two threshold points instead of one. Output would ideally be writing a black and white image based on true/false for pixels with the above conditional, or just calculating percent of the image with the specified values.
>
> OR -- how could I dump all the RGB values in an image into a spreadsheet or equivalently a CSV file. Then I could do all the conditionals in excel or some such.
>
> I don't see anything like this as a preexisting command or macro in ImageJ thought I can't imagine it is too hard.
Here is a macro that converts pixels in an RGB image to white if the red, green and blue values are within specified ranges or black if they are not. It also calculates and displays the percent of the image with the specified values.
-wayne
if (bitDepth!=24)
exit("RGB image required");
Dialog.create("Thresholder");
Dialog.addNumber("r1:", 0);
Dialog.addNumber("r2:", 255);
Dialog.addNumber("g1:", 0);
Dialog.addNumber("g2:", 255);
Dialog.addNumber("b1:", 0);
Dialog.addNumber("b2:", 255);
Dialog.show();
setupUndo;
r1 = Dialog.getNumber();
r2 = Dialog.getNumber();
g1 = Dialog.getNumber();
g2 = Dialog.getNumber();
b1 = Dialog.getNumber();
b2 = Dialog.getNumber();
getSelectionBounds(x1, y1, width, height);
count = 0;
for (y=y1; y<y1+height; y++) {
for (x=x1; x<x1+width; x++) {
v = getPixel(x,y);
r = (v&0xff0000)>>16;
g = (v&0xff00)>>8;
b = (v&0xff);
if (r>=r1&&r<=r2&&g>=g1&&g<=g2&&b>=b1&&b<=b2) {
putPixel(x, y, 0xffffff);
count++;
} else
putPixel(x, y, 0);
}
}
print("Percent thresholded: "+count*100/(width*height));