Ubuntu tesseract使用全是干货

本文介绍了如何在C++中利用OpenCV进行图像预处理,如二值化、高斯模糊和阈值化,然后使用Google的libtesseract库识别中文文本,以实现文字检测功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文字检测

方案

利用opencv二值化处理。最后检测使用google的开源库libtesseract识别文字。

tesseract安装

apt install libtesseract-dev
# 前面那个是英文,后面那个是中文
apt install tesseract-oct tesseract-ocr-chi-sim

手册

手册

qt creator中使用

pro文件中加入下面内容

CONFIG += link_pkgconfig
PKGCONFIG += tesseract opencv4 lept

使用

// 前面是opencv的内容,不明白请自行学习
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>

using namespace std;
using namespace cv;

static char* get_picture_text(char *file_name);

int main(int argc, char **argv)
{
    Mat srcImage = imread("../../image/chineseTest.png", 1);
    if(!srcImage.data)
    {
        cout<<"Open chineseTest error!!!"<<endl;
        return -1;
    }

    Mat imageGuussian;
    GaussianBlur(srcImage, imageGuussian, Size(3,3), 0);

    Mat thresholdImage;
    threshold(imageGuussian, thresholdImage, 150, 255, ADAPTIVE_THRESH_GAUSSIAN_C);

    Mat distImage;
    erode(thresholdImage, distImage, Mat::ones(5,5,CV_8U));
    dilate(distImage, distImage, Mat::ones(5,5,CV_8U));

    char *temp_file = "temp_file.png";
    imwrite(temp_file, distImage);
    cout<<"Get the image text:"<<get_picture_text(temp_file)<<endl;
    while(waitKey(-1) != 27);

    return 0;
}

static char* get_picture_text(char *file_name)
{
    char *ret_text;

    tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();

    // 英文用"eng",中文简体用"chi_sim"
    if(api->Init(NULL, "chi_sim"))
    {
        cout<<"Couldn't init tesseract!"<<endl;
        return NULL;
    }
    Pix *image = pixRead(file_name);
    api->SetImage(image);
    ret_text = api->GetUTF8Text();
    api->End();
    delete api;
    pixDestory(&image);


    return ret_text;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值