Unable to create custom colormap from RGB data?

조회 수: 13(최근 30일)
Roger Breton
Roger Breton 2022년 1월 18일
댓글: Roger Breton 2022년 1월 18일
Still bogged down in the difference between an array and a matrix...
Everything was working well until I stumbled upon the following error message, at the time of attempting to create the colormap :
Colormap must be an Mx3 array of type single or double with values in the range [0,1] or an Mx3
array of type uint8.
Here's my code :
% Generate three vectors
Axe_A_B = transpose(linspace(-128, 128, 21));
Axe_La = transpose(linspace(39, 50, 21));
Axe_Lb = transpose(linspace(15,100, 21));
Lab_Axe_AB = [Axe_La, Axe_A_B, Axe_A_B]; % 21 x 3 double
RGB_Axe_AB = lab2rgb(Lab_Axe_AB)
% negatives values?
[row,col] = size(RGB_Axe_AB);
for ROW = 1:row
if RGB_Axe_AB(ROW,1) < 0
RGB_Axe_AB(ROW,1) = 0;
end
if RGB_Axe_AB(ROW,2) < 0
RGB_Axe_AB(ROW,2) = 0;
end
if RGB_Axe_AB(ROW,3) < 0
RGB_Axe_AB(ROW,3) = 0;
end
end
RGB_Axe_AB_ColorMap = colormap(RGB_Axe_AB)
I'm going in circle...
Documentation states :
% Colormap must have 3 columns: [R,G,B]
% defining a three-column matrix of values
Here's the content of my RGB_Axe_AB 'variable' :
RGB_Axe_AB =
0 0.5400 1.2184
0 0.5321 1.1330
0 0.5240 1.0486
0 0.5152 0.9653
0 0.5058 0.8831
0 0.4952 0.8020
0 0.4833 0.7220
0 0.4695 0.6432
0 0.4536 0.5654
0.2388 0.4349 0.4886
0.4126 0.4126 0.4126
0.5271 0.3857 0.3369
0.6198 0.3526 0.2607
0.7011 0.3106 0.1812
0.7756 0.2540 0.0886
0.8459 0.1660 0
0.9139 0 0
0.9810 0 0
1.0478 0 0
1.1142 0 0
1.1804 0 0
I did not bother to create a 256 size variable at this point. ...

채택된 답변

Roger Breton
Roger Breton 2022년 1월 18일
편집: Walter Roberson 2022년 1월 18일
Oopsy, doopsy! I just realize there are values > 1.0 in those columns! Oh boy! Egg on my face...
Here's the revised code :
for ROW = 1:row
if RGB_Axe_AB(ROW,1) < 0
RGB_Axe_AB(ROW,1) = 0;
end
if RGB_Axe_AB(ROW,1) > 1
RGB_Axe_AB(ROW,1) = 1;
end
if RGB_Axe_AB(ROW,2) < 0
RGB_Axe_AB(ROW,2) = 0;
end
if RGB_Axe_AB(ROW,2) > 1
RGB_Axe_AB(ROW,2) = 1;
end
if RGB_Axe_AB(ROW,3) < 0
RGB_Axe_AB(ROW,3) = 0;
end
if RGB_Axe_AB(ROW,3) > 1
RGB_Axe_AB(ROW,3) = 1;
end
end
Naturally, everything works now. No more errors....
Is there a way to do this process more efficiently? Without resorting to a loop?
Thank you in advance for your help and patience :-)
  댓글 수: 6
Roger Breton
Roger Breton 2022년 1월 18일
Rome was not built in one day... I spotted an error in the appearance of yellow? This is the corrected code :
% Generate three vectors
Axe_A_B = transpose(linspace(-128, 128, 21));
Axe_B_A = transpose(linspace(-128, 128, 21));
Axe_La = transpose(linspace(39, 50, 21)); % Green to Magenta
Axe_Lb_Zero = transpose(linspace(0, 0, 21));
Axe_Lb = transpose(linspace(15,100, 21)); % Blue to Yellow
Lab_Axe_AB = [Axe_La, Axe_A_B, Axe_Lb_Zero]; % 21 x 3 double
RGB_Axe_AB = lab2rgb(Lab_Axe_AB) % Green to Magenta
Lab_Axe_BA = [Axe_Lb, Axe_Lb_Zero, Axe_B_A]; % 21 x 3 double
Flipped = flipud(Lab_Axe_BA);
% RGB_Axe_BA = lab2rgb(Lab_Axe_BA) % Yellow to Blue
RGB_Axe_BA = lab2rgb(Flipped) % Yellow to Blue
Had to flip the array upside down, to get the correct Lab (RGB) values to appear at the correct (b* = +128) end of the scale. Silly me. Now this is better :

댓글을 달려면 로그인하십시오.

추가 답변(0개)

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by