How to fill a hollow donut-shaped object so that it looks like a circle?

3 views (last 30 days)
Nadia_Img
Nadia_Img on 20 Oct 2021
Commented: Nadia_Img on 20 Oct 2021
I have a segmentation that looks like a donut (see image attached). I want to fill the "donut hole" so that the segmentation looks like a circle instead of a donut. I feel like there has to be a simple solution but I cannot seem to figure it out. I have tried inverting the image using imcomplement and then using bwconncomp and regionprops to find the pixel indices of the two objects in the image (1 being the centre circle, the other being the background pixels which are now white, as seen in the attached image). However, running bwconncomp on the inverted image, I can get the pixel indices of the white pixels, but there is no distinction between the two objects such that I can set the "background" area to black and keep the centre circle as white. Is there any other solution?

Accepted Answer

Matt J
Matt J on 20 Oct 2021
Edited: Matt J on 20 Oct 2021
load Image
montage( {BW, imfill(BW,'holes')}, [],'Back','w','Bor',5)
  1 Comment
Nadia_Img
Nadia_Img on 20 Oct 2021
Thank you, I used your 'imfill' approach but did bwperim first to get a smoother line. This is my code:
j = double(bwperim(mask_Alg1)); %get perimeters of inner and outer circles
objects = bwconncomp(j); %get pixel indices
numPixels = cellfun(@numel,objects.PixelIdxList); %find indices inner and outer circles
[smallest,endo_indx] = min(numPixels); %smallest index
j(objects.PixelIdxList{endo_indx}) = 0; %get rid of smallest index
Epi_Mask=imfill(j,'holes'); %create new mask using only inner index.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!