Convert color map and a corresponding color scale to a matrix of numerical values.
23 次查看(过去 30 天)
显示 更早的评论
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'.
0 个评论
回答(2 个)
Luna
2019-11-28
编辑:Luna
2019-11-28
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 个评论
Siva Poornan
2019-11-29
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
2019-11-29
编辑:Luna
2019-11-29
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.
Image Analyst
2019-11-28
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(:)]
2 个评论
Siva Poornan
2019-11-29
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.
Image Analyst
2019-11-29
I solve that kind of problem with the attached demo where I get the temperature from an image using the color bar.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Orange 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!