Counting number of white > 5 Pixel objects in BW image

4 vues (au cours des 30 derniers jours)
CJ le 1 Déc 2022
Commenté : CJ le 7 Déc 2022
I want to automatically count the particles over 100 microsope images. Due to the sample grind I lost some of my particles and that is why I used the thresholding method according to otsu to make a BW image where my particles and the holes are white and my polymer black. Now im at the "easy part" of counting all the objects. Sadly i just cannot get it running. I tried to delete white pixels for a concentration of less than 5 pixels and simply count with bwlabel the white objects. Is there any other simpler way to count these objects?
please see the attachment.
I found my own solution:
clear all
%Image loading
for k=1:oNumFiles
BW2 = bwmorph(A,'bridge'); %Bridge unconnected pixels
BW3 = bwareaopen(BW2,5); %Remove objects containing fewer than 5 pixels
BW4 = imfill(BW3,"holes"); %fills holes
[L,Num] = bwlabel(BW4); %Counting white objects in my BW
Anzahl_Oben(:,k) = Num %Sum
[r, c]= size(F);
%Counting B&W Pixel
nWhite = sum(Size_Oben(:));
nBlack = numel(Size_Oben) - nWhite;
%Area related to the particles
Oben_Flaeche = numel(Size_Oben);
Oben_Partikelanteil(:,k) = nWhite/numel(Size_Oben);

Réponse acceptée

Image Analyst
Image Analyst le 4 Déc 2022
You can use bwareafilt or bwareaopen before calling regionprops.
BW4 = bwareafilt(BW4, 5); % Retain only blobs 5 pixels or bigger in area.
props = regionprops(BW4, 'Area'); % Measure areas.
allAreas = [props.Area] % Extract from structure array into a vector.
numParticles = numel(props) % Count the number of blobs in this image.
  4 commentaires
Image Analyst
Image Analyst le 5 Déc 2022
Yeah, sorry about that. @millercommamatt thanks for the fix.
What I had meant to use was bwareaopen instead of bwareafilt:
BW4 = bwareaopen(BW4, 5); % Retain only blobs 5 pixels or bigger in area.
So either function should work as long as you call them before regionprops.
It looks like you just wanted the total area of all blobs summed together. regionprops will give you the area of each blob individually. If that's the case you can do either
totalNumWhitePixels = sum(allAreas);
totalNumWhitePixels = nnz(BW4);
Let me know if this works for you.
CJ le 7 Déc 2022
Thank you again. You are right, I want to count the white objects and get the total area. Its seems that my skript does already that. So I will let it untouched

Connectez-vous pour commenter.

Plus de réponses (1)

millercommamatt le 1 Déc 2022
You should be able to to something like this. I'm waiting on a slow scatter interpolation on my client to finish so I haven't tested this so the syntax might night be quite right, but the idea is.
stats = regionprops(BW); %BW is the array of your binary image
num_object_get5 = sum(stats.Area >= 5);
  1 commentaire
CJ le 1 Déc 2022
Thank you for your answer! Sadly i get an error that i use too many input arguments for this

Connectez-vous pour commenter.


En savoir plus sur Image Processing Toolbox 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!

Translated by