Value of RGB at a pixel(x,y) in image J

Hi,
I have a doubt in image J
I have some x and y position of few pixels in my ROI manager and I have to find and store corresponding RGB value.I didnt see any tool to collect RGB of a pixel

Interesting fact: While you scroll through the image in fiji/image J RGB value is showing under toolbar.But I dont know how to store these.
Please help…

Hi @Sandra_Mankore

check https://imagej.nih.gov/ij/developer/macro/functions.html

getPixel(x, y)
Returns the raw value of the pixel at (x,y). Uses bilinear interpolation if 'x' or 'y' are not integers.
Use getValue(x,y) to get calibrated pixel values from 8 and 16 bit images and intensity values from RGB images.
Note that pixels in RGB images contain red, green and blue components that need to be extracted
using shifting and masking.
See the Color Picker Tool macro for an example that shows how to do this. 

https://imagej.nih.gov/ij/macros/tools/ColorPickerTool.txt

2 Likes

Sir,
What do mean by ‘shifting and masking’ to get RGB from RGB images?
Can you explain with an example?

RGB images are stored as Integer data.

Integer data consists of 32bits - or in other words - contains 4 Bytes (4 x 8bit = 32bit).

Integer = Byte3 | Byte2 | Byte1 | Byte0

The 8bit R, G, B values are stored as Byte2, Byte1, Byte0 in the Integer value.

To retrieve the 8bit R, G, B values from the Integer number the data bits have to be shifted by the shift operator >> and masked by the hex number 0xff.

The operations can be seen in the above ColorPickerTool macro (mentioned above):

   macro "Color Picker Tool -C44f-o4499" {
        getCursorLoc(x, y, z, flags);
        v = getPixel(x,y);
        row = nResults;
        setResult("X", row, x);
        setResult("Y", row, y);
        if (nSlices>1) setResult("Z", row, z);
        if (bitDepth==24) {
            red = (v>>16)&0xff;  // extract red byte (bits 23-17)
            green = (v>>8)&0xff; // extract green byte (bits 15-8)
            blue = v&0xff;       // extract blue byte (bits 7-0)
            setResult("Red", row, red);
            setResult("Green", row, green);
            setResult("Blue", row, blue);
        } else
            setResult("Value", row, v);
        updateResults;
   }

Here is a simple version:

v = getPixel(x,y);
red = (v>>16)&0xff;  // extract red byte (bits 23-17)
green = (v>>8)&0xff; // extract green byte (bits 15-8)
blue = v&0xff;       // extract blue byte (bits 7-0)

Sir,
We have coordinates of ‘centre of mass’ different particlesin ROI manager.
Can you please share the macro to get each XM,YM from Results box directly and take their RGB value.

If I would have such a macro I would share it with you. But I don’t have it.
My hint:
Create a macro with the following steps

  • Collect the x/y pairs from the result table
  • Iterate through all pairs
  • Retrieve the pixel value by using getPixel(x, y) for each x/y pair
  • Extract the RGB values as described above

Regarding macro programming a lot of information can be found:
https://imagej.nih.gov/ij/developer/macro/macros.html
https://imagej.nih.gov/ij/developer/macro/functions.html
https://imagej.nih.gov/ij/macros/examples/

Hope that helps.

Note that the centre of mass of a region does not necessarily fall in a pixel belonging to that region unless there are some other conditions fulfilled (eg the region is convex, contains no holes, has certain size [where is the centre of mass in a region made by 2 pixels joined by the corners?], etc.). The pixel XM, YM could actually be on the background (eg a “C” shaped region) or even in a different region that occupies that background pixel.

Yes sir…I am aware of that…Lucky thing is that all my particles are round in nature…nothing convex…

Sure sir…The links and macro code helped.I was able to write code for collecting RGB value for specific x and y.
If possible,can you please tell how to collect xm and ym , and itirate the macro multiple times??

Check Built-in Macro Functions

There you can find the command to retrieve the values from the ResultsTable

getResult("Column", row)
Returns a measurement from the ImageJ results table or NaN if the 
specified column is not found. The first argument specifies a column 
in the table. It must be a "Results" window column label, such as "Area", 
"Mean" or "Circ.". The second argument specifies the row, where 
0<=row<nResults. nResults is a predefined variable that contains the 
current measurement count. (Actually, it's a built-in function with the "()"
 optional.) Omit the second argument and the row defaults to nResults-1
 (the last row in the results table). See also: nResults, setResult, isNaN, 
getResultLabel. 

Please check the macro examples to learn how to iterate over the array of results.

Thank you sir…This helped…