OCR for cards

You might wonder why you need OCR for cards. Well there is a simple answer: i haven’t seen something done yet and there is a usefull thing if you need to register every game in a database. If you whant to make an online game with real cards with might need something like this.

At the first sight, when I was told the project, it hadn’t no idee about image recognition and it looked complicated, but after a while it stared to take shape.

The project was about a online game poker, game witch must be recorded in a database to respect the legislation. For that is not enough to display the cards as images to the online players, you need to know exactly what cards on the table so you can decide witch was the winner or to check if the game rules are obeyed.

In the game are nine players witch at first are given two cards and later are added five more card but this are visible to everybody. With your own two card and the visible one you have to make a combination. The player with the highest five-card poker hand at showdown wins the pot. This is Texas holdem.

The main problem of this project was the cards recognition. After talking with some friends (many thanks to Traian Zvirid)  it came out the folowing idea: let’s evaluate the card (sign and number) as number of colored pixels of the total zone pixels. Using this numbers I will make a database and then compare values computed from a new card with that database. If I will have a very close match I can tell what the card was.

Another problem were the pictures that had to be taken from the cards. I have to use an Axis 211 webcam witch has only 640×480 rezolution and an optic system not to good: at lower focus distances the fish-eye efect is big. Besides this there must be an eficient light source to light up the cards. The cards must be put in a fixed pisition … so there are some things to be put toghether.

Let’s start with the cards recognition algorithm. To do this the card must be devided in 3 parts: first will contain the sign and number from the left-up corner, second the body card (with caracters and signs) and the third the sign and number from the right-down corner. The first and the third areas are usefull to the image recognition. To get the coordinates of this regions you can do the following way:

1. for the first zone:

- start scanning column by column and compute the number of colored pixels out of total pixels. If you have only white pixels (or enough all most white pixels) it means that you haven’t hit the sign of the card (my cards had the sign larger that the number).

- when you hit the sign you skip the colored area unil it becomes white again

- keep those two pixels

2. for the third zone the algoritm is the same but is starts from the right to left

After this you will compute the X1-4 coordonates just as in the folowing picture:

Now that we have delimited the simbol and number areas there have to be splitted: for the recognition algoritm we have to have simbols and numbers separate. So this time we use the same algoritm described earlier but we scan the image from top to half  for the left corner and from bottom to half for the right corner. The images is scanned between the coordonates X1-X2 and X3-X4. We go to half on the picture because it is not necesary to scan all the image (performance reasons). After that we will compute the Y1-8 coordinates.

At this point we have all we need to start the evaluation algoritm:

- left corner: number (x1,y1, x2,y2), simbol (x1,y3,x2,y4)

-right corner: number (x3,y7,x4,y8), simbol (x3, y5, x4, y6)

So for every element in our picture we do:

- from top to bottom of the element we compute the number of colored pixel / number of total pixels
- the resulted array is compared with all values from all numbers/simbols stored in the database
- it the mach is found (with threshold) then that is the card. The match is computed as an arithmetic mean of the differences of the values so that is we have a single totaly out of order value that will be distributed uniform to the rest of the values and the error is canceled. But if there are many out of order values then the hole mean is afected.

Leave a comment

3 Comments.

  1. If you are today looking for an OCR tool compatible with any platforms like Windows, Linux and Mac, a site that can do OCR for you online may attract you: Free OCR. This online application allows transforming the content of an image file to text format. Though other output formats like MS Word is not supported yet, the result looks good to me.

  2. Hello. Thanks for your comment. I needed an application for linux that was part of a biger application. The online solution was not suited in this case. I will look over the site solution to see what it can do.

  3. Hey, try using the Hu moments for each part , number and sign. They are pretty good in recognizing shapes. You can find a version from the image analysis lab at ETTI here. http://alpha.imag.pub.ro/ro/cursuri/archive/mfiles.zip. The moments are pretty easy to compute and are skew and rotational invariants. There are other pretty nice methods to do shape recognition involving some edge detection. But since the set is limited ( 4 shapes, 10 numbers, and 3 figures) it should be quite straightforward. Also, a nice easy review about shape recognition here : http://alpha.imag.pub.ro/ro/cursuri/archive/forma.pdf. . I believe your approach interesting though. Any results? Maybe again you could make a surface(number of coloured pixels) / perimeter( number of edge pixels). Should be quite easy and interesting too. Please keep us posted.

Leave a Reply


[ Ctrl + Enter ]