Counting number of white > 5 Pixel objects in BW image

2 次查看(过去 30 天)
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

采纳的回答

Image Analyst
Image Analyst 2022-12-4
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 个评论
Image Analyst
Image Analyst 2022-12-5
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 2022-12-7
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

请先登录,再进行评论。

更多回答(1 个)

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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with Image Processing Toolbox 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by