MATLAB Answers

i want to keep just the object which there is circle inside it and remove other objects from image

3 views (last 30 days)
alaa shamasneh
alaa shamasneh on 19 Feb 2018
Edited: alaa shamasneh on 19 Feb 2018
I need help on the following code it's working but I don't know how to remove the object which don't have a black circle inside it can any one help (this code for keep the roundness object which threshold>0.65 and remove others so the roundness object have a circle inside as image attachment)please any one help update on the same code thanks bw = imread('segment.jpg'); imshow(bw);
bw = rgb2gray(bw); %bw = imbinarize(bw); %imshow(bw)
% remove all object containing fewer than 30 pixels
bw = bwareaopen(bw,30);
% fill a gap in the pen's cap
se = strel('disk',4);
bw = imclose(bw,se);
% fill any holes, so that regionprops can be used to estimate % the area enclosed by each of the boundaries bw = imfill(bw,'holes');
%imshow(bw)
[B,L] = bwboundaries(bw,'noholes');
% Display the label matrix and draw each boundary imshow(label2rgb(L, @jet, [.5 .5 .5])) hold on for k = 1:length(B) boundary = B{k}; plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 1) end
stats = regionprops(L,'Area','Centroid');
threshold = 0.65;
% loop over the boundaries for k = 1:length(B)
% obtain (X,Y) boundary coordinates corresponding to label 'k'
boundary = B{k};
% compute a simple estimate of the object's perimeter
delta_sq = diff(boundary).^2;
perimeter = sum(sqrt(sum(delta_sq,2)));
% obtain the area calculation corresponding to label 'k'
area = stats(k).Area;
% compute the roundness metric
metric = 4*pi*area/perimeter^2;
% display the results
metric_string = sprintf('%2.2f',metric);
% mark objects above the threshold with a black circle
if metric > threshold
centroid = stats(k).Centroid;
plot(centroid(1),centroid(2),'ko');
end
text(boundary(1,2)-35,boundary(1,1)+13,metric_string,'Color','y',...
'FontSize',14,'FontWeight','bold');
end
title(['Metrics closer to 1 indicate that ',... 'the object is approximately round']);

Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by