How to crop object in image automatically?

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

 Accepted Answer

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 Comments

I use your code but it has an error "Insiffucient number of outputs from right hand side of eqal sign to satisfy assignment" in line
bigcircpict = S(bigcircidx).Image;
in some image for example
why it happend like this?? Can you help me please
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.

Sign in to comment.

More Answers (1)

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);

Products

Release

R2021a

Asked:

on 10 Nov 2022

Commented:

DGM
on 10 Nov 2022

Community Treasure Hunt

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

Start Hunting!