I'm doing a uni project where I have to detect the number of structural elements in a picture. My aim is to create a blob for each element so that I can then count them.
So far I did this little bit of coding attached. The problem is that often the objects are overlapping (see bottom left part with 4 attached elements forming a single blob). How can I bypass this problem? I genuinely do not know how to solve this.
% Read the image and convert to L*a*b* color space
I = imread('Crop.jpg');
Ilab = rgb2lab(I);
% Extract a* and b* channels and reshape
ab = double(Ilab(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
% Segmentation usign k-means
nColors = 4;
[cluster_idx, cluster_center] = kmeans(ab,nColors,...
'distance', 'sqEuclidean', ...
'Replicates', 3);
% Show the result
pixel_labels = reshape(cluster_idx,nrows,ncols);
figure(1);
imshow(pixel_labels,[]), title('image labeled by cluster index');
EDIT: I also thought that some kind of object detection algorithm could do the trick but the elements change in orientation too besides overlapping. I saw this video tutorial Object and feature detection, but I can't read the code for the centroid at around 10:58 so I can't try it myself. Do you think this could work? Thanks again.