How to crop out a detected object from image and store it ?
5 views (last 30 days)
Show older comments
The code bellow classifies objects based on their roundness using bwboundarie. It estimates each object's area and perimeter and uses these results to form a simple metric indicating the roundness of an object:( metric = 4*pi*area/perimeter^2).
This metric is equal to 1 only for a circle and it is less than one for any other shape. But with this code, I'm using a threshold of 0.80 so that only object with a metric value greater than 0.80 will be classified as round.
My question is: when a given object classified as a round, how can I crop it out from the original image 'img' (not 'I' nor 'bw') and save it as a new image?
I think using label matrix and boundary matrix would be enough to do so, but still don't know how to manipulate them.
Thank you for your help.
img=imread('cap.png');
I = rgb2gray(img);
% Step 2: Threshold the Image
bw1 = imbinarize(I);
bw = imcomplement(bw1);
% Step 3: Remove the Noise
bw = bwareaopen(bw,30); % remove small objects
bw = imfill(bw,'holes');
% Step 4: Find the Boundaries
[B,L] = bwboundaries(bw,'noholes');
imshow(label2rgb(L,@jet,[.5 .5 .5]))
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2),boundary(:,1),'w','LineWidth',2)
end
% Step 5: Determine which Objects are Round
stats = regionprops(L,'Area','Centroid');
threshold = 0.80;
% 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);
% Test if the current object classified as a round
if metric > threshold
% HERE, I want to crop the current object from the 'img'
% and save it as a new image
end
end
title(['Metrics closer to 1 indicate that ',...
'the object is approximately round'])
0 Comments
Answers (1)
Mohammed Sadiq
on 15 Jun 2021
Edited: Mohammed Sadiq
on 15 Jun 2021
The rectangle boundary values for cropping an object from image can be extracted from the list of boundary points using min() and max() functions. boundary(:,2) represents the X coordinates and boundary(:,1) represents the Y coordinates for the contour of object. For more information, refer to bwboundaries documentation.
Using the following code will save the image after cropping on the round object.
if metric > threshold
% Calculate x limits
left = min(boundary(:, 2));
right = max(boundary(:,2));
% Calculate y limits
up = min(boundary(:, 1));
down = max(boundary(:,1));
% Saving cropped image
imwrite(img(up:down, left:right, :),['img_round', int2str(k), '.png']);
end
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!