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

57 views (last 30 days)

Show older comments

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 Comments

### Answers (2)

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

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.

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(:)]

##### 2 Comments

Image Analyst
on 29 Nov 2019

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!