I am writing a code where user clicks a point in hole and it has to be filled with white.I am not allowed to use imfill.

1 view (last 30 days)
implement hole filling algorithm without imfill.My code fills all the holes in one click,instead of just that hole where user clicks.Please help!

Accepted Answer

Image Analyst
Image Analyst on 23 Apr 2016
You want to have the user click on a "hole" in an image and fill it without using imfill(). I assume you have a binary image. So just label the image and find out what black hole they clicked on, create a mask, and fill it.
[column, row] = ginput(1); % Get user's click location.
labeledImage = bwlabel(~binaryImage); % Label holes.
thisLabel = labeledImage(row, column); % Find ID # of hole they clicked on.
holePixels = labeledImage == thisLabel; % Determine which pixels are in that hole.
% Fill with white
binaryImage(holePixels) = true;
Sounds like homework since homework often tells you not to use certain built-in functions. Hopefully for you it's not or using my code would not be allowed. But you didn't tag it as homework so it must not be, so I believe this code would be very fast.
  4 Comments

Sign in to comment.

More Answers (1)

Jon
Jon on 23 Apr 2016
A possible but perhaps slow solution is:
1. Get list of all holes in the image. There are a few ways to do this; you can use imcomplement then bwconncomp. bwconncomp returns a structure where one of the fields is PixelIdxList; this is the list you want.
2. Through your GUI or whatever, have the user select a pixel from within the hole to be filled.
3. Loop through each element in your PixelIdxList cell and check if your input pixel coordinates (might be more efficient to resize your image as a single-column vector) are within that blob.
4. If so, just set all the pixels of that blob (i.e. cc.PixelIdxList{idx_of_blob_containing_pixel}) equal to true or 1 in your original image.

Community Treasure Hunt

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

Start Hunting!