Ask Your Question

Detect contour with mask

asked 2014-10-25 05:20:06 -0600

Rasmus gravatar image

updated 2014-10-26 05:18:34 -0600

Hi all,

I'm detecting circular objects in an image by first getting a binary map based on color deviation from the background and then getting the contours of the connected components (and then a bunch of other stuff). The thing is that there may be text written over the objects, which results in that the objects are split into several parts. The text can be small and just inside some connected component or it may be many times larger than the components. I can generate a mask for the text based on its color. I want to ignore the text somehow, but I'm not quite sure how.

image description

In this case the fitting for three connected components has failed due to overlapping text. In the leftmost case no fitting could be made (no black circles around object) since the "O" excludes a piece of the object. In the middle case there are many small incorrect circles fitted because the contour gets fragmented. In the right case the fitting is bad due to the overlapping text.

I see three possible ways forward:

-Deal with this when first getting the connected components

-Merge contours which are connected by the text mask

-Get contours which include the text and then modify the contours afterwards

Any help with this would be much appreciated! Maybe there is even some clever OpenCV function for this? Thanks!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2014-10-25 12:07:46 -0600

Hi! I would do this in four steps:

  1. Extract connected component of text (using your binary mask)
  2. For each connected component, look for the surrounding class (i.e. object of interest or background)
  3. Paint each connected component in white or black, depending on the surrounding class...
  4. extract contour of the new binary mask

It's probably not the fastest way to do that, but it will probably work!

edit flag offensive delete link more


Or you can also use inpainting to erase text from the binary map...

petititi gravatar imagepetititi ( 2014-10-25 12:12:02 -0600 )edit

Thanks! I was maybe not very clear in the first post, but one problem is that the text may be only partly overlapping with the objects of interest. If I inderstood you correctly a text component would be colored white if some part of it touched an object of interest. That is not what I'm looking for since it would change the shape of the object. Inpainting looks interesting, but I'm afraid it would also distort the shapes. Is it possible to perform set operations like union, setdiff and so on on contours?

Rasmus gravatar imageRasmus ( 2014-10-25 12:48:59 -0600 )edit

Regarding your image, the best thing to do seems to apply Inpainting on the color image, then apply your segmentation. Inpainting will try to preserve the shape of the objects...

Union/diff of contours are not implemented in Opencv so if you want to go that way, you will have to deal with another library.

petititi gravatar imagepetititi ( 2014-10-26 11:53:17 -0600 )edit

Question Tools

1 follower


Asked: 2014-10-25 05:20:06 -0600

Seen: 1,786 times

Last updated: Oct 26 '14