How to read the RGB value from a grayscale image (just like the data cursor does)

I have a DICOM image. I have read it and stored the value in variable 'I'. So, how to read the RGB value from a grayscale image (just like the data cursor does). Here is the image with the data cursor. Variable 'I' is consisted of the index values shown in the second line on the picture. I wonder whether we can read the RGB value by command just like the third value shown on the image. If yes, how to do it? Thanks a lot

 Accepted Answer

cmap = colormap();
RGB = ind2rgb(YourImage, cmap);
RGB(290, 189) %notice that X is columns and Y is rows
However, if you used imshow() to display the image, then chances are that imshow decided on the colormap for you instead of using the current colormap. If so, then instead of assigning colormap() to cmap, use
cmap = gray(256);

7 Comments

Hi, Walter, thank you. But the result value is 1, not the same to the data cursor value. Why does this happen?
For example,
Image = imread('cameraman.tif');
imshow(Image);
I dropped a data cursor on it, which said
[X,Y] [168 198]
Index: 60
[R,G,B]: [0.2353 0.2353 0.2353]
So if I now
cmap = gray(256)
cmap(60+1, :)
I get 0.235294117647059 0.235294117647059 0.235294117647059
so index 60 (uint8) does map to the same color as the data cursor shows.
Now,
RGB = ind2rgb(Image, cmap);
X = 168; Y = 198;
RGB(Y, X, :)
and the result is
ans(:,:,1) =
0.235294117647059
ans(:,:,2) =
0.235294117647059
ans(:,:,3) =
0.235294117647059
so using ind2rgb with the appropriate colormap does get you the same results as the data cursor. Provided, that is, that you use Y for rows and X for columns, (Y,X,:) rather than the less confusing but incorrect (X,Y,:)
Hey Walter, thank you. Here is my code:
Image=dicomread('D:\CT\001\001.dcm');
figure;
imshow(Image,[]);
cmap=gray(256);
RGB=ind2rgb(Image,cmap);
X=189;Y=290;
RGB(Y,X,:)
and the result is:
% code
ans(:,:,1)=1
ans(:,:,2)=1
ans(:,:,3)=1
and the result shown in Data cursor is:
If you are always using a greyscale colourmap then the answer is simply the normalised value at the defined point in your matrix, you don't even need the colourmap.
thanks to Adam and Walter. It works by using the function mat2gray. But how to define the amin and amax value that correspoing to 0 and 1 in grayscale image? we have several nomalized value in 'Image' that corrsponding to 1. How to define the minimum one? thanks a lot to your help.
If I use the function mat2gray by the default setting,the code is
if true
Image=dicomread('D:\CT\001\001.dcm');
figure;
imshow(Image,[]);
I=mat2gray(Image);
figure;
imshow(I,[])
end
But the value has a little difference to the data cursor. Here is the picture:

Sign in to comment.

More Answers (2)

Alternatively, I wrote a function for this a while back. It's been a while since I wrote it, so no guarantees regarding how well it works. It's called colorpicker.
The function you want is called impixelinfo():
hp = impixelinfo();
set(hp, 'Units', 'normalized');
set(hp, 'Position', [.5, .9, .3, .05]); % Or whatever size and location you want.
This will put up a status label where you can see the x,y and RGB values as you mouse around over the image.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!