0

I have an image which i try to count the number of object -mainly filled circles- so to kill the overlapping between the circle i end up with a labeled image which works pretty well so far, no i apply
bwconncomp(theLabeledImage,4); in way to get the number of filled circles , but what i get looks like this

  Connectivity: 4
  ImageSize: [505 394 3]
  NumObjects: 87
  PixelIdxList: {1x87 cell}

which happened to be wrong the given output is 87, while i have 48 in the image.

Image:

enter image description here

So any suggestion :) ?.

4
  • You can post a link to the image and someone will upload it for you. It is very likely that you need to apply some threshold based on the size of the objects to get the correct count. An image will help us help you :) Commented Jan 9, 2015 at 19:49
  • This is a link to the image drive.google.com/file/d/0B-U9dn2Kg61gVWU5SkdXWnVxS1U/… Commented Jan 9, 2015 at 19:57
  • Do you have the original image, not the segmented? Also, your image has an unnecessary white border surrounding the objects. My guess is that this image was in a figure, and you chose Save As to save this figure to file. Also, with regards to the objects, your image has a bunch of quantization error, and could appear as isolated dots in the image. If you did connected components analysis, these dots would count as individual regions. If you have the original image as well, I'd be more inclined to help you. Commented Jan 9, 2015 at 20:34
  • this is the link to the original image ,,, i first convert it to a binary image and then use the watershed to remove the overlapping drive.google.com/file/d/0B-U9dn2Kg61gVVU4TEprWFVaMEU/… Commented Jan 9, 2015 at 21:05

1 Answer 1

1

A couple of things were tripping you up here.

First, I think you were running bwconncomp on the labeled image, not the binary image. This will double-count a lot of the regions because you'll end up counting the region and its border. See below in my code where I do this (labelImageBWCC = bwconncomp(rgb,4);) and end up with a count of 89 regions.

Second thing is that the watershed transform will sometimes not cleanly break at a junction and instead will end up generating a bunch of little regions right on the border. This is the 'plateau problem' with watershed transforms, but thankfully we can avoid the ramifications of this issue by simply filtering out those little regions with an area threshold.

With those things corrected you can get the right number of coins. Code here:

img = imread('coins.tif');

% Threshold and binarize image and fill holes 
binImg = ~im2bw(img, graythresh(img));
binImg = imfill(binImg, 'holes');

% Distance transform and watershed segmentation
D = bwdist(~binImg);
D = -D;
D(~binImg) = -Inf;
L = watershed(D);
% Generate label image
rgb = label2rgb(L,'jet',[.5 .5 .5]);
% Show results of watershed 
figure, imshow(rgb,'InitialMagnification','fit')
title('Watershed transform of coins.tif')

% Count number of label regions.  Note - this is incorrect!  This will
% count borders and background, which we don't want
labelImageBWCC = bwconncomp(rgb,4);

% Generate new binary image that only looks at individual regions generated
% by watershed segmentation so we are counting watershed regions, not label
% colors
binWatershed = L > 1; % 1 is background region; any region with index > 1 is coin
minCoinSize = 50; % minimum size in pixels
regs = regionprops(binWatershed, 'Area', 'Centroid', 'PixelIdxList');
% Remove all regions with size below threshold
regs(vertcat(regs.Area) < minCoinSize) = [];


% Display image with coin count labeled
figure, imshow(img)
hold on
for k = 1:numel(regs)

    text(regs(k).Centroid(1), regs(k).Centroid(2), num2str(k), ...
        'Color', 'r', 'HorizontalAlignment', 'center')

end
hold off
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.