Save pixel color value for indexed image (without image Processing Toolobx)

With reference to Steve Eddins blog post on pixel colors: http://blogs.mathworks.com/steve/2006/02/10/all-about-pixel-colors-part-3/
Suppose I create his same image: A = magic(5);
Then I display it with imagesc and colormap HSV: imagesc(A); colormap(hsv);
Is there any way now (without using Image processing Toolbox) to save to a matrix the R,G,B triplets corresponding to the colors of each pixel as produced using HSV?
Thanks, Matteo

1 Comment

Thanks Walter and Sean.
It will take me some time but i will try both suggestions and then will comment on results.
Matteo

Sign in to comment.

 Accepted Answer

apply that routine and then get() the CData property and that will be the RGB.

6 Comments

This worked
A = magic(5);
hA=imagesc(A);colormap(hsv);
RGB=get(hA,'CData')
RGB =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Now I am trying to figure out how to get the alpha as well. I will let you know if I get that figured out.
That's just a magic square of size 5;
magic(5)
You need to figure out how to map that to the colormap. When the number of unique values in the image is less than the length of the map; it will index directly, i.e. 1 will be the first element, 2 will be the second.
When you have more unique elements - say uint16 image where you can have integer values of 65535 and you're map is only 256 elements or 64 elements or however many elements, then you need to figure out how they scale. That's what I attempted do with histc. You copuld always just make a colomap contain as many values as you ave unique in your image. For a uint8 image this is trivial map = hsv(256) and the pixel values will line up with an offset of 1, i.e. 0->1st row of map; 1->2nd row of map;255->256th row of map)
When you have an indexed image, getting the CData of it is going to return the indexes, not the color it is mapped to. You need to convert the indexes in to color values. The freezeColors routine from the FEX sets the CData to the color values, and Sean's routine calculates the color values without going through CData, but the block of code you show here will just return the original indices.
Matteo, There was no Alpha mentioned in your question, and hsv color map does not use Alpha. Which Alpha are you referring to?
Sorry, it took me a bit to get back to this.
The example in my original question was a simple one, and with a reference to Steve's blog, so it was perfect to explain what I wanted to do (I often tent to be a bit vague, typical of non native speakers).
Sean, usually my images would have more than 256 elements, so I will have to look into the suggested method more carefully as I'm not familiar with either histc or numel.
With one of the maps I had in mind I was trying to get to the RGB colors after the application of an overlay with alpha as in this other post by Steve: http://blogs.mathworks.com/steve/2009/02/18/image-overlay-using-transparency/ . Walter's idea of using freezeColors worked in that case too, with a slight adaptation. I'm about to post an example on my blog and I will acknowledge the answer, then post it on this thread.
Hi Walter
Here's my blog post showing what I am doing (I used your suggestion and applied to color with shading)
http://mycarta.wordpress.com/2012/04/05/visualization-tips-for-geoscientists-matlab-part-ii/
Thanks again. And thanks to Sean as well.
Matteo

Sign in to comment.

More Answers (1)

Something like:
A = magic(5);
imagesc(A);
map = hsv;
colormap(map);
Av = A(:);
[~, bin] = histc(Av,linspace(min(Av),max(Av),min(numel(Av),size(map,1))));
mapp = permute(map,[1 3 2]);
rgb = reshape(mapp(bin,:,:),size(A,1),size(A,2),3);
maybe?

3 Comments

Hi Sean
I get this error message:
??? [~, bin] = histc(Av,linspace(min(Av),max(Av),min(numel(Av)),size(map,1)));
|
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.
You missed a closing ')' on that line - just like the error message says.
Just copy and paste it.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!