Question

I have one segmented image that includes 5 classes. Each class is indicated by number from 1 to 5. I want to show it with different color for each class. How to implement it by matlab code![Please see my example here? Thank you so much.

Was it helpful?

Solution

Assuming your image is an integer MxN matrix, where each pixel has a value corresponding to the class number (between 1 and C, where C = number of classes), and you have the Image Processing Toolbox, you could use the label2rgb function.

Example:

imgColor = label2rgb(img);
imshow(imgColor);

Alternatively you could create a colormap and generate the image using this:

map = colormap('lines');
imgColor = reshape(map(img, :), [size(img) 3]);
imshow(imgColor);

You could define your own colormap by just creating a matrix Cx3, each row corresponding to a normalized RGB color (between 0.0 and 1.0), for each class.

map = [1.0 0.0 0.0; % Red
       0.0 1.0 0.0; % Green
       ...          % And so on...
      ]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top