Counting number of white > 5 Pixel objects in BW image

조회 수: 9 (최근 30일)
CJ
CJ 2022년 12월 1일
댓글: CJ 2022년 12월 7일
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

댓글을 달려면 로그인하십시오.

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by