Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Mat和Pix的不同细分结果 球门_C++_Opencv_Ocr_Tesseract_Leptonica - Fatal编程技术网

C++ Mat和Pix的不同细分结果 球门

C++ Mat和Pix的不同细分结果 球门,c++,opencv,ocr,tesseract,leptonica,C++,Opencv,Ocr,Tesseract,Leptonica,使用OpenCVMat与使用LeptonicaPix使用Tesseract进行OCR时获得相同的质量结果 环境 C++17、OpenCV 3.4.1、Tesseract 3.05.01、Leptonica 1.74.4、Visual Studio社区2017、Windows 10 Pro 64位 描述 我正在与Tesseract和OCR合作,发现了我认为是一种奇特的行为 这是我的输入图像: 这是我的代码: #include "stdafx.h" #include <iostream>

使用OpenCV
Mat
与使用Leptonica
Pix
使用Tesseract进行OCR时获得相同的质量结果

环境 C++17、OpenCV 3.4.1、Tesseract 3.05.01、Leptonica 1.74.4、Visual Studio社区2017、Windows 10 Pro 64位

描述 我正在与Tesseract和OCR合作,发现了我认为是一种奇特的行为

这是我的输入图像:

这是我的代码:

#include "stdafx.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>

#pragma comment(lib, "ws2_32.lib")

using namespace std;
using namespace cv;
using namespace tesseract;

void opencvVariant(string titleFile);
void leptonicaVariant(const char* titleFile);

int main()
{
    cout << "Tesseract with OpenCV and Leptonica" << endl;

    const char* titleFile = "raptor-companion-2.jpg";
    opencvVariant(titleFile);
    leptonicaVariant(titleFile);

    cout << endl;
    system("pause");
    return 0;
}

void opencvVariant(string titleFile) {

    cout << endl << "OpenCV variant..." << endl;

    TessBaseAPI ocr;
    ocr.Init(NULL, "eng");
    Mat image = imread(titleFile);
    ocr.SetImage(image.data, image.cols, image.rows, 1, image.step);

    char* outText = ocr.GetUTF8Text();
    int confidence = ocr.MeanTextConf();

    cout << "Text: " << outText << endl;
    cout << "Confidence: " << confidence << endl;
}

void leptonicaVariant(const char* titleFile) {

    cout << endl << "Leptonica variant..." << endl;

    TessBaseAPI ocr;
    ocr.Init(NULL, "eng");
    Pix *image = pixRead(titleFile);
    ocr.SetImage(image);

    char* outText = ocr.GetUTF8Text();
    int confidence = ocr.MeanTextConf();

    cout << "Text: " << outText << endl;
    cout << "Confidence: " << confidence << endl;
}
从上面的输出可以看出,
Pix
变量比
Mat
变量的结果要好得多。由于我的代码在OCR之前严重依赖OpenCV进行计算机视觉,所以OCR对我来说非常重要,它可以很好地与OpenCV及其类配合使用

问题
  • 为什么
    Pix
    Mat
    给出更好的结果,反之亦然
  • 如何改变算法,使
    Mat
    变量与
    Pix
    变量一样高效

OpenCV
imread
函数默认读取彩色图像,这意味着您获得的像素为
bgrbgr…

在您的示例中,假设opencv图像是灰度的,因此有两种方法可以解决此问题:

  • 根据opencv图像中的通道数更改
    SetImage

    ocr.SetImage((uchar*)image.data,image.size().width,simageb.size().height,image.channels(),image.step1())

  • 使用1个通道将opencv图像转换为灰度

    cv::cvt颜色(图像,图像,cv_bgr2灰色)


  • 能否尝试用
    ocr.setImage((uchar*)image.data,image.size().width,simageb.size().height,image.channels(),image.step1())替换opencv setImage。默认情况下,imread将读取彩色图像(即使它看起来像黑白的),所以您可能只给tessereact
    blue
    channel颜色Mat@DmitriiZ. 我将其替换为
    ocr.SetImage((uchar*)image.data,image.size().width,image.size().height,image.channels(),image.step1())现在我得到了相同的结果!你应该写下你的评论作为回答!:-)
    
    OpenCV variant...
    Text: Rapton
    
    
    Confidence: 68
    
    Leptonica variant...
    Text: Raptor Companion
    
    
    Confidence: 83