Removing Appendages From Binary Mask

6 visualizzazioni (ultimi 30 giorni)
Greg
Greg il 8 Nov 2022
Risposto: Maria Byrne il 11 Mag 2023
I am trying to create a binary mask to segment some biological images. I am able to generate a decent mask, however there are unwanted appendages dangling off the final mask, as seen in the image below. How can I remove these appendages without affecting the rest of the mask? I have tried eroding, but whenever I erode enough to get rid of the appendages, it inevitably affects the rest of the mask, making it too round. Watershedding has also not helped with this problem. Also, changing the thresholding level does not fix the issue. I would like the rest of the mask to stay exactly the same as in the image below, and only remove the appendages. Here is the thresholding function I have written so far:
function [I2,BW,BW1,BW2] = threshold_frame(I,size)
I2=normalizeImage(I); %normalize image to max (and subtract minimum pixel value too).
I2=1-I2; %plot the "negative".
[counts,x] = imhist(I2,500);
stem(x,counts)
T = otsuthresh(counts); %generate the threshold using Otsu's method.
BW = imbinarize(I2,T); %threshold the image
BW = bwareafilt(BW,1); %get the largest object
BW=imfill(BW,'holes'); %fill holes
se1 = strel('disk',10,8); %create structuring element for erosion
BW = imdilate(BW,se1); %dilate, just to fill any gaps in the exterior
BW = imerode(BW,se1); %erode
BW=imfill(BW,'holes'); %fill in holes again, hopefully filling in any pockets that were previously unfillable before the dilation
se2 = strel('disk',size,8); %generate structuring element to remove appendages
BW1 = imerode(BW,se2); %erode to remove appendages
BW1 = bwareafilt(BW1,1); %get the largest object
BW2 = imdilate(BW1,se2); %dilate to restore initial shape
BW2 = imgaussfilt(uint8(BW2),30); %smoothen the contour a little bit
%Now that the image has been smoothened, it needs to be thresholded again to make it binary
[counts,x] = imhist(BW2,500);
stem(x,counts)
T = otsuthresh(counts);
BW2 = imbinarize(BW2,T); %rethreshold the smoothened image
BW2 = bwareafilt(BW2,1); %finally, take the biggest obejct again
end
Your help is much appreciated.

Risposte (2)

Image Analyst
Image Analyst il 8 Nov 2022
You can "chop it off" if you manually lassoo it with drawpolygon. See attached demo.
Otherwise you can repeatedly use imerode until there is more than one blob. Then call bwareafilt and imdilate to expand it back to the original size. Plus a few more things to make sure the original boundary is unchanged.

Maria Byrne
Maria Byrne il 11 Mag 2023
I was stuck on this same challenge for a few days. I eventually discovered that "imopen" is the ideal solution for this. Use a structuring element that is disk shaped and that has a radius larger than your appendage but smaller than the larger object.

Community Treasure Hunt

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

Start Hunting!

Translated by