How can I count the black particles without counting the small particles in the background?

1 view (last 30 days)
I want to automatically measure the particle size and number, but I can't because of the out-of-round shape and the contrast with the background.
rgb_im = imread('bild_1.jpg');
gray_im = adapthisteq(rgb2gray(rgb_im));
% bw_im = imbinarize(gray_im,'adaptive','ForegroundPolarity','dark','Sensitivity',1);
bw_im2 = ~imbinarize(gray_im,'adaptive','ForegroundPolarity','bright','Sensitivity',1);
bw_im4 = imdilate(bw_im2,strel('disk',1));
BW1 = imfill(bw_im2,'holes');
BW2=255-BW1;
figure, imshow(BW2)
imshowpair(BW1,BW2,'montage')
I have inverted the image and now I want to remove the small black dots from the background. However, I can't get that to work.

Accepted Answer

Antoni Garcia-Herreros
Antoni Garcia-Herreros on 12 Apr 2023
Edited: Antoni Garcia-Herreros on 12 Apr 2023
Hello loris
Regionprops does exactly what you are looking for.
You can try something like this:
clear all
close all
rgb_im = imread('bild_1.jpg'); %Read RGB image
Error using imread>get_full_filename
File "bild_1.jpg" does not exist.

Error in imread (line 372)
fullname = get_full_filename(filename);
gray_im=rgb2gray(rgb_im); % RGB to grayscale
BW=~imbinarize(gray_im,'adaptive','ForegroundPolarity','bright','Sensitivity',1); %Binarize image
BWao = bwareaopen(BW,20); % Remove particles smaller than 20 pixels
BWaod = imdilate(BWao,strel('disk',2));
BWaodf=imfill(BWaod,'holes');
r=regionprops('table',BWaodf,'Centroid','Area','Circularity','EquivDiameter');
m=table2array(r); %Transform table to array
%Filter by cicularity
thr=0.6; % You may change this parameter
Points=m(m(:,4)>thr,:); % Array containing all the Particles
% Points=[Area [pixels], Centroid X, Centroid Y, Circularity, Equiv Diameter ]
imshow(rgb_im)
hold on
viscircles([Points(:,2),Points(:,3)],Points(:,5)*0.5,'LineWidth',0.5);
Nparticles=size(Points,1); %Number of Particles
MeanD=mean(Points(:,5)); % Mean Diameter
Hope this helps

More Answers (0)

Community Treasure Hunt

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

Start Hunting!