How to crop object in image automatically?

조회 수: 8(최근 30일)
Warisa Theerathamtada
Warisa Theerathamtada 2022년 11월 10일
댓글: DGM 2022년 11월 10일
I want to count pixel (binary =1) in object (in this case It is a pill) but this picture have noise so I think I should to crop the image to count How can I automatically crop the circle. or new suggestion how to count pixel

채택된 답변

DGM
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
imshow(bigcircpict)
bigcircarea
bigcircarea = 1968
  댓글 수: 3
DGM
DGM 2022년 11월 10일
Again, save the images, not screenshots of the images. The image I end up using is not the same as the image you're using, because you're giving me a compressed screenshot that's probably not the same size.
Anyway. As the comments mention, you'll have to adjust the parameters to suit whatever pictures you're using. Since I don't have any of your actual pictures, that's not something I can do. I did add a little error check. The circularity was lower than expected by the prior threshold.
% 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('aaaaaaaaaaaaaaaaaaaaaaaaaa.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.7); % pick a limit
if isempty(iscircular)
error('no circular objects found under these conditions')
end
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
imshow(bigcircpict)
bigcircarea
bigcircarea = 8707
I don't know if discriminating by circularity is even worthwhile. I don't know what the other images look like, and I don't know what can be done (or will be done) to improve the initial thresholding.

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Image Analyst
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);
imshow(binaryImage);
% 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);
imshow(binaryImage);

Community Treasure Hunt

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

Start Hunting!

Translated by