2

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');

Crop.jpg:
Crop.jpg

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.

Shai
  • 111,146
  • 38
  • 238
  • 371
Jack
  • 41
  • 6
  • since all your objects have roughly the same size (i.e., appearing in same scale in the image) you can get a rough estimate of segment size (in pixels) and then for too large segments, estimate the number of objects by dividing the blob size with the average object size. – Shai Nov 02 '17 at 12:48
  • Yes that could be a way. I was thinking about creating a segment after the pixel_labels picture pops out, something like: h = ginput(2); Dmax = sqrt((h(2)-h(1))^2+(h(4)-h(3))^2); meanBlobArea = 3.14*(Dmax^2)/4; but then how (coding) can I access to the blobs areas values and create a loop where I use the meanBlobArea to exclude blobs with dimensions bigger and smaller than that, then count those that make the cut? Thanks – Jack Nov 02 '17 at 13:40
  • (1) look at `regionprops` command and `bwlabel` to get sizes of blobs. (2) you might want to look at `median` area rather than `mean`: it is more robust to outliers. – Shai Nov 02 '17 at 13:47
  • Yes I have a similar code by SimpleColorDetectionByHue() that uses both regionprops and bwlabel, but I'm not able to adjust it to my problem. I don't have the skills. I can't post code too because the comment will be too long. – Jack Nov 02 '17 at 15:51
  • you may edit your question or ask a new one if it is not overlap too.much with this one. – Shai Nov 02 '17 at 15:53

0 Answers0