Call tesseract__c++ in C + + and OpenCV

Source: Internet
Author: User

Reference address
Tesseract with C + + or OpenCV and some code trace 1.Tesseract Direct use in C + +

Tess.cpp: 
//Recognize text on a image using Tesseract API and print it to the screen
//Usage:./tess image.p ng

#include <tesseract/baseapi.h>
#include <tesseract/strngs.h>
#include <iostream>

int main (int argc, char** argv)
{
    if (argc!= 2)
    {
        std::cout << "Please specify the input image!" << Std::endl;
        return-1;
    }

    Const char* LANG = "eng";
    Const char* filename = argv[1];

    New Tess base class
    Tesseract::tessbaseapi Tess;
    Initialize
    Tess. Init (NULL, Lang, tesseract::oem_default);
    Set the recognition mode
    Tess. Setpagesegmode (tesseract::P sm_single_block);

    file* fin = fopen (filename, "RB");
    if (fin = = NULL)
    {
        std::cout << "Cannot open" << filename << std::endl;
        return-1;
    }
    Fclose (Fin);

    STRING text;
    To identify
    if (!tess. Processpages (filename, NULL, 0, &text))
    {
        std::cout << "Error during processing." << std:: Endl;
        return-1;
    }
    else
        std::cout << text.string () << Std::endl;

    return 0;
}

Ubuntu Compile command

g++-i/usr/local/include ' pkg-config--cflags--libs tesseract ' Tess.cpp-o Tess

The principal functions for OCR recognition are:

TESSERACT::TESSBASEAPI::
bool Tessbaseapi::P rocesspage (pix* Pix, int page_index, const char* filename,
                              const char* retry_config, int timeout_millisec,
                              string* text_out)

In the analysis process, focus on this API, location in Baseapi.cpp;

follow-up analysis; 2. Tesseract used in conjunction with OPENCV

You can select ROI and then OCR;

In order to identify the Cv::mat matrix, it is passed to Tessbaseapi::setimage ();

Use Tessbaseapi::getutf8text () to get the identified text;

Tesscv.cpp://Using tesseract API with OpenCV #include <opencv2/highgui/highgui.hpp> #include &LT;OPENCV2/IMGPR oc/imgproc.hpp> #include <tesseract/baseapi.h> #include <iostream> int main (int argc, char** argv) {/ /USAGE:TESSCV Image.png if (argc!= 2) {std::cout << "Please specify the input image!" << s
        Td::endl;
    return-1;
    }//Load image Cv::mat im = Cv::imread (argv[1]);
        if (Im.empty ()) {std::cout << "cannot open source image!" << Std::endl;
    return-1;
    } Cv::mat Gray;
    Cv::cvtcolor (IM, Gray, cv_bgr2gray);
    ... other image pre-processing ...//pass it to tesseract API Tesseract::tessbaseapi Tess; Tess.
    Init (NULL, "Eng", Tesseract::oem_default); Tess.
    Setpagesegmode (tesseract::P sm_single_block); Tess.

    SetImage ((uchar*) Gray.data, Gray.cols, Gray.rows, 1, gray.cols); Get the text char* out = Tess.
    Getutf8text (); Std: cout << out << Std::endl;
return 0; }

Ubuntu Compile command

g++-i/usr/local/include ' pkg-config--cflags--libs opencv tesseract ' Tesscv.cpp-o TESSCV

over!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.