Tesseract OCR+OpenCV识别二进制壁纸照片效果不佳求C++改进方案
二进制壁纸拍摄照片的OCR识别优化请求
我正在开发一个个人项目,需要识别二进制壁纸的拍摄照片。目前用Tesseract OCR结合OpenCV能正常识别文本截图,但对拍摄的壁纸照片识别效果极差,输出存在不完整、错误的情况。希望在C++环境下获得改进建议,也可尝试其他库,最终要将识别结果解析转换为ASCII。
当前预处理及识别代码
#include <string> #include <fstream> #include <vector> #include <iostream> #include "basicOCR.h" using namespace std; using namespace cv; void getText(string imPath, string outPath) { char *outText; ofstream outputFile(outPath); double count, avg, lowest = 100; Mat im = cv::imread(imPath); if (im.empty()) { cerr << "Error reading image file" << endl; return; } Mat imGray, imDenoised,imThresholded, imInverted; fastNlMeansDenoising(im, imDenoised); cvtColor(imDenoised, imGray, COLOR_BGR2GRAY); threshold(imGray, imThresholded, 0, 255, THRESH_BINARY_INV | THRESH_OTSU); // bitwise_not(imThresholded, imInverted); cv::imshow("idk", im); waitKey(0); cv::imshow("idk", imDenoised); waitKey(0); cv::imshow("idk", imGray); waitKey(0); cv::imshow("idk", imThresholded); waitKey(0); //cv::imshow("idk", imInverted); //waitKey(0); tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI(); if (api->Init(NULL, "eng", tesseract::OEM_LSTM_ONLY)) { cerr << "Could not initialize tesseract " << endl; exit(1); } api->SetPageSegMode(tesseract::PSM_AUTO); api->SetVariable("tessedit_char_whitelist", "01 "); api->SetImage(imThresholded.data, imThresholded.cols, imThresholded.rows, imThresholded.channels(), imThresholded.step); api->Recognize(0); tesseract::ResultIterator *ri = api->GetIterator(); tesseract::PageIteratorLevel level = tesseract::RIL_TEXTLINE; if (ri != 0) { do { const char *word = ri->GetUTF8Text(level); float conf = ri->Confidence(level); int x1, y1, x2, y2; ri->BoundingBox(level, &x1, &y1, &x2, &y2); // outputFile << word << endl; outputFile << word << "conft :" <<conf << "box: " << x1 << ", "<< y1 <<", "<< x2 <<", "<< y2<<endl; count++; avg += conf; if (conf < lowest) { lowest = conf; } delete[] word; } while (ri->Next(level)); } avg = avg / count; //cout << "average: " << avg << " lowest: " << lowest << " count: " << count << endl; // Destroy used object and release memory api->End(); delete api; delete[] outText; outputFile.close(); }
测试示例
拍摄的壁纸照片(识别失败)
图片1
- 原图:

- 预处理后:

- 错误输出:

图片2
- 原图:

- 预处理后:

- 错误输出:

文本截图(识别成功)
- 原图:

- 预处理后:

- 识别输出:
01010111011010000110010101110010011001010110000101110011001000000100100100101100001000000101010001101000011011 conft :88.0777box: 12, 12, 2008, 38 11011100110010111000100000010001110010111000100000010000110110110001100101011011010111001101101111011011100010110 conft :84.2204box: 12, 52, 2015, 78 ...
该输出可正常转换为ASCII。
内容的提问来源于stack exchange,提问作者Macto24
相关产品推荐
相关产品推荐

