Convert color map and a corresponding color scale to a matrix of numerical values.

20 views (last 30 days)
I have a color map and a corresponding color scale. How do i convert this to a matrix of numerical values?
For example, find the image attached. I have to convert this picture into a matrix of values in accordance to the color scale given.
This should be the opposite of the inbuilt function 'colorbar'.
ex_colormap.png

Answers (2)

Luna
Luna on 28 Nov 2019
Edited: Luna on 28 Nov 2019
What do you mean by opposite of inbuilt function colorbar?
colorbar is a function to create any colorbar near your axis. The colormap you are looking for is already defined as name 'jet'. If you want to make flip just use flip function.
Here is sample code. Read the comments.
figure;
colorbar; % creates default colorbar (with a yellow - green color)
c = jet(64); % 64x3 colormap matrix
colormap(c); % changes the color of the colorbar
% OR
colormap(flip(c)); % flips the colorbar upside down
  2 Comments
Siva Poornan
Siva Poornan on 29 Nov 2019
Thank you for responding back but I think I haven't made the question clear.
The problem for me is, let us say we have the image attached in the question. From the color scale, we know that dark red corresponds to nearly 1 Million while dark blue corresponds to 0 Million and other colors have a value in between these, e.g., green has a value of 0.5 Million. So if the image is converted to matrix with numerical values, I would expect that the first few coloumns would have a low value since they are blue in color. And similarly, I would expect the value to increase in subsequent columns since the color gradually changes to red.
Can you please help me out to get this matrix for any picture with a corresponding color scale?
Kindly note that if I have this matrix, I can use the 'colorbar' function to get back the picture and its scale. So this I why I said I need the inverse of the inbuilt function 'colorbar'.
Thank you.
Luna
Luna on 29 Nov 2019
Edited: Luna on 29 Nov 2019
In the above code c variable is the matrix. You can get higher size of it by changing the input of jet.
For example:
c = jet(1000000) % gives you 1000000x3 Matrix and each row represents R G B values between 0-1.
By the way, @Image Analyst's code convert your actual image into matrix cmap. You can use colormap(cmap) function to change your colorbar's color.

Sign in to comment.


Image Analyst
Image Analyst on 28 Nov 2019
Read each channel of the image and transpose and concatenate
rgbImage = imread('image.png');
subplot(2, 1, 1);
imshow(rgbImage);
impixelinfo; % Show RGB value on figure as you mouse around.
% Please crop image out of screenshot so there is no white padding.
[rows, columns, numberOfColorChannels] = size(rgbImage);
row = round(rows/2); % Take middle row.
redCurve = rgbImage(row, :, 1);
greenCurve = rgbImage(row, :, 2);
blueCurve = rgbImage(row, :, 3);
subplot(2, 1, 2);
plot(redCurve, 'r-', 'LineWidth', 3);
hold on;
plot(greenCurve, 'g-', 'LineWidth', 3);
plot(blueCurve, 'b-', 'LineWidth', 3);
grid on;
title('Color Map', 'FontSize', 15);
xlabel('Input Gray Level', 'FontSize', 15);
ylabel('Output Color Level', 'FontSize', 15);
legend('Red Curve', 'Green Curve', 'Blue Curve', 'location', 'east');
cmap = [redCurve(:), greenCurve(:), blueCurve(:)]
0001 Screenshot.png
  2 Comments
Siva Poornan
Siva Poornan on 29 Nov 2019
Thank you for responding back but I think I haven't made the question clear.
The problem for me is, let us say we have the image attached in the question. From the color scale, we know that dark red corresponds to nearly 1 Million while dark blue corresponds to 0 Million and other colors have a value in between these, e.g., green has a value of 0.5 Million. So if the image is converted to matrix with numerical values, I would expect that the first few coloumns would have a low value since they are blue in color. And similarly, I would expect the value to increase in subsequent column as the color gradually changes to red.
Can you please help me out to get this matrix for a any picture with a color scale?
Thank you.

Sign in to comment.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!