How can I separate connected objects from the color segmented RGB image ?

4 views (last 30 days)
I have a image like "testImage" . Applying some sort of threshold, like the code I could separate the blue objects but now i am having problem for separating these connected blue objects. I applied watershed(i don't know i made it right/wrong) but didn't work out. so, i need help to separate these objects.
testImage
resultImage
my code is shown below :
RGB=imread('testImage.jpg');
RGB = im2double(RGB);
cform = makecform('srgb2lab', 'AdaptedWhitePoint', whitepoint('D65'));
I = applycform(RGB,cform);
channel1Min = 12.099;
channel1Max = 36.044;
channel2Min = -9.048;
channel2Max = 48.547;
channel3Min = -53.996;
channel3Max = 15.471;
BW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
maskedRGBImage = RGB;
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
figure
imshow(maskedRGBImage)

Answers (1)

Marco Marcon
Marco Marcon on 22 Aug 2015
Edited: Marco Marcon on 23 Aug 2015
You can use the bwlabel function: L = bwlabel(BW);
in this case a progressive integer number will be assigned to every separate object In 'L' every pixel belonging to the same object will have the same value (the integer value assigned to that pixels' cluster).
Otherwise you can use regionprops function:
s = regionprops(BW,'centroid');
's' will be a structure array containing the baricentra of every separate object in the image. All the baricentra can be converted in a single matrix using e.g.
baricentra = cat(1, s.Centroid);
now every row of baricentra matrix contains the coordinates of the centroid of a separate object in the image.
Concerning overlapping or joined objects, a preliminary morphological opening can be applied in order to remove small false positive regions due to RGB segmentation errors and to smooth objects borders. Then
areas = regionprops(BW,'Area');
can be used to get the area of each connected region: dividing every area value by the average objects area (assuming that all the analysed objects have a similar area) will provide an estimation of the number of objects in every connected region. If the color segmentation presents also false negatives (object pixels assigned to background) morphological closing can be applied to fill holes; regionprops with 'FilledImage' property can be also used to fill holes in connected regions.
Then, in order to isolate different objects in connected regions, morphological erosion can be adopted with a sufficiently large structuring element that concurrently must contained in every single possible object, (e.g. a large circle that can be contained in every possible analysed object). This operation, if the object overlap is not too high, should be able to keep only a small isolated cluster of pixels close to the baricentrum of every object. Reapplying the bwlabel function should then be able to localize the centroid of different objects.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!