How to Separate Connected Objects in an Image

25 views (last 30 days)
I am trying to count the number of objects in a picture using bwconncomp, but the results are incorrect because some of the regions blend together. I used the image from https://www.publicdomainpictures.net/pictures/290000/velka/field-of-flowers-1553807209QZG.jpg to illustrate my problem.
This is the original image, and I'd like to write code that counts the total number of flowers in the picture.
I used the following code:
image = imread(fileName);
%Convert to grayscale
imageGray = rgb2gray(image);
%Remove noise
SE = strel('disk', 5);
afterOpening = imopen(image, SE);
%Convert to binary image and filter out small objects
imageBin = imbinarize(afterOpening);
imageBin = bwareaOpen(imageBin, 300);
%Count the number of objects
CC = bwconncomp(imageBin);
numObjects = CC.NumObjects;
I am pretty sure that bwconncomp will count the three flowers highlighted above as one region, thereby skewing the count in numObjects. Is there a way that I can get Matlab to recognize the highlighted box as having 3 objects instead of 1?

Accepted Answer

Matt J
Matt J on 31 May 2021
Edited: Matt J on 31 May 2021

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!