MATLAB: How can i detect round objects and remove other objects in an image using matlab

Image Processing Toolboxshape filtering

IMAGES{i} = imread(sprintf(Fimage,i));
result = cell(1,N);
result{i} = rgb2gray(IMAGES{i});
BW = im2bw(result{i}, .4);
se = strel('disk',11);
erodedBW = imdilate(BW,se);
imagesc(erodedBW)
bw=bwareaopen(erodedBW,50000);
%imshow(bw)
%hold
[B,L] = bwboundaries(bw,'noholes');
for k = 1:length(B)
boundary = B{k};
xr(i,k)=round(mean((boundary(:,2))));
yr(i,k)=round(mean((boundary(:,1))));
imgindex(i)=i;
end
st=regionprops( ~bw,'area','centroid','PixelIdxList');

Best Answer

It seemed to work for me:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
rgbImage = imread('005.png');
grayImage = rgb2gray(rgbImage);
subplot(2,2,1);
imshow(grayImage);
title('Original Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Threshold the image.
binaryImage = im2bw(grayImage, .4);
% Display the image.
subplot(2,2,2);
imshow(binaryImage)
title('Initial Binary Image', 'FontSize', fontSize);
% Dilate the image to enlarge the small blobs.
se = strel('disk',11);
binaryImage = imdilate(binaryImage,se);
subplot(2,2,3);
imshow(binaryImage)
title('Dilated Image', 'FontSize', fontSize);
% Label the blobs.

labeledImage = bwlabel(binaryImage);
measurements = regionprops(labeledImage,'Area','Perimeter');
% Do size filtering and roundness filtering.
% Get areas and perimeters of all the regions into single arrays.

allAreas = [measurements.Area]
allPerimeters = [measurements.Perimeter]
% Compute circularities.
circularities = allPerimeters.^2 ./ (4*pi*allAreas)
% Find objects that have "round" values of circularities.
maxAllowableArea = 50000;
keeperBlobs = circularities < 3 & allAreas < maxAllowableArea; % Whatever values you want.
% Get actual index numbers instead of a logical vector
% so we can use ismember to extract those blob numbers.
roundObjects = find(keeperBlobs);
% Compute new binary image with only the small, round objects in it.
binaryImage = ismember(labeledImage, roundObjects) > 0;
subplot(2,2,4);
imshow(binaryImage);
title('Final Image', 'FontSize', fontSize);
% Remeasure with this new segmentation.
% Label the blobs.
labeledImage = bwlabel(binaryImage);
measurements = regionprops(labeledImage,'Area','Perimeter', 'Centroid');
% Get areas and perimeters of all the regions into single arrays.
allAreas = [measurements.Area]
allPerimeters = [measurements.Perimeter]
allCentroids = [measurements.Centroid]
centroidX = allCentroids(1:2:end);
centroidY = allCentroids(2:2:end);
% Plot circles around them
hold on;
for k = 1 : length(centroidX);
plot(centroidX(k), centroidY(k), ...
'ro', 'MarkerSize', 20, 'LineWidth', 2);
end
What does "not working well" mean to you? What exactly do you want as an output of this routine? The size, location, what?????