How to crop object in image automatically?
    4 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    

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 
0 commentaires
Réponse acceptée
  DGM
      
      
 le 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
3 commentaires
  DGM
      
      
 le 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
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.
Plus de réponses (1)
  Image Analyst
      
      
 le 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);
0 commentaires
Voir également
Catégories
				En savoir plus sur Computer Vision with Simulink dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






