调整的好的话 测试准确率80%以上
int main(int argc, char** argv)
{
// Usage: tesscv image.png
system("chcp 65001");
string path = "D:/Image/8/";
vector<string> files;
//getAllFiles(path, files);
//for (auto file : files)
// {
// Load image
cv::Mat im = cv::imread("21.jpg");
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 here...
Mat threImage;
threshold(gray, threImage,150, 255, THRESH_BINARY_INV);
Mat horizontalStructure = getStructuringElement(MORPH_RECT, Size(15,2));
dilate(threImage, threImage, horizontalStructure, Point(-1, -1));
//imshow("阈值处理", threImage);
//waitKey(0);
//初始化模型
tesseract::TessBaseAPI tess;
tess.Init(NULL, "Japanese", tesseract::OEM_DEFAULT);
tess.SetPageSegMode(tesseract::PSM_SINGLE_BLOCK);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(threImage, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));
vector<Rect> boundRect(contours.size());
for (int i = 0; i < contours.size(); i++)
{
if (contourArea(contours[i])>5000 || contourArea(contours[i]) < 100)
{
continue;
}
else
{
boundRect[i] = boundingRect(Mat(contours[i]));
//rectangle(im, boundRect[i].tl(), boundRect[i].br(), (0, 0, 255), 1, 8, 0);
Mat temp = gray(Rect(boundRect[i].x, boundRect[i].y-2, boundRect[i].width, boundRect[i].height+4));
//
copyMakeBorder(temp, temp, 5, 5, 5, 5, BORDER_REFLECT_101);
//threshold(temp,temp,0,255,THRESH_OTSU);
//copyMakeBorder(temp, temp, 5, 5, 5, 5, BORDER_CONSTANT,Scalar(255));
//imshow("temp", temp);
//waitKey(0);
tess.SetImage((uchar*)temp.data, temp.cols, temp.rows, 1, temp.cols);
char* out = tess.GetUTF8Text();
std::cout << out << std::endl;
imshow("temp", temp);
waitKey(0);
//putTextZH(im, out, Point(boundRect[i].x, boundRect[i].y - 10), Scalar(255, 0, 0),20, "华文行楷");
}
}
//imwrite("srcImage.jpg", im);
cout << "suucess" << endl;
waitKey(0);
#if 0
// Pass it to Tesseract API
tesseract::TessBaseAPI tess;
tess.Init(NULL, "Japanese", tesseract::OEM_DEFAULT);
tess.SetPageSegMode(tesseract::PSM_SINGLE_BLOCK);
tess.SetImage((uchar*)threImage.data, threImage.cols, threImage.rows, 1, threImage.cols);
// Get the text
char* out = tess.GetUTF8Text();
std::cout << out << std::endl;
#endif
//}
return 0;
}