Convert an array of numbers into a color mapped image

I have a 2-d array of integers. It has 1024 rows and 1280 columns. The integers are whole numbers in the range 1-12.
I want to define a rgb colour for each of the twelve values (e.g. "1" is equivalent to [1 1 0], "2" is equivalent to [0 1 1] ... "12" is equivalent to [0.2 0.7 0.6]).
I then want to convert the numerical array into a rgb TIF image with 1024x1280 pixels that shows the appropriate colour corresponding to the array value in the pixel position. For example, if the value of the array element at row 1 column 1 is equal to 2, then I would expect to see a cyan [0 1 1] pixel in the top left hand corner of my rgb image.
How can I do this?

3 commentaires

I've had a go and come up with the following simplified example:
Array1 =[0 0 0 0 0 0; 0 1 0 2 0 6; 4 0 5 5 0 9; 0 10 7 0 8 0];
Array2 =[0 0 0 0 0 0; 0 1 0 2 0 3; 4 0 5 5 0 6; 0 7 0 8 9 0];
mymap = [0 0 0; 0.6 1 0.2;1 1 0;0.6 0.2 1; 1 0.6 0.2;1 0 1;0.2 1 0.6;0 1 0;1 0 0;0.2 0.6 1;0 0 1];
colormap(mymap);
imagesc (Array1);
imagesc (Array2);
At the moment, the value of 7 in Array1 is appearing as green and the value of 7 in Array 2 is appearing as red. I want 7 to always be plotted as green! Same applies to the other colors. I want them to be consistent with the corresponding value.
DGM
DGM le 19 Jan 2023
Modifié(e) : DGM le 20 Jan 2023
The reason this happens has to do with how the CData gets mapped to the current colormap.
Array1 =[0 0 0 0 0 0; 0 1 0 2 0 6; 4 0 5 5 0 9; 0 10 7 0 8 0];
Array2 =[0 0 0 0 0 0; 0 1 0 2 0 3; 4 0 5 5 0 6; 0 7 0 8 9 0];
mymap = [0 0 0; 0.6 1 0.2;1 1 0;0.6 1 0.2; 1 0.6 0.2;1 0 1;0.2 1 0.6;0 1 0;1 0 0;0.2 0.6 1;0 0 1];
subplot(2,1,1)
hi1 = imagesc(Array1);
colormap(mymap)
caxis
ans = 1×2
0 10
subplot(2,1,2)
hi2 = imagesc(Array2);
colormap(mymap)
caxis
ans = 1×2
0 9
When you use imagesc(), you're scaling everything such that the range of values in the image maps to the extent of the color table. When range of your image varies, so does the mapping.
You can either:
Use imagesc() (scaled mapping) and explicitly specify caxis so that the scale stays consistent. There really isn't a good reason to do this for an indexed image, but I guess you could.
hi1 = imagesc(Array1);
colormap(mymap)
caxis([0 length(mymap)-1])
Use image() (direct mapping) and present the image data as expected for its class
%hi1 = image(Array1+1); % either offset by 1
hi1 = image(uint8(Array1)); % or use an integer class
colormap(mymap)
Or you can use imshow() with the syntax for direct mapping. The same convention described for image() holds here as well. Note that this sets the colormap, so using colormap() isn't necessary.
%hi1 = imshow(Array1+1,mymap); % either offset by 1
hi1 = imshow(uint8(Array1),mymap); % or use an integer class
All of this is really moot if your goal is to write the image instead of just displaying it. Use ind2rgb() as Walter shows, and pay attention to the fact that indexing is class-dependent. Write the images with imwrite().
imwrite(B1,'myimage.tif') % write as RGB
Though note that TIFF does support indexed images.
imwrite(uint8(Array1),mymap,'myimage.tif') % write as indexed
Thanks for this, DGM, and for your comment to Walter's answer. Superb stuff.

Connectez-vous pour commenter.

 Réponse acceptée

Array1 =[0 0 0 0 0 0; 0 1 0 2 0 6; 4 0 5 5 0 9; 0 10 7 0 8 0];
Array2 =[0 0 0 0 0 0; 0 1 0 2 0 3; 4 0 5 5 0 6; 0 7 0 8 9 0];
mymap = [0 0 0; 0.6 1 0.2;1 1 0;0.6 1 0.2; 1 0.6 0.2;1 0 1;0.2 1 0.6;0 1 0;1 0 0;0.2 0.6 1;0 0 1];
B1 = ind2rgb(Array1, mymap);
B2 = ind2rgb(Array2, mymap);
image(B1)
image(B2)

3 commentaires

Since Array1, Array2 are floating point, their indexing is presumed to start at 1. Note the second element of row 2 is truncated, since both 0 and 1 map to the first row in the CT.
So to correct, either offset the indices by 1
B1 = ind2rgb(Array1+1, mymap);
B2 = ind2rgb(Array2+1, mymap);
Or change the class of the arrays. Integer-class indexed images start at 0.
B1 = ind2rgb(uint8(Array1), mymap);
B2 = ind2rgb(uint8(Array2), mymap);
Awesome comments, DGM. Thank you!
Testing this, it it exactly what I need:
Array1 =[0 0 0 0 0 0; 0 1 0 2 0 6; 4 0 5 5 0 9; 0 10 7 0 8 0];
Array2 =[0 0 0 0 0 0; 0 1 0 2 0 3; 4 0 5 5 0 6; 0 7 0 8 9 0];
mymap = [0 0 0; 0.6 1 0.2;1 1 0;0.6 1 0.2; 1 0.6 0.2;1 0 1;0.2 1 0.6;0 1 0;1 0 0;0.2 0.6 1;0 0 1];
B1 = ind2rgb(Array1+1, mymap);
B2 = ind2rgb(Array2+1, mymap);
image(B1)
image(B2)
Thanks so much for your time! I was struggling with trying to infer how imagesc worked.
Thank you @Walter Roberson for taking the time to answer this question. Much appreciated.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Version

R2022a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by