以下是一个使用OpenCV和Tesseract OCR库实现汉字识别的简单demo:
#include <opencv2/opencv.hpp>
#include <tesseract/baseapi.h>
using namespace cv;
using namespace std;
int main()
{
// 读入图像并灰度化
Mat img = imread("chinese.jpg");
if (img.empty())
{
cout << "Failed to read image" << endl;
return -1;
}
Mat gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
// 初始化OCR引擎
tesseract::TessBaseAPI ocr;
ocr.Init(NULL, "chi_sim", tesseract::OEM_DEFAULT);
ocr.SetPageSegMode(tesseract::PSM_SINGLE_CHAR);
// 提取每个字符并识别
vector<Rect> charBoxes;
vector<string> charTexts;
Mat thresholded;
threshold(gray, thresholded, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
findContours(thresholded.clone(), charBoxes, RETR_LIST, CHAIN_APPROX_SIMPLE);
for (const auto& box : charBoxes)
{
Mat charImg = thresholded(box);
// 调整字符大小以便识别
int targetHeight = 32;
float scale = static_cast<float>(targetHeight) / charImg.rows;
Mat resizedCharImg;
resize(charImg, resizedCharImg, Size(static_cast<int>(charImg.cols * scale), targetHeight));
// 识别字符
ocr.SetImage(resizedCharImg.data, resizedCharImg.cols, resizedCharImg.rows, 1, resizedCharImg.cols);
string text = ocr.GetUTF8Text();
charTexts.push_back(text);
}
// 显示结果
for (int i = 0; i < charBoxes.size(); ++i)
{
rectangle(img, charBoxes[i], Scalar(0, 255, 0), 2);
putText(img, charTexts[i], Point(charBoxes[i].x, charBoxes[i].y - 5),
FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 0, 255), 2);
}
imshow("Result", img);
waitKey();
return 0;
}
该程序使用“chi_sim”语言模型,可以识别简体中文字符。如果需要识别其他语言或字体,请根据Tesseract OCR的文档进行相应设置。