Convert an array of numbers into a color mapped image

23 views (last 30 days)
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 Comments
Steve Francis
Steve Francis on 19 Jan 2023
Thanks for this, DGM, and for your comment to Walter's answer. Superb stuff.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 19 Jan 2023
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 Comments
Steve Francis
Steve Francis on 19 Jan 2023
Thank you @Walter Roberson for taking the time to answer this question. Much appreciated.

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by