Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to detect credit card sized card

I want to detect a credit card sized card in image. The card can be any card eg identity card, member card. Currently, I am thinking to use Canny Edge, Hough Line and Hough Circle to detect the card. But the process will be tedious when I want to combine all the information of Hough Line and Hough Circle to locate the card. Some people suggest threshold and findContour but the color of card can be similar to the background which make this method difficult to achieve the desired result. Is there any kernel and method which can help me to detect the card?

enter image description here

enter image description here

like image 750
SamTew Avatar asked Mar 21 '18 02:03

SamTew


2 Answers

I think, your problem is similar to document scanner. You can refer to this link

  1. Find edges in the image using Canny edge detector (lower and higher thresholds can be set as 0.66*meanIntensity and 1.33*meanIntensity) and do a morphological close operation.

    Edge image after performing close

  2. Find the contours in the image using findContours

  3. filterout unwanted contours (I used contourArea to filter contours)

  4. using approxPolyDP approximate the contours to 7 or more points. (I used 0.005 * perimeter as the parameter here)

  5. If you want to find accurate edges, fit lines between the points and get the 4 biggest lines.Find their intersection (since the card may or may not contain curved edges)

  6. You'll end up with the card endpoints which can be used further for homography or to determine the region.

    vertices of the card

Edit Edited the answer to include the steps to obtain the vertices of the card and results are updated.

like image 156
Gopiraj Avatar answered Nov 11 '22 04:11

Gopiraj


There are two sub-problems here -

  1. Detect rectangular object in the image.
  2. The rectangular object's actual size should be similar to Credit Card.

For first part, you can try out several methods to extract rectangular region in the image and see which suits your need.

  • This post shows a lot of methods which you can try out.
  • In my experience edge detection works best in most cases. Try Canny > Contours with Appoximations > Filter out irrelevant contours > Search for rectangles using some shape detection, template matching or any other methods. Even this post does a similar thing to achieve its task.

Coming to the second point, you cannot find out the size of an object in an image unless you have any reference(known) sized object in the image. If the image was captured from a closer distance, the card will seem larger and if taken from far, the card will seem smaller. So while capturing, you will have to enforce some restrictions like you can ask the user to capture image along with some standard ruler. You can also ask the user to capture image on an A4 sheet with all the sheet edges visible. Since, you know the size of the A4 sheet, you'll be able to estimate the size of the card.

Apart from above methods, if you have enough data set of such images, you can use Haar Classifiers or Neural Network/Deep Learning based methods to do this with much better accuracy.

like image 2
Gaurav Raj Avatar answered Nov 11 '22 04:11

Gaurav Raj