map from value to colormap

47 views (last 30 days)
Guy Eyal
Guy Eyal on 31 Jul 2014
Edited: KSSV on 23 Jun 2020
I use pcolor to create a figure out of some matrix of data.
h=pcolor(X,Y,M);
Is it possible to receive for a specific index in the matrix in which color (RGB values) pcolor used (assuming a constant colormap)?
Or should i ask it - If I have a colormap and caxis - is there any function that will give me the color of a value (mapping from value to rgb value in the colormap)?
Thanks

Accepted Answer

Arun Mathew Iype
Arun Mathew Iype on 31 Jul 2014
Please check the solution provided at the below link by Jason D. Yeatman which does the exact thing that you want http://web.stanford.edu/~jyeatman/AFQ/doc/utilities/vals2colormap.html#_top
function rgb = vals2colormap(vals, colormap, crange)
0002 % Take in a vector of N values and return and return a Nx3 matrix of RGB
0003 % values associated with a given colormap
0004 %
0005 % rgb = AFQ_vals2colormap(vals, [colormap = 'jet'], [crange])
0006 %
0007 % Inputs:
0008 % vals = A vector of values to map to a colormap or a cell array of
0009 % vectors of values
0010 % colormap = A matlab colormap. Examples: colormap = 'autumn';
0011 % colormap = 'jet'; colormap = 'hot';
0012 % crange = The values to map to the minimum and maximum of the colormap.
0013 % Defualts to the full range of values in vals.
0014 %
0015 % Outputs:
0016 % rgb = Nx3 matrix of rgb values mapping each value in vals to the
0017 % corresponding rgb colors. If vals is a cell array then rgb
0018 % will be a cell array of the same length
0019 %
0020 % Example:
0021 % vals = rand(1,100);
0022 % rgb = AFQ_vals2colormap(vals, 'hot');
0023 %
0024 % Copyright Jason D. Yeatman, June 2012
  3 Comments
Image Analyst
Image Analyst on 12 Jan 2019
Edited: Image Analyst on 12 Jan 2019
Maybe not a bad thing considering the bad programming practices they used. For example, calling an input argument colormap when colormap is the name of a built in function. So how can you use the colormap function to get the arrays? I'm almost certain you can't do
cmap = colormap(colormap);
Assuming you just put in the string directly, you can probably do pretty much the same thing by doing this:
grayImage = imread('pout.tif');
subplot(2,2,1);
imshow(grayImage);
fontSize = 14;
title('Gray Scale Image', 'FontSize', fontSize);
subplot(2,2,2);
imhist(grayImage);
grid on;
title('Histogram', 'FontSize', fontSize);
% Define range of gray levels that the colormap should go between.
grayLevelRange = [75, 150]; % Whatever you want.
cmap = jet(256);
% Specify color for gray levels below the specified range.
cmap1 = repmat(cmap(1,:), [grayLevelRange(1), 1]);
% Let the middle part be remapped such that the full colormap
% covers the specified gray level range.
cmap2 = imresize(cmap, [abs(grayLevelRange(2) - grayLevelRange(1) + 1), 3]);
% Specify color for gray levels above the specified range.
cmap3 = repmat(cmap(end, :), [255 - grayLevelRange(2), 1]);
% Combine to form a new colormap.
cmap = [cmap1; cmap2; cmap3];
% Apply the colormap to the gray scale image to form an RGB image.
rgbImage = ind2rgb(grayImage, cmap);
% Display the RGB image.
subplot(2, 2, 3);
imshow(rgbImage);
title('RGB Color-Mapped Image', 'FontSize', fontSize);

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!