1

I have an image which is a result of k-means segmentation. The code to obtain it it's here:

% Read the image and convert to L*a*b* color space
I = imread('Crop.jpg');
% h = ginput(2);
% Diameter = sqrt((h(2)-h(1))^2+(h(4)-h(3))^2);
% MeanArea = 3.14*(diameter^2)/4;
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');

Resulting in this picture: pixel_labels

Now as you can see, most of the elements are connected, so I want to count all of the blobs (besides the background one), then filter them using MeanArea, area of the elements incircle. If the blob has dimensions < MeanArea I do not count it, while if the blob has dimensions > MeanArea I want to divide its area by MeanArea to obtain the number of elements. All of this is to have a measure such that #blobs = #elements. I know that it has something to do with 'bwlabel' and 'regionprops' but I don't know how to code this since I'm a beginner, any coding help is appreciated. Thanks.

EDIT: using the 'trees' approach linked in the comments I got very bad results, so I don't think it's the right method. I don't have objects with same color as the tree example, I just have same shape.

Trees approach

I'm following this other approach. Color segmentation by k-means I obtained the labeled image above, but how can I save it into a variable so that I can erode it and count the number of blobs? That's my question.

EDIT2: The original picture is this one. I'm trying to detect the number of red green and blue objects. Original image

Jack
  • 41
  • 6
  • Try eroding that image – Ander Biguri Nov 02 '17 at 17:24
  • Your other question has been closed as duplicate of [this one](https://stackoverflow.com/questions/42374463/matlab-segmentation-to-separate-touching-objects-in-an-image) where they show how to do it. Why is this one not a duplicate? – Ander Biguri Nov 02 '17 at 17:28
  • @AnderBiguri I don't know but it's not really the same problem. Of course I tried to run that code too but it's giving me very poor results. How can I save the image of my last code line to a variable so then I can try to erode it? – Jack Nov 02 '17 at 20:45
  • You need to explain us why that code doesn't work, or else we'll close it as duplicate – Ander Biguri Nov 02 '17 at 22:29
  • @AnderBiguri I edited the post showing why the example you told me to follow it's not working. It may have to do with the fact that I have 3 different colors, not just green like the trees image. – Jack Nov 03 '17 at 09:26
  • 1
    What are you actually trying to count? You appear to have black, white, light grey and dark grey areas? Can you provide a second image where you have marked, by hand maybe, the blobs you seek in red or something please? – Mark Setchell Nov 03 '17 at 09:31
  • @MarkSetchell yes now I uploaded the original image in EDIT2. I'm trying to count red green and blue – Jack Nov 03 '17 at 10:04
  • Can you make life easier for yourself by improving the lighting - i.e. making it more uniform and more diffuse - maybe put greaseproof paper between light and objects, or add sheets of white paper or reflectors all around? – Mark Setchell Nov 03 '17 at 10:15
  • @MarkSetchell The physical model has been dismissed, but I'll do that if there's no other way to get proper results. Do you think that will improve it? If anyone has some insight please share because I'm stuck anyways. Thanks. – Jack Nov 03 '17 at 11:50
  • I took a pic of an old physical model made of just WHITE structural elements and it's 100 times easier. I might as well rebuild the whole model using only white elements and see how it goes for the whole structure. – Jack Nov 03 '17 at 14:12

0 Answers0