Digit recognition with OpenCV 2.4.6 in Java

asked 2013-10-29 11:31:12 -0500

sebastianrevan gravatar image

updated 2013-10-29 11:44:12 -0500

berak gravatar image

Im trying to implement a basic Digit recognition using java, the main issue is that i'm really new with the framework and i dont know how to implement this.

i've read several c++ tutorials like this one: http://blog.damiles.com/2008/11/basic-ocr-in-opencv/ or this one: http://www.aishack.in/2010/08/sudoku-grabber-with-opencv/5/

and i know that i need a class with a train function that read a train file (which dictates what can be and what can not be recognized) , and a classify function using the k-nearest algorithm provided by opencv, im not so sure how to implement this, given that c++ tutorials use bitwise operations and operations that i can't replicate in java. the pre-procesing part is all done, i have a class that gives me a Mat object that contains the image of the digit, centered and without noise. i have to implement the part that actually takes that Mat object and turns it into an integer.

Thanks a lot for all your help!

edit retag flag offensive close merge delete


you can do bit operations in java, it just looks different http://docs.opencv.org/java/org/opencv/core/Core.html#bitwise_and(org.opencv.core.Mat,%20org.opencv.core.Mat,%20org.opencv.core.Mat)

berak gravatar imageberak ( 2013-10-29 11:47:25 -0500 )edit

for example, look at this method:

int DigitRecognizer::readFlippedInteger(FILE *fp) { int ret = 0; BYTE *temp;

temp = (BYTE*)(&ret);
fread(&temp[3], sizeof(BYTE), 1, fp);
fread(&temp[2], sizeof(BYTE), 1, fp);
fread(&temp[1], sizeof(BYTE), 1, fp);
fread(&temp[0], sizeof(BYTE), 1, fp);

return ret;


sebastianrevan gravatar imagesebastianrevan ( 2013-10-29 13:56:34 -0500 )edit

so ? that's reading a reversed sequence of 4 bytes. don't tell me that's impossible in java

berak gravatar imageberak ( 2013-10-29 14:01:11 -0500 )edit

please don't get mad. yes, i know it isnt impossible, but for example in this code, there are operations like memset that i dont really know how to use in java, i don't know any c++. the author talks about the endianness of the processor, and uses operations with pointers, since java runs on a virtual machine i don't know how can handle this

sebastianrevan gravatar imagesebastianrevan ( 2013-10-29 14:41:39 -0500 )edit

hey, no fear ;) noone's mad at you. porting c/c++ to java can be quite tricky.

again, sorry for sounding harsh, not intended.

berak gravatar imageberak ( 2013-10-29 14:52:51 -0500 )edit

ok no problem :), yes, its very tricky. i have a speciffic question: How can i read the training data so i can contain it in a Mat object?, and what format does the training data has to have? the training data is algorithm-specific?

sebastianrevan gravatar imagesebastianrevan ( 2013-10-29 15:50:45 -0500 )edit

sorry, must have missed something , training data for what, again ?

berak gravatar imageberak ( 2013-10-29 15:55:01 -0500 )edit

What about python?Maybe it is easier to understand.Try this link--http://stackoverflow.com/questions/9413216/simple-digit-recognition-ocr-in-opencv-python. You need to understand pointer before you try to study memset.Although java do not support pointer(haven't tried Unsafe class yet), but you are using a less powerful pointer in java anytime.The object of java is some sort of "const(final) pointer", or similar to the "reference" in c++.

stereomatching gravatar imagestereomatching ( 2013-10-29 15:58:05 -0500 )edit

if it's an opencv machine-learning thing, you're expected to deliver a Mat with 1 row per item ( if that's an image, you'll have to 'flatten' it to a row using reshape(1,1). also, iirc, most algos assume float data

berak gravatar imageberak ( 2013-10-29 15:59:07 -0500 )edit

hi, stereomatching, note that this guy is already confused enough, translating c++ to java ;)

berak gravatar imageberak ( 2013-10-29 16:01:31 -0500 )edit