MATLAB Answers


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.

Asked by preeti visweswaran on 23 Apr 2016
Latest activity Commented on by preeti visweswaran on 23 Apr 2016
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!


Sign in to comment.

2 Answers

Answer by Image Analyst
on 23 Apr 2016
 Accepted Answer

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.


Show 1 older comment
That doesn't look good. For one thing, arrays are not indexed as (x,y) - they are indexed as (y,x). I suggest you scrap that method and use my simpler, faster, and more straightforward approach.
Thanks a lot!! The change from [x,y] to [y,x] worked like a charm!!!

Sign in to comment.

Answer by 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.

  1 Comment

Great!Thanks,let me try this and come back to you.Appreciate your help

Sign in to comment.