Unable to create custom colormap from RGB data?

15 visualizaciones (últimos 30 días)
Roger Breton
Roger Breton el 18 de En. de 2022
Comentada: Roger Breton el 18 de En. de 2022
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. ...

Respuesta aceptada

Roger Breton
Roger Breton el 18 de En. de 2022
Editada: Walter Roberson el 18 de En. de 2022
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 comentarios
Roger Breton
Roger Breton el 18 de En. de 2022
"One more thing" as Steve Jobs used to say... My axes were "reversed"? So, I thought I was going to crawl under untangling the correct logic but I only changed a few lines and here is the correct result :
I had to change these lines of 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_La, Axe_Lb_Zero, Axe_B_A]; % 21 x 3 double
RGB_Axe_BA = lab2rgb(Lab_Axe_BA) % Yellow to Blue
Still many, many, many details to work on but, I'm so please with this result, so far.
Roger Breton
Roger Breton el 18 de En. de 2022
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 :

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Just for fun en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by