QT开发中文网

c++ OpenCV 汉字识别 简单demo使用麻烦了

以下是一个使用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的文档进行相应设置。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=705

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?