Message Boards Message Boards

4
|
7435 Views
|
1 Reply
|
6 Total Likes
View groups...
Share
Share this post:

Removing holes from images - including those connected to the boundary

I just read a blog post on Defining and filling holes on the border of an image on Steve on Image Processing which is about removing image holes including those connected to the image boundary.

The way the solution is formulated is by assuming that holes that are connected to the borders, are black regions that are connected to either of the two adjacent border (e.g. Left and Top, or Top and Right, etc).

This is our version of their image:



I wondered how and how easily this task can be done in Mathematica and here are my two simple solutions:

1) If all holes (both those entirely inside and those connected to the border) are small, one could use DeleteSmallComponents on negated image and add negate to get the filled image. So:
ColorNegate@DeleteSmallComponents[ColorNegate[i]]






2) Second solution would be to (almost) implement the idea that Steve has implemented. Almost, since I chose an even simpler approach. On a negated image, I use SelectComponents and pick all components that are connected to 2 or fewer borders. This will return all holes, both internal holes as well as those connected to the at most two borders. Add the result to the original image to get the holes filled:
ImageAdd[i,
SelectComponents[ColorNegate[i], "AdjacentBorderCount", # <= 2 &]]




Notice that this definition of "holes connected to borders" is not a well defined concept. An image could very well be a diagonal set of black and white stripes going from top-right to bottom-left and using this technique all black stripes are removed. I am sure you can think of more counter examples.
POSTED BY: Shadi Ashnai
Another way to do it is using FillingTransform. It is manual and more tedious because one must create a musk but it is more flexible - here we fill only boundary holes and leave "true" holes unfilled.
i = Import["http://goo.gl/EvvA7j"];
mask = Image@SparseArray[{{50, 50} -> 1, {61, 413} -> 1, {50, 806} -> 1, {800, 800} -> 1, {850, 500} -> 1}, ImageDimensions[i] + 2];
FillingTransform[ImagePad[i, 1, White], mask]

POSTED BY: Vitaliy Kaurov
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract