7

I'm doing computer vision project for automatic card detection. I need to separate the card from the background. I have applied the canny edge detection, using automatic parameter settings from this

Automatic calculation of low and high thresholds for the Canny operation in opencv

The result is excellent. However, sometimes the canny is not perfect like this Photo after canny

I have applied cvFindContour to detect the box. However, due to "hole" on the upper side, opencv failed to detect the contour.

How do I tune the cvFindContour to detect the contour or should I tune the canny edge instead?

Community
  • 1
  • 1
IllSc
  • 1,419
  • 3
  • 17
  • 24
  • 1
    did you try "line detection" (like HoughLinesP or RANSAC) to extract the 4 main lines? – Micka Aug 05 '14 at 16:15
  • See the answer [here](http://stackoverflow.com/questions/22240746/recognize-open-and-closed-shapes-opencv/22242203#22242203) might be helpful. – Haris Aug 05 '14 at 16:16
  • I tried using Hough, but the letters inside the card is becoming noise for the Hough. – IllSc Aug 06 '14 at 05:48
  • Since you're getting a pretty clean outline anyway e.g. in the binary image output above, why not just run a hough transform over that output? Then you have the robustness of the hough global approach having used findcontours as a method for filtering out the text on the card that was causing you problems. – jcollomosse Aug 10 '14 at 20:04
  • Actually inside the card there are edges of picture and text (this is an identity card). But I censored it for privacy purposes – IllSc Aug 12 '14 at 14:54

1 Answers1

13

There are multiple possible solutions.

The simplest one may be:

  • if FindContours does not find a closed contour, repeat the canny filter with a slightly decreased low_threshold, until you find a closed contour. If the closed contour has roughly the right size and shape, it is a card. The answer linked by Haris explains how to check whether a contour is closed

Another rather simple solution:

  • Don't apply Canny to the image at all. Execute findContours on the otsu thresholded image. Optionally use morphological opening and closing on the thresholded image to remove noise before findContours

FindContours does not need an edge image, it is usually executed with a thresholded image. I don't know your source image, so I cannot say how good this would work, but you would definitely avoid the problem of holes in the shape.

If the source image does not allow this, then the following may help:

  • use watershed to separate the card from the background. Use a high threshold to get some seed pixels that are definitely foreground and a low threshold to get pixels that are definitely background, then grow those two seeds using cv:watershed().

If the background in that image is the same color as the card, then the previous two methods may not work so well. In that case, your best bet may be the solution suggested by Micka:

  • use hough transform to find the 4 most prominent lines in the image. Form a rectangle with these 4 lines.
Community
  • 1
  • 1
HugoRune
  • 13,157
  • 7
  • 69
  • 144