0

I know this theme already exists, but I didn't find any solution for this. I am trying to detect characters from picture in this code below:

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

#include <sstream>
#include <memory>
#include <iostream>

#define path "/home/jovan/Pictures/"

void resize(cv::Mat &img);
PIX *mat8ToPix(const cv::Mat *mat8);
cv::Mat pix8ToMat(PIX *pix8);

int main(int argc, char **argv)
{
    // Load image
    std::stringstream ss;
    ss << path;
    ss << argv[1];
    cv::Mat im = cv::imread(ss.str() );
    if (im.empty())
    {
        std::cout<<"Cannot open source image!" << std::endl;
        return EXIT_FAILURE;
    }
    resize(im);

    cv::Mat gray;
    cv::cvtColor(im, gray, CV_BGR2GRAY);

    // Pass it to Tesseract API
    tesseract::TessBaseAPI tess;
    tess.Init(NULL, "eng", tesseract::OEM_DEFAULT);
    tess.SetPageSegMode(tesseract::PSM_SINGLE_BLOCK);
    tess.SetVariable("tessedit_char_whitelist", "QWERTYUIOPASDFGHJKLZXCVBNM");

    PIX *image = mat8ToPix(&im);

    //tess.SetImage((uchar*)gray.data, gray.cols, gray.rows, 1, gray.cols);
    tess.SetImage(image);

    // Get the text
    char* out = tess.GetUTF8Text();
    if(out != nullptr)
        std::cout << "here it is: "<< out << std::endl;

    cv::imshow("image", im);
    cv::imshow("gray", gray);
    cv::waitKey();

    return 0;
}

void resize(cv::Mat &img)
{
    while(img.size().width >= 500 && img.size().height >= 500 )
        cv::resize(img, img, cv::Size(img.size().width/2, img.size().height/2) );   
}

PIX *mat8ToPix(const cv::Mat *mat8)
{
    PIX *pixd = pixCreate(mat8->size().width, mat8->size().height, 8);
    for(int y=0; y<mat8->rows; y++) 
        for(int x=0; x<mat8->cols; x++) 
            pixSetPixel(pixd, x, y, (l_uint32) mat8->at<uchar>(y,x));

    return pixd;
}

cv::Mat pix8ToMat(PIX *pix8)
{
    cv::Mat mat(cv::Size(pix8->w, pix8->h), CV_8UC1);
    uint32_t *line = pix8->data;
    for (uint32_t y = 0; y < pix8->h; ++y) 
    {
        for (uint32_t x = 0; x < pix8->w; ++x) 
            mat.at<uchar>(y, x) = GET_DATA_BYTE(line, x);
        line += pix8->wpl;
    }
    return mat;
}

whatever picture I put to process I get this on terminal:

$: Warning: Invalid resolution 0 dpi. Using 70 instead.

Does anyone have some solution?

Thanks in advance.

3 Answers3

3

If you know the input image's resolution, you can call pixSetResolution on Leptonica Pix object.

Or use Tesseract API to pass in the value. See Tess4j - Pdf to Tiff to tesseract - "Warning: Invalid resolution 0 dpi. Using 70 instead."

nguyenq
  • 8,212
  • 1
  • 16
  • 16
0

Maybe it helps: I used EMGU & C#, but I think it must be the same in C++:

ocr.SetVariable("user_defined_dpi", "70");

... and the message should disappear ;)

0

I had similar issue. Found out from here that dark background in the image is the problem. Inversion of the image colors worked.

Selva Prakash
  • 154
  • 11