22
$\begingroup$

I need to find all the contours in an image retrieved from the camera. So I first use the canny edge detector to find the edges and then find the contours. Pretty simple.

However, my contours get "merged". For example, in the image below, I clearly have 4 different objects. The edges are slightly touching at some points though, so I get one big contour instead of four separate ones. I tried changing around the thresholds, eroding, morphology operations and similar things but the edges keep slightly touching. Does anyone have any suggestions on how to get separate contours in images similar to the one below? (The image below is obviously just an example, my actual images are much more complex, but have the same basic problem).

enter image description here

$\endgroup$
2
  • $\begingroup$ Watershed segmentation can work. $\endgroup$
    – sm176357
    Jul 23, 2013 at 11:58
  • $\begingroup$ So, you should also consider cases where the contact is a line and not just a point too (touching but not overlapping) $\endgroup$ Aug 13, 2019 at 14:11

4 Answers 4

12
$\begingroup$

Detecting different components:

If you're trying to detect the different components, there probably are other approaches to do them than detecting the contours. Here's an example in Mathematica. An erosion followed by dilation is used to close the gap in the second component before detection (if you don't do this, it won't detect it).

img = Binarize@Import["https://i.stack.imgur.com/yqDyu.png"];
Colorize[MorphologicalComponents[Dilation[Erosion[img,1],1]]]

The figure on the left below, shows imperfect object detection (without closing the gap) and on the right, shows the correct detection (running the above code).

enter image description here enter image description here

Detecting the different contours:

However, if you indeed want to separate only the contours, here's an example. The erosion and dilation are performed as before to close the gap and the resulting image is run through a Canny edge detector. I've made the default options explicit, so that you can see what's being used.

img2 = EdgeDetect[Dilation[Erosion[img, 1], 1], Method -> "Canny"]

This will give you both the inside and the outside edge (see figure on the left below), since the pixel width is greater than 1 all around. I haven't had much luck trying to get it thinner, as the performance degrades (may be different for your other images). The inner contours are the ones you want, and the outer contour is just the combined contour of all 4 components. Now all we need to do is drop the outermost one with:

SelectComponents[img2, "EnclosingComponentCount", # > 0 &]

which gives you just the inner contours (see bottom right). In other words, it only picks those contours that are enclosed by at least one other contour, which automatically disqualifies the outermost one. I do not know the equivalent of these commands/operations in openCV.

enter image description here enter image description here

Note that the apparent breaks in the figure are due to saving to jpeg in a smaller size. It does not look that way on my screen.

$\endgroup$
0
2
$\begingroup$

Try pre-processing your images with a morphological filter like erosion. This will allow touching contours to be separated. After detecting your contours, you can apply a dilation operation to complete lattices.

$\endgroup$
2
  • $\begingroup$ I tried that, but the results showed no improvement. $\endgroup$
    – Farhad
    Dec 3, 2011 at 22:12
  • 1
    $\begingroup$ Can you show a real example image? $\endgroup$
    – fmorency
    Dec 4, 2011 at 0:07
2
$\begingroup$

It's not an answer to your question, but contour analysis is error-prone. You cannot do much about it and it works only on very simple scenarios.

If you have trouble using it, you should look for a completely different algorithm. There are more complex and more robust ways to solve things, but it depends on what you want to achieve (object detection, tracking, etc...)

$\endgroup$
2
  • $\begingroup$ Thank You. My program is used for hand detection, so I would think it would be very similar to object detection. Do you gave any suggestions for more complex and robust algorithms? Haar Features, SURF and similar machine learning algorithms are not something I can do. $\endgroup$
    – Farhad
    Dec 4, 2011 at 20:58
  • $\begingroup$ Did you look at these resources? paginas.fe.up.pt/~hgc2011 They are mainly databases/results, but I hope you'll be able to find some good papers here. $\endgroup$
    – vasile
    Dec 5, 2011 at 8:29
0
$\begingroup$

Contours are not necessarily open consider that you have used canny to detect them. The problems with Canny were already disccused here. The discussion on canny gives you the basic idea that there is still operations like closing and dilation that are require on top of canny to evaluated closed contours.

This also depends on whether we are looking for contours or segmentations (Canny versus methods like Graphcuts). So i guess looking for robust solution depends on your final application.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.