1. 程式人生 > 實用技巧 >c++ 呼叫opencv+tesseract做圖片文字識別

c++ 呼叫opencv+tesseract做圖片文字識別

2020年12月28日22:08:08

環境:windows10 vscode cmake vcpkg

vcpkg installopencv

vcpkg installtesseract

參考手冊

tesseract-ocr demo https://github.com/tesseract-ocr/tessdoc/blob/master/APIExample.md opencv https://docs.opencv.org/master/index.html https://www.w3cschool.cn/opencv/opencv-p2hd2ccj.html http://www.woshicver.com/ https://apachecn.gitee.io/opencv-doc-zh/#/

注意網路不好的話使用

github下載加速網站 http://toolwa.com/github/ https://gh.api.99988866.xyz/ https://shrill-pond-3e81.hunsh.workers.dev/ 1,語言資料 https://github.com/tesseract-ocr/langdata 2,訓練過的資料 Tesseract 4.0x +添加了基於LSTM神經網路的新OCR引擎。最初它可以在x86 / Linux上正常執行(很好),並具有適用於100多種語言和35多種指令碼的官方語言模型資料。有關各種型別的模型的詳細資訊,請參見資料檔案。 4.00可從tessdata標記為4.00的版本獲得模型檔案。它具有從2016年11月開始的模型。可從以下連結獲得各個語言檔案的連結。 tessdata 4.00 2016年11月 4.0.0可從標記為4.0.0的tessdata獲得用於版本和更高版本的模型檔案。它具有2017年9月以來的舊模型,並已使用tessdata_bestLSTM模型的Integer版本進行了更新。這套訓練有素的資料檔案同時支援帶有--oem 0和的LSTM模型的舊式識別器--oem 1。 這些模型可從以下Github儲存庫中獲得。 tessdata official以下Github儲存庫中提供了另外兩組在Google培訓下的受訓練資料。這些沒有舊模型,只有可用於的LSTM模型--oem 1。 tessdata_best tessdata_fast 建議使用Google的訓練好的資料 https://github.com/tesseract-ocr/tessdata_best

tessdata檔案下放入

chi_sim.traineddata

chi_sim_vert.traineddata

eng.traineddata

邏輯很簡單,通過opencv是降噪圖片資料,給tesseract-ocr來讀取資料,但是寫的比較簡單,所以識別度比較低,如果需要提高識別率,需要好好除錯

#define _CRT_SECURE_NO_DEPRECATE

#include "main.h"
#include <stdio.h>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <leptonica/allheaders.h>
#include 
<tesseract/baseapi.h> #include <tesseract/publictypes.h> #include <opencv2/imgproc.hpp> using namespace cv; int main() { std::string image_name = "3.jpg"; Mat imageMat; imageMat = imread(image_name); if (imageMat.data == nullptr) { printf("No image data \n"); return -1; } Mat blurMat; // 影象模糊 // cv::medianBlur(imageMat, blurMat, 5); // 灰度圖 Mat z1, z2; cv::cvtColor(imageMat, z1, cv::COLOR_BGR2GRAY); // 閾值 // cv::threshold(z1, z2, 214, 255, cv::THRESH_BINARY); // 自動降噪 cv::adaptiveThreshold(z1, g_grayImage, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 7, 25); // 顯示圖片 namedWindow("Image1", WINDOW_AUTOSIZE); imshow("Image1", g_grayImage); //waitKey(0); //控制檯中文異常 std::system("chcp 65001"); char *outText; tesseract::TessBaseAPI api; // Initialize tesseract-ocr with English, without specifying tessdata path if (api.Init(NULL, "chi_sim")) { std::cout << stderr << std::endl; exit(1); } // 讀取圖片路徑 // Pix *image = pixRead("3.jpg"); api.SetImage((uchar *)g_grayImage.data, g_grayImage.cols, g_grayImage.rows, 1, g_grayImage.cols); // Get OCR result outText = api.GetUTF8Text(); if (outText == nullptr) { std::cout << "沒有資料" << std::endl; } // printf("OCR output:\n%s", outText); std::cout << outText << std::endl; // Destroy used object and release memory api.End(); // delete api; delete[] outText; // pixDestroy(&image); return 0; }

結果:

Active code page: 65001
Warning: Invalid resolution 0 dpi. Using 70 instead.
文 華 財經 ( 編輯 整理 李 志 偉 ) -- 以 下 為 12 月 24 日 倫敦 金屬 交易 所 (LME) 庫 存 變化 情況 ( 單位 :
噸 )
3
錫 | 136100 |! -3275|!1 -2.745
得 “| 1372350 | 1 -5750|1 -0.423
非 205900 |! -325|1 -0.165
246654 | 人 +336|T +0.149%
133850 |T +75|T +0.065%
轎 2105 一 -625|1 -22.89%
外 S 人 | 4200 | -4300|1 -370%

opencv還提供簡單的除錯工具

#define _CRT_SECURE_NO_DEPRECATE

#include "main.h"
#include <stdio.h>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <leptonica/allheaders.h>
#include <tesseract/baseapi.h>
#include <tesseract/publictypes.h>
#include <opencv2/imgproc.hpp>

using namespace cv;

#define WINDOW_NAME "視窗"

int g_nThresholdValue = 100;
int g_nThresholdType = 3;

void on_Threshold(int, void *);
Mat g_srcImage, g_grayImage, g_dstImage;

int main()
{

    std::string image_name = "3.jpg";
    Mat imageMat;
    imageMat = imread(image_name);
    if (imageMat.data == nullptr)
    {
        printf("No image data \n");
        return -1;
    }
    Mat blurMat;
    // 影象模糊
    // cv::medianBlur(imageMat, blurMat, 5);
    // 灰度圖
    Mat z1, z2;
    cv::cvtColor(imageMat, g_grayImage, cv::COLOR_BGR2GRAY);
    
    //除錯模式
    namedWindow(WINDOW_NAME);
    createTrackbar("模式", WINDOW_NAME, &g_nThresholdType, 4, on_Threshold);
    createTrackbar("引數值", WINDOW_NAME, &g_nThresholdValue, 255, on_Threshold);
    on_Threshold(0, 0);
    waitKey(0);
    return 0;
}

void on_Threshold(int, void *)
{
    threshold(g_grayImage, g_dstImage, g_nThresholdValue, 255, g_nThresholdType);
    imshow(WINDOW_NAME, g_dstImage);
}