MATLAB: How to retrieve RGB array from a colormap given a CData
cdatacolormapMATLABrgb
Hello,
If I have a designated colormap, jet for example, and the caxis is set to [0 1] and I want to return the RGB values (array of three numbers) of CData 0.5, how can I do this?
Thanks!
Best Answer
Isn't it just half way up your colormap? So if you specify 256 colors
cmap = jet(256);
and the first row cmap(1,:) applies to 0, and the last row cmap(256,:) applied to values of 1, then 0.5 will be the middle row of your colormap (green if you chose jet). See:
grayImage = imread('moon.tif');
grayImage = im2double(grayImage);
imshow(grayImage);
cmap = jet(256);
colormap(gca, cmap);
colorbar;
% Find the middle row, which corresponds to an image value of 0.5
middleRow = round(size(cmap, 1) / 2)
% Print the color there out to the command window:
cmap(middleRow, :)
% Shows ans = 0.5 1 0.5 (green color as expected);
You can obtain the indices based on the colormap and the Z vector applied to the CData property. Then you can get the corresponding RGB values using the "ind2rgb" function as follows:
h = scatter(x,y);
h.CData = z;
Cdata = h.CData;
cmap = colormap;
% make it into a index image.
cmin = min(Cdata(:));
cmax = max(Cdata(:));
m = length(cmap);
index = fix((Cdata-cmin)/(cmax-cmin)*m)+1; %A
% Then to RGB
RGB = ind2rgb(index,cmap)
For information on the "ind2rgb" function, refer this link:
Best Answer