20

I'm using OpenCV on the iPhone and need to detect numbers in an image. I split the image into smaller images so each image has only one number (1-9). All numbers are printed, NOT handwritten.

What would be the best approach to figure out the numbers with OpenCV?

UPDATE:

I have successfully found the numbers and extracted them. They look like this:

http://img198.imageshack.us/img198/5671/101ht.jpg
http://img824.imageshack.us/img824/539/606yu.jpg

When they are extracted they are in the same size and so on. I have saved a bunch of images and put them in a OCR dir where they are categorized into numbers. Like: ocr/1/100.jpg 101.jpg.... and ocr/2/200.jpg 201.jpg....

Then I was going to use the same approach as in the Basic OCR tutorial:http://blog.damiles.com/?p=93

However, I'm programming for iPhone and can't use C++ code (error on compiling and so on) and I don't have access to highgui.

I tried using cvMatchTemplate() and match a bunch of images but it seems to work pretty bad...

Any other ideas I can try?

Chris
  • 8,030
  • 4
  • 37
  • 56
Linus
  • 2,799
  • 3
  • 29
  • 43
  • @Linues Have you ever tried to differentiate the numbers & characters in an image, i.e. only get numbers from image OR something in dimensional array of contours using :findContours. I am trying to differentiate numbers from char. Please let me know, if suggestion from your end. – Ajay Sharma Jun 02 '14 at 05:47
  • [Basic OCR in opencv](http://blog.damiles.com/?p=93) – Martin Beckett May 03 '11 at 17:55
  • 1
    Thank you. I have read it before though. He's doing OCR on handwritten numbers. Will the train images work good for printed text as well? – Linus May 03 '11 at 18:35
  • It will work much much better. It's always easier with regular shapes – Martin Beckett May 03 '11 at 18:47
  • Sure, but then I'll have to find train images for printed numbers and not handwritten numbers, right? – Linus May 03 '11 at 18:49
  • Yes - just generate a set of images with your numbers in the correct font, size etc – Martin Beckett May 03 '11 at 18:51
  • I just updated the first post with some more info about my issues... – Linus May 10 '11 at 09:27

10 Answers10

8

You could start by reading about Principal Component Analysis (PCA), Fisher's Linear Discriminant Analysis (LDA), and Support Vector Machines (SVMs). These are classification methods that are extremely useful for OCR, and there are libraries in any language including C++, Python, C# etc.

It turns out that OpenCV already includes excellent implementations on PCAs and SVMs[dead link]. I haven't seen any OpenCV code examples for OCR, but you can use some modified version of face classification to perform character classification. An excellent resource for face recognition code for OpenCV is this website[dead link].

Anthon
  • 69,918
  • 32
  • 186
  • 246
Jaime Ivan Cervantes
  • 3,579
  • 1
  • 40
  • 38
5

If the numbers are printed, the job is quite simple, you just need to figure out a nice set of features to match. If the numbers are one font, you can get away with this approach:

  • Extract the number
  • Find the bounding box
  • Scale the image down to something like 10x8, try to match the aspect ratio
  • Do this for a small training set, take the 'average' image for each number

  • For new images, follow the steps above, but the last is just a absolute image difference with each of the number-templates. Then take the sum of the differences (pixels in the difference image). The one with the minimum is your number.

All above are basic OpenCV operations.

Rob Audenaerde
  • 19,195
  • 10
  • 76
  • 121
2

Basically your problem is just to classify a feature vector, which is the set of pixel intensities after some preprocessing steps. You can use any classifier for this task, like eg. neural networks, which should have a C implementation inside OpenCV. You might also try a C libsvm library for Support Vector Machines.

There is a good site related to this problem with a lot of papers and a training database.

crenate
  • 3,370
  • 2
  • 20
  • 13
2

Maybe the most simple and convinient way is to use svm as ml algorithm http://opencv.willowgarage.com/documentation/cpp/support_vector_machines.html and gray images as feature vectors.

Alex Hoppus
  • 3,821
  • 4
  • 28
  • 47
1

Convolution Neural Networks are by far the best algorithms for hand written digits. The are implemented in most systems like USPS etc. Here are few papers explaining the algorithms. http://yann.lecun.com/exdb/lenet/

Krish
  • 1,747
  • 14
  • 19
1

Objective C++? Try renaming your .m files to .mm and you can then use c++ in your iPhone project.

melps
  • 1,247
  • 8
  • 13
0

Simple Digit Recognition OCR in OpenCV-Python

This might help you out. Converting the code from Python to C++ is not a difficult task, since OpenCV API's are same for the both.

Community
  • 1
  • 1
divanshu
  • 335
  • 1
  • 5
  • 12
0

Tesseract is also a nice free OCR engine that is readily available for iPhone and allows you to use your own sets of training images: http://tinsuke.wordpress.com/2011/11/01/how-to-compile-and-use-tesseract-3-01-on-ios-sdk-5/

bellkev
  • 915
  • 7
  • 10
0

HOG + SVM (Try to play with kernels)

Alex Hoppus
  • 3,821
  • 4
  • 28
  • 47
0

This is a nice open source ,It is a ORCDemo on iPhone.Hope it is useful to you

shihongzhi
  • 1,921
  • 16
  • 17