How to crop object in image automatically?
조회 수: 8(최근 30일)
DGM 2022년 11월 10일
It might be better to work on getting a better segmentation, but here's one way you might approach the cleanup.
% this is a logical image of a screenshot
% this won't be the same size as the original
% save images using imwrite, not by saving the figure
inpict = imread('pillsc.png');
% get rid of small speckles
inpict = bwareaopen(inpict,200); % pick some minimum blob area
% get object properties
S = regionprops(inpict,'circularity','area','image');
% find things that are roughly circular
iscircular = find([S.Circularity] > 0.9); % pick a limit
circareas = [S(iscircular).Area]; % areas of the circular objects
% if you want to pick only the largest circular object
[bigcircarea bigcircidx] = max(circareas);
bigcircidx = iscircular(bigcircidx);
% if you want a close-cropped copy of the object
bigcircpict = S(bigcircidx).Image;
% show the object and its area
Image Analyst 2022년 11월 10일
I guess you figured it out since you accepted the answer but I'll offer a different way. Simply call imclearborder followed by bwareafilt
binaryImage = imread('pillsc.png');
subplot(2, 1, 1);
% Get rid of blobs touching border.
binaryImage = imclearborder(binaryImage);
% Take the largest of the blobs that are left.
binaryImage = bwareafilt(binaryImage, 1);
subplot(2, 1, 2);