Filtering Blobs on a Binary Image
5 views (last 30 days)
Show older comments
Hi, I have a 12bit grayscale image of some beads. I want to make measurements on the central one and as a first approach, I want to remove all the other blobs and replace their values with an average of the background

I've identified the most central blob. Is there away to remove the other blobs?
Also, for the area filtering, I have hard coded the limits, but it will change for my images . Im looking for a way to get a ball park estimate of the size of the objects (area). I was thinking about c=normxcor with a gaussian template and loop it about 5 times with different gaussian fwhm's and see which one throws up the highest value of c. Is this a good / bad idea, any other ways to get a ball park figure of the size (area)?
Thanks
ax=app.UIAxes2; clc;
IMraw=getimage(ax); IM=IMraw-min(IMraw(:)); %Background
[sy,sx]=size(IM); xc=sx/2; yc=sy/2;
% Create Binary Image
mn=mean2(IM);
thresholdValue = mn;
BW = IM > thresholdValue; % Binary image
myfigure(app,1200,400);
tcl = tiledlayout(1,4,'TileSpacing','compact','Padding','compact'); %tight
ax1=nexttile;
myImagesc(app,ax1,IM); title(ax1,'Raw Image');
ax2=nexttile;
myImagesc(app,ax2,BW); title(ax2,'Binarised');
ax3=nexttile;
img1 = xor(bwareaopen(BW,40), bwareaopen(BW,100)); % Area Filter Here (Can also use bwareafilt here)
myImagesc(app,ax3,img1); title(ax3,'Area Filter');
% Try To Ientify blob nearest centre of image - need
% RegionProps
s = regionprops(BW,'Centroid','Circularity');
cen = cat(1,s.Centroid); circ=cat(1,s.Circularity);
x1=cen(:,1); y1=cen(:,2);
% Find Distance of Blob centroids from centre of Image
n=numel(x1);
data=[];
if n>0
for i=1:n
X = [x1(i),y1(i);xc,yc];
d = pdist(X,'euclidean');
Intensity=IM(round(y1(i)),round(x1(i)));
data(i,1)=1; data(i,2)=x1(i); data(i,3)=y1(i); data(i,4)=d; data(i,5)=Intensity; data(i,6)=circ(i,1);
end
end
% Sort rows in Distance D from xc,yc
B= sortrows(data , 4,"ascend")
% Plot on Binary the one to keep
xkeep=B(1,2); ykeep=B(1,3); hold(ax3,'on');
plot(ax3,xkeep,ykeep,'r*');
2 Comments
Answers (1)
Matt J
on 7 Mar 2025
Edited: Matt J
on 7 Mar 2025
If you can form an under-estimate of the extent of the blobs, let's call them sub-blobs, then you can fit a Gaussian lobe to the sub-blob pixels, using for example, this FEX download:
The fitted covariance matrix will then give you a sharper estimate of the full blob size.
1 Comment
Matt J
on 7 Mar 2025
Edited: Matt J
on 7 Mar 2025
1: How to make the "Keep" Blob exactly circular - that then becomes my mask?
Once you've estimated the center and radius of the mask, you can circularize it with
h=drawcircle('Center',___,'Radius',__);
mask=h.createMask;
delete(h);
or
V=nsidedpoly(1000,'Center',___,'Radius',____).Vertices;
mask=poly2mask(V(:,1),V(:,2),sy,sx);
2: I've currently put every ~mak as the mdeain of the min 10 pixels, but its too low. Any suggestions to getting the median value of the pixels in the perimeter of the mask?
If you use gaussfitn, as I suggesed above, then the estimated D parameter will be an estimate of the background.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!