0

I've been trying to figure out how to go about detecting cards on a table, then extracting images of just the cards. After doing some reading, I figured it's best to use cv.Canny to edge detect since the cards will be the only things on the white table. Then, get the card image from a rectangular cutout of the image according to the edges detected.

But I'm not sure how to get the edge information(coordinates) from the Canny info. I can get the Canny information and store it to an IplImage, but I'm not sure how to get information necessary to create a rectangle around the card and cutout the image.

import cv

cam = cv.CaptureFromCAM(0)

while True:
    capture = cv.QueryFrame(cam)

    #apply mask to block out white background
    grey = cv.CreateImage(cv.GetSize(capture), 8, 1)
    masked_image = cv.CreateImage(cv.GetSize(capture), 8, 3)
    cv.CvtColor(capture, grey, cv.CV_BGR2GRAY)
    cv.Threshold(grey, grey, 100, 255, cv.CV_THRESH_BINARY)
    cv.Zero(masked_image)
    cv.Not(grey, grey)
    cv.Copy(capture, masked_image, grey)

    #detect corners
    corners = cv.CreateImage(cv.GetSize(masked_image), 8, 1)
    #cv.CornerHarris(masked_image, corners, ?)
    cv.Canny(grey, corners, 900, 890)
    cv.ShowImage('b_window', corners)
    cv.WaitKey(2)
user594044
  • 255
  • 2
  • 4
  • 12
  • Possible duplicate of http://stackoverflow.com/questions/1817442/how-to-recognize-rectangles-in-this-image – Andrew B. Mar 27 '12 at 06:56
  • I specifically need to know the x, y coordinates, and height, width of the detected cards from the image. – user594044 Mar 27 '12 at 07:32
  • possible duplicate of [OpenCV C++/Obj-C: Detecting a sheet of paper / Square Detection](http://stackoverflow.com/questions/8667818/opencv-c-obj-c-detecting-a-sheet-of-paper-square-detection) – karlphillip Mar 27 '12 at 12:52
  • That's a different language and I specifically stated I need the x, y coordinates of edges/corners. – user594044 Mar 28 '12 at 18:26

3 Answers3

2

Assuming that the edges of the card/rectangle are prominent after the Canny operation, you can find external contours using findContours with CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE to retrieve the rectangular contour encoded with 4 points. Note that perspective distortion does not matter as it will encode the 4 external points regardless of perspective.

Test the contours (convex with angles in range). karlphillip did a good example of this: https://stackoverflow.com/a/8863060/3475075

If it detected other contours in the image, retrieve the largest rectangle (contour with largest area) using contourArea()

If you need to know which point is top left, right, etc a simple way to determine is calculating the sum and difference of the x and y values:

  • top left has the smallest sum
  • bottom right has the largest sum
  • top right point has the smallest difference
  • bottom left has the largest difference
Community
  • 1
  • 1
Donovan
  • 291
  • 2
  • 8
1

There is a good blog post about it:

So I Suck At 24: Automating Card Games Using OpenCV and Python

The code is available on gihub: https://github.com/arnabdotorg/Playing-Card-Recognition

Community
  • 1
  • 1
Maxime R.
  • 8,043
  • 6
  • 50
  • 58
0

You should post more details to help narrow the advice. For example, is the image overhead, or will there be projective distortion? Is there uniform lighting? How many cards are there? Do cards overlap? In general, an edge detector is just a small first step in object detection.

If your problem is highly-constrained, though, you might be able to threshold, filter connected components, use the Hough transform or use color information.

Andrew B.
  • 962
  • 5
  • 18
  • They will be laid out on a white background, no projective distortion, uniform lighting, hopefully as many cards as possible(dozen?), they don't overlap. I already have the images I'm looking for(scans of the cards) in a database, with a percectual hash(pHash) so I just need an image that is the same/similar as the card scans. If I could somehow get the actual x,y coordinates of each of the corners of the detected cards/edges then I think I could take it from there. – user594044 Mar 27 '12 at 16:44