处理tesseract ocr的暗文本图像

时间:2015-09-19 07:28:27

标签: ocr tesseract leptonica python-tesseract

我注意到每当我使用深色背景图像进行tesseract时,我都会遇到分段错误。我试图使用此代码提取符号

#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
#include <iostream>
#include <map>
#include <bits/stdc++.h>
using namespace std;
int main()
{
    char *outText;
    map<pair<char*,char*>,float> matrix;
    set<char> allChars;
    tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
    // Initialize tesseract-ocr with English, without specifying tessdata path
    if (api->Init(NULL, "eng")) {
        fprintf(stderr, "Could not initialize tesseract.\n");
        exit(1);
    }

   int a[256][256];
   for(int i=0;i<256;i++){
     for(int j=0;j<256;j++){
       a[i][j]=0;
     }
   }
    // Open input image with leptonica library
    string images[] = {List of images};
for (int ii=0;ii<7;ii++){
  Pix *image = pixRead((const char*) images[ii].c_str());
  cout << images[ii] << endl;
   api->Init(NULL, "eng");
   api->SetImage(image);
   string valid_set = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890~`!@#$%^&*()_-+=,./<>/:;'[]{}|";
   api->SetVariable("tessedit_char_whitelist", valid_set.c_str());
   api->SetVariable("save_blob_choices", "T");
   //api->SetRectangle(37, 128,648, 523);
   //api->SetRectangle(30, 86, 590, 100);
   //api->SetRectangle(30,100,430,30);
   api->Recognize(NULL);

   tesseract::ResultIterator* ri = api->GetIterator();
   tesseract::PageIteratorLevel level = tesseract::RIL_SYMBOL;
   if(ri != 0) {
       do {
           const char* symbol = ri->GetUTF8Text(level);
           //cout << symbol << endl;
           if(ri!=0){
             float conf = ri->Confidence(level);
           }
           //cout << "err" << endl;
           if(symbol != 0) {
              //printf("symbol %s, conf: %f", symbol, conf);
               bool indent = false;
               tesseract::ChoiceIterator ci(*ri);
               do {
                   const char* choice = ci.GetUTF8Text();
                   if (indent) //printf("\t\t ");
                  // printf("\t- ");
                  //cout << symbol<<" Look up "<<choice << endl;
                  matrix[make_pair(strdup(symbol), strdup(choice))]=ci.Confidence();
                   //printf("%s conf: %f\n", choice, ci.Confidence());
                   indent = true;
               } while(ci.Next());
           }
           //printf("---------------------------------------------\n");
           delete[] symbol;
       } while((ri->Next(level)));
   }

   int count = 0;
   for(map<pair<char*,char*>,float>::iterator it = matrix.begin();it!=matrix.end();it++){
      allChars.insert((strdup)(it->first.first)[0]);
      allChars.insert((strdup)(it->first.second)[0]);
      //cout<<it->first.first<<" "<<it->first.second<<endl;
      //cout << (strdup)(it->first.first)[0]<<" "<<(strdup)(it->first.second)[0]<<endl;
      a[(strdup)(it->first.first)[0]][(strdup)(it->first.second)[0]]+=it->second;
      count++;
   }
  // cout << count << endl;
   for(set<char>::iterator it = allChars.begin();it!=allChars.end();it++){
     //cout << *it << endl;
   }
   for(int i=0;i<256;i++){
     for(int j=0;j<256;j++){
       if(a[i][j]!=0){
        ///cout << i << " " <<j<<endl;
        //cout << a[i][j]<<endl;
       }
       //cout << a[i][j] << endl;
     }
   }

   api->End();
    pixDestroy(&image);
}
    return 0;

}`

具体在代码中

float conf = ri->Confidence(level);

那么这个问题的解决方案是什么?我们应该训练更黑暗的图像吗?

编辑: 示例图片enter image description here

0 个答案:

没有答案