Filtering Blobs on a Binary Image

5 views (last 30 days)
Jason
Jason on 7 Mar 2025
Edited: Matt J on 7 Mar 2025
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
Jason
Jason on 7 Mar 2025
Edited: Jason on 7 Mar 2025
I've just found this seems to work for isolating the central spot
BW2 = bwselect(BW,xkeep,ykeep);
But the subtraction doesn't work
%Subtract from BW
mask=BW-B;
Jason
Jason on 7 Mar 2025
Edited: Jason on 7 Mar 2025
So I've got a little further and dilated the central blob abit.
Couple of follow on queastions
1: How to make the "Keep" Blob exactly circular - that then becomes my mask?
2: I've currently put every ~mask as the median 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?
% Sort rows in Distance D from xc,yc
B= sortrows(data , 4,"ascend");
B
% Plot on Binary the one to keep
xkeep=B(1,2); ykeep=B(1,3); hold(ax3,'on');
plot(ax3,xkeep,ykeep,'r*');
BW2 = bwselect(BW,xkeep,ykeep); % Removes all other blobs
SE2 = strel('disk',6); % would like to "circularise and expand a bit"
BW3 = imdilate(BW2,SE2);
ax4=nexttile;
myImagesc(app,ax4,BW3); title(ax4,'Keep');
%Apply Mask
mask=BW3;
median(mink(IM(:),10))
IM(~mask)=median(mink(IM(:),10));
ax5=nexttile;
myImagesc(app,ax5,IM); title(ax5,'Mask');

Sign in to comment.

Answers (1)

Matt J
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
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.

Sign in to comment.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!