Using Tesseract with openCV Mat

56 views
Skip to first unread message

Adam Richards

unread,
Oct 11, 2018, 10:39:14 AM10/11/18
to tesseract-ocr
Hi I am having issues with sending a Mat image the I have opened through OpenCV to Tesseract using the "SetImage" function.  
I can use Tesseract on the image when I just input the commented out bit at the end of the line as: SetImage(image);

however when I run it as below the program will crash before even reaching this point and return to console "segmentation error"

I have found this code as being identical to somebody who previously did my project, and i have no idea why this error is occurring.


Please if somebody can help me or provide a working example of Tesseract using Mats from OpenCv I would greatly appreciate it.

kind regards,

Adam


int main(int argc, char** argv )
{

imgOriginal = imread(argv[1],1);          // open image
Mat img = imgOriginal;

.........

        namedWindow("img", WINDOW_NORMAL); 
imshow("img", img);
waitKey(0);                 // hold window open until user presses a key

    char *outText;
    tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
    // Initialize tesseract-ocr with English, without specifying tessdata path
    if (api->Init("/usr/share/tesseract-ocr", "eng")) {
        fprintf(stderr, "Could not initialize tesseract.\n");
        exit(1);
    }
    
    // Open input image with leptonica library
    Pix *image = pixRead("/home/pi/opencv/sudoku_capture/sudoku2/Sudoku.jpg");
    api->TesseractRect( img.data, 1, img.step1(), 0, 0, img.cols, img.rows);
    api->SetImage((uchar*)img.data, img.size().width, img.size().height, img.channels(), img.step1());        //image);
    api->Recognize(0);
    
    // Get OCR result
    outText = api->GetUTF8Text();
    printf("OCR output:\n%s", outText);

    // Destroy used object and release memory
    api->End();
    delete [] outText;
    //pixDestroy(&image);

Soumik Ranjan Dasgupta

unread,
Oct 11, 2018, 4:08:45 PM10/11/18
to tesser...@googlegroups.com
Although I've not used the C++ API, I've run into segmentation errors quite a lot during my initial days, and in most of the cases it generally stemmed from unauthorised directory accesses. Check whether the directory where the images are stored is readable and writable by you. Hope this helps!

--
You received this message because you are subscribed to the Google Groups "tesseract-ocr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tesseract-oc...@googlegroups.com.
To post to this group, send email to tesser...@googlegroups.com.
Visit this group at https://groups.google.com/group/tesseract-ocr.
To view this discussion on the web visit https://groups.google.com/d/msgid/tesseract-ocr/5248e483-3f5c-4834-9c31-341b6873ffe5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Regards,
Soumik Ranjan Dasgupta

Adam Richards

unread,
Oct 12, 2018, 2:52:23 AM10/12/18
to tesser...@googlegroups.com
Thanks  Soumik, I can access the original image but I guess it must be a problem with Tesseract accessing the OpenCV mats wherever they exist, which I'll have to try and work out. 

Is there a way to authorise access? I'm using cmake to compile on a raspberry pi

You received this message because you are subscribed to a topic in the Google Groups "tesseract-ocr" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/tesseract-ocr/LQ92oBFKBU8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to tesseract-oc...@googlegroups.com.

To post to this group, send email to tesser...@googlegroups.com.
Visit this group at https://groups.google.com/group/tesseract-ocr.

Soumik Ranjan Dasgupta

unread,
Oct 12, 2018, 7:22:14 AM10/12/18
to tesser...@googlegroups.com
No, there isn't a way to authorise access via Tesseract save the default way your OS provides.
Please copy paste the entire error log, it might help figuring out the exact problem.


For more options, visit https://groups.google.com/d/optout.

Zdenko Podobny

unread,
Oct 12, 2018, 2:29:07 PM10/12/18
to tesser...@googlegroups.com
If you are really interested in help, than please post complete code of your test case (+ how you compiled it)  including (link to) image you try to process. 

Zdenko


št 11. 10. 2018 o 12:39 Adam Richards <ajrich...@gmail.com> napísal(a):
--
Reply all
Reply to author
Forward
0 new messages