MATLAB: How to to make a car park shows different colors for each dots using matlab

car parkcolor dotsImage Processing Toolbox

I convert my car park image to binary image and clear the unwanted white dots/region to get this image:
This is my original image with 10 green dots:
This is my codes:
sceneImage = imread('nocars10green.jpg');
figure;
imshow(sceneImage);
hsvscene = rgb2hsv (sceneImage);
figure;
imshow (hsvscene);
grayscene = rgb2gray (hsvscene);
figure;
imshow (grayscene);
bwScene = im2bw (grayscene);
figure;
imshow (bwScene);
str = strel('disk',4)
bw = imerode(bwScene,str)
figure;
imshow (bw);
How do I convert the binary image after erode so that I can show different colors for different dots?
I read in this journal.
Al-Kharusi, Hilal, and Ibrahim Al-Bahadly. "Intelligent parking management system based on image processing." World Journal of Engineering and Technology 2014 (2014).
it is mentioned:
if `(newmatrix(y,x) > 0)` % an object is there, if `(e(newmatrix(y,x)) = 0)` this object has not been seen `(newmatrix(y,x)) = x;` make the value and index 3 equal to the current `X` coordinate.
and this is their output image:
which they convert from this image:
But I don't understand how it works. Can anyone explain to me how it work and how to write the commands to convert my binary image to get the same as their output image in order to get different colors of each dots?
or if there is any other way to convert it?

Best Answer

You can label your binary image and then convert it to colored image like this
labeledImage = bwlabel(binaryImage);
coloredDots = label2rgb(labeledImage, 'hsv');
Then extract the color channels of your rgb image:
% Extract the individual red, green, and blue color channels.

redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Extract the individual red, green, and blue color channels.
redChannelDots = coloredDots (:, :, 1);
greenChannelDots = coloredDots (:, :, 2);
blueChannelDots = coloredDots (:, :, 3);
Then assign the colors in the dots mask
redChannel(binaryImage) = redChannelDots(binaryImage);
greenChannel(binaryImage) = rgreenChannelDots(binaryImage);
blueChannel(binaryImage) = blueChannelDots(binaryImage);
Then reassemble the new RGB image from the channels.
% Recombine separate color channels into a single, true color RGB image.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);