how to convert a label matrix to colour matrix for image segmentation?

  • Last Update :
  • Techknowledgy :

Convert the label matrix into an RGB image, specifying optional parameters. This example uses the 'spring' colormap, sets background pixels to the color cyan, and randomizes how colors are assigned to the labels.,Convert the label matrix into RGB image, using default settings.,RGB = label2rgb(L) converts a label image, L into an RGB color image for the purpose of visualizing the labeled regions. The label2rgb function determines the color to assign to each object based on the number of objects in the label matrix. The label2rgb function picks colors from the entire range of the colormap.,Colormap matrix specifying c colors, each as an RGB triple. c must be greater than or equal to the number of labels, numlabels, in label matrix L. You can compute the number of labels as numlabels = max(L(:)).

I = imread('rice.png');
imshow(I)
BW = imbinarize(I);
CC = bwconncomp(BW);
L = labelmatrix(CC);
RGB = label2rgb(L);
figure
imshow(RGB)
RGB2 = label2rgb(L, 'spring', 'c', 'shuffle');
figure
imshow(RGB2)

Suggestion : 2

You are looking into indexed RGB images - an RGB image where you have a fixed "pallet" of colors, each pixel indexes to one of the colors of the pallet. See this page for more information.

from PIL
import Image

img = Image.fromarray(x, mode = "P")
img.putpalette([
   255, 255, 255, # index 0
   144, 0, 0, # index 1
   0, 255, 0, # index 2
   0, 0, 255, # index 3
   #...and so on, you can take it from here.
])
img.show()

Suggestion : 3

Convert the label matrix into an RGB image, specifying optional parameters. This example uses the 'spring' colormap, sets background pixels to the color cyan, and randomizes how colors are assigned to the labels.,Convert the label matrix into RGB image, using default settings.,If you set the background color zerocolor to the same color as one of the regions, label2rgb will not issue a warning.,Label matrix of contiguous regions, specified as a 3-element vector representing an RGB triple, or one of the following color abbreviations.

I = imread('rice.png');
imshow(I)
BW = imbinarize(I);
CC = bwconncomp(BW);
L = labelmatrix(CC);
RGB = label2rgb(L);
figure
imshow(RGB)
RGB2 = label2rgb(L, 'spring', 'c', 'shuffle');
figure
imshow(RGB2)