Counting number of white > 5 Pixel objects in BW image

9 views (last 30 days)
Hello!
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.
Thanks!
I found my own solution:
clc
clear all
%Image loading
Oberseite='XXXX';
oFileName=dir(fullfile(Oberseite,'*.png'));
oNumFiles=numel(oFileName);
for k=1:oNumFiles
E=fullfile(Oberseite,oFileName(k).name);
A=imread(E);
oScans{1,k}=A;
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
F=im2bw(A);
[r, c]= size(F);
Size_Oben=F(1:r,1:c);
%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);
end

Accepted Answer

Image Analyst
Image Analyst on 4 Dec 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 Comments
Image Analyst
Image Analyst on 5 Dec 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);
or
totalNumWhitePixels = nnz(BW4);
Let me know if this works for you.
CJ
CJ on 7 Dec 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

Sign in to comment.

More Answers (1)

millercommamatt
millercommamatt on 1 Dec 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 Comment
CJ
CJ on 1 Dec 2022
Thank you for your answer! Sadly i get an error that i use too many input arguments for this

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!