It's easy - use the imagesc function:
p = imread('peppers.png'); %Read image b = (p(:,:,2)>100); % Thresholding by some constant threshold
If you already have a binary image, just use this section of code: ( b is the image)
L = bwlabel(b); %Find components figure(); %Create figure imagesc(L); %Draw the components, each in its own color.

You can also change colors using the colormap :
colormap(bone)

To customize the colors, define an nx3 matrix and pass it as input to colormap
cm = [1 0 0; 0 1 0; 0 0 1 0 1 1 1 1 0 ]; colormap(cm)
source share