How to crop object in image automatically?

3 Ansichten (letzte 30 Tage)
Warisa Theerathamtada
Warisa Theerathamtada am 10 Nov. 2022
Kommentiert: DGM am 10 Nov. 2022
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

Akzeptierte Antwort

DGM
DGM am 10 Nov. 2022
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 Kommentare
Warisa Theerathamtada
Warisa Theerathamtada am 10 Nov. 2022
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
DGM
DGM am 10 Nov. 2022
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 10 Nov. 2022
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);

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by