MATLAB: In labelled binary image, fill the blobs with different colors defined by me.

digital image processingImage Processing Toolboximage segmentation

Hello,
I have this labelled binary image with multiple blobs. I want to fill the labelled blobs with colors defined by me. For example: as shown in image, blobs of label 2,4 and 8, I want them to be filled with green color. Similarly, blobs of label 5,6 and 11, I want them to be filled with red color. Rest blobs should be filled with blue color. I know about label2rgb command but dont know how to do this color filling with label2rgb.
How to do this?

Best Answer

  • use bwselect to select specific blob. Assign pixels value: red - 1, green - 2, blue - 3
  • create your own colormap and use it to display colors
I1 = bwselect(I,coord_red); % choose pixels in red group
I2 = bwselect(I,coord_green); % choose pixels in green group
I3 = bwselect(I,coord_blue); % choose pixels in blue group
II = I1*1 + I2*2 + I3*3; % create new image
% create new colormap
cmap = [0 0 0 % black
1 0 0 % red
0 1 0 % green
0 0 1]; % blue
ishow(II,cmap) % display new image with new colors