Beispiel #1
0
void testtess() {
	cv::Mat img = imread("phototest.png");
	if(img.empty()) { cerr << "noup." << endl; return; }
	imshow("blah",img);
	waitKey(1);

	cout << "photo " << img.size() << endl;

	int rc = api.Init(tessdata_dir.c_str(), "ara", tesseract::OEM_DEFAULT);
	if (rc) {
		cerr << "Could not initialize tesseract.\n";
		exit(1);
	}

	api.SetPageSegMode(tesseract::PSM_AUTO);

	printf("Tesseract Open Source OCR Engine v%s with Leptonica\n",
		   tesseract::TessBaseAPI::Version());
	printf("Init languages %s\n",api.GetInitLanguagesAsString());

    Mat tmp; cvtColor(img, tmp, CV_BGR2GRAY);
    adaptiveThreshold(tmp, tmp, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 51, 35);
    imshow("eqhist", tmp);

//    cvtColor(tmp, out(Rect(0,0,r.width,r.height)), CV_GRAY2BGR);

    char* cstr = api.TesseractRect(tmp.data, tmp.channels(), tmp.cols*tmp.channels(), 0, 0, tmp.cols, tmp.rows);

    cout << cstr << endl;

    delete[] cstr;

    waitKey(0);
}
Beispiel #2
0
pair<int,string> TesseractBridge::processEx(const Mat& tmp, Rect& r) {
    api.Clear();
    
    char* cstr = api.TesseractRect(tmp.data, tmp.channels(), tmp.cols*tmp.channels(), 0, 0, tmp.cols, tmp.rows);
//    cout << cstr << endl;
    delete[] cstr;
    
    tesseract::ResultIterator* ri = api.GetIterator();
    tesseract::PageIteratorLevel level = tesseract::RIL_WORD;
    if (ri != 0) {
        const char* word = ri->GetUTF8Text(level);
        
        if(!word) return make_pair(0, "");
        
        float conf = ri->Confidence(level);
        int x1, y1, x2, y2;
        ri->BoundingBox(level, &x1, &y1, &x2, &y2);
//        printf("word: '%s';  \tconf: %.2f; BoundingBox: %d,%d,%d,%d;\n",
//               word, conf, x1, y1, x2, y2);
        string theword(word);
        delete[] word;
        
        bool allnonalphanom = true;
        for (int i=0; i<theword.size(); i++) {
            allnonalphanom = allnonalphanom && !(isalnum(theword[i]) || theword[i] == '"' || theword[i] == '.' || theword[i] == ',' || theword[i] == '?' || theword[i] == '!' || theword[i] == '\'');
        }
        if (allnonalphanom) return make_pair(0, "");
        
        r.x += x1;
        r.y += y1;
        r.width = x2-x1;
        r.height = y2-y1;
        return make_pair((int)conf, theword);
    } else
        return make_pair(0, "");
}