Separating touching objects in binary image

A few things come to mind.

  1. Can you show us the input image?
  2. How did you get to the mask image from the input image? Maybe you could tweak come of the image processing parameters to prevent this from happening in the first place? (Is there a visible gap between the planks in the input image?)
  3. I might consider fitting lines to the image and wherever you find two* nearly parallel lines that are close to each other (or intersect at a point inside the image), compute an average line and draw it to the line as black. You might need to draw it with a width wider than 1 pixel so aliasing doesn’t result in the two separate things remaining connected diagonally at the pixel level.

*in reality you might end up with a bundle of lines grouped together (if you are using houghLines to detect the lines, for example), so you’d compute the average line among potentially many lines.

1 Like