Removing blobs that don't meet certain properties?

15 views (last 30 days)
Hi.
I'm using blob detection to detect objects in my image (after background removal). I'd like to remove blobs from recognition if they are too close to the edge (640*480 image, i.e. if centroidX is 610, I'd like to discount this blob).
I'm getting blobs as follows:
labeledImage = bwlabel(filteredMatrix, 8);
blobMeasurements = regionprops(labeledImage, filteredMatrix, 'all');
numberOfBlobs = size(blobMeasurements,1);
And centroids as follows:
allBlobCentroids = [blobMeasurements.Centroid];
centroidsX = allBlobCentroids(1:2:end-1);
centroidsY = allBlobCentroids(2:2:end);
How can I remove blobs whose values are less than a certain centroid? The reason I'd like to do this is because as blobs move closer to the edge, part of the image is hidden, so when taking the average RGB, it is normally calculated wrong and classified incorrectly.
Thanks

Answers (2)

Walter Roberson
Walter Roberson on 20 Feb 2016
R = 20;
maxx = size(filteredMatrix,2);
maxy = size(filteredMatrix,1);
mask = centroidsX < R | centroidsY < R | centroidsX > maxx-R | centroidsY > maxy-R;
blobMeasurements(mask) = [];
  2 Comments
Faizan Tahir
Faizan Tahir on 20 Feb 2016
Edited: Faizan Tahir on 20 Feb 2016
Hi. Could you explain exactly what this is doing? I solved my problem using bwareaopen (for some reason I completely forgot about that function when creating this post). What I did was changed
labeledImage = bwlabel(filteredMatrix, 8);
to
labeledImage = bwareaopen(bwlabel(filteredMatrix, 8), 300);
so when the blobs reached the edge, once a portion of it is covered, it would no longer be labelled. Is this an okay solution also?
Walter Roberson
Walter Roberson on 20 Feb 2016
The code I posted checks to see if the centroid is within R of any edge of the matrix and removes it if it is.
I am not familiar enough with bwareaopen() to have any sense of whether your code would work.

Sign in to comment.


Image Analyst
Image Analyst on 21 Feb 2016
I do that in my Image Segmentation Tutorial. http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862 Here is a snippet from it that does the size filtering.
% Now I'll demonstrate how to select certain blobs based using the ismember() function.
% Let's say that we wanted to find only those blobs
% with an intensity between 150 and 220 and an area less than 2000 pixels.
% This would give us the three brightest dimes (the smaller coin type).
allBlobIntensities = [blobMeasurements.MeanIntensity];
allBlobAreas = [blobMeasurements.Area];
% Get a list of the blobs that meet our criteria and we need to keep.
% These will be logical indices - lists of true or false depending on whether the feature meets the criteria or not.
% for example [1, 0, 0, 1, 1, 0, 1, .....]. Elements 1, 4, 5, 7, ... are true, others are false.
allowableIntensityIndexes = (allBlobIntensities > 150) & (allBlobIntensities < 220);
allowableAreaIndexes = allBlobAreas < 2000; % Take the small objects.
% Now let's get actual indexes, rather than logical indexes, of the features that meet the criteria.
% for example [1, 4, 5, 7, .....] to continue using the example from above.
keeperIndexes = find(allowableIntensityIndexes & allowableAreaIndexes);
% Extract only those blobs that meet our criteria, and
% eliminate those blobs that don't meet our criteria.
% Note how we use ismember() to do this. Result will be an image - the same as labeledImage but with only the blobs listed in keeperIndexes in it.
keeperBlobsImage = ismember(labeledImage, keeperIndexes);
% Re-label with only the keeper blobs kept.
labeledDimeImage = bwlabel(keeperBlobsImage, 8); % Label each blob so we can make measurements of it
% Now we're done. We have a labeled image of blobs that meet our specified criteria.
subplot(3, 3, 7);
imshow(labeledDimeImage, []);
axis image;
title('"Keeper" blobs (3 brightest dimes in a re-labeled image)', 'FontSize', captionFontSize);
However since I've posted that there are new functions bwareafilt() and bwpropfilt() that may be easier.

Community Treasure Hunt

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

Start Hunting!