我遇到了同样的问题。我的解决方法是创建一个简单的 C++ 应用程序,它以 PNG 文件名作为参数并自动旋转/去歪斜。
我的代码是
#include <iostream>
#include <cmath>
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
using namespace std;
int main(int argc, char **argv)
{
if (argc != 2) {
cerr << "usage: " << argv[0] << " <image>\n";
exit(1);
}
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
// Initialize tesseract-ocr with English, without specifying tessdata path
if (api->Init(NULL, "eng")) {
cerr << "Could not initialize tesseract.\n";
exit(2);
}
const char* inputfile = argv[1];
tesseract::Orientation orientation;
tesseract::WritingDirection direction;
tesseract::TextlineOrder order;
float deskew_angle;
PIX *image = pixRead(inputfile);
if (image == NULL) {
cerr << "could not open " << inputfile << endl;
return -2;
}
api->SetPageSegMode(tesseract::PSM_AUTO_OSD);
api->SetImage(image);
api->Recognize(0);
tesseract::PageIterator* it = api->AnalyseLayout();
it->Orientation(&orientation, &direction, &order, &deskew_angle);
cout << "Orientation: " << orientation <<
"\nWritingDirection: " << direction <<
"\nTextlineOrder: " << order <<
"\nDeskew angle: " << deskew_angle << "\n";
PIX* pixd = NULL;
switch (orientation) {
case 0:
cout << "image in the correct position, nothing to do\n";
if (fabs(deskew_angle) > 0.0001f) {
cout << "deskewing...\n";
pixd = pixRotate(image, -deskew_angle, L_ROTATE_SHEAR, L_BRING_IN_WHITE, 0, 0);
}
break;
case 1:
cout << "rotating image by 270 degrees\n";
pixd = pixRotate90(image, -1);
if (deskew_angle > 0.0001f) {
cout << "deskewing...\n";
pixd = pixRotate(pixd, -deskew_angle, L_ROTATE_SHEAR, L_BRING_IN_WHITE, 0, 0);
}
break;
case 2:
cout << "rotating image by 180 degrees\n";
pixd = pixRotate180(NULL, image);
if (deskew_angle > 0.0001f) {
cout << "deskewing...\n";
pixd = pixRotate(pixd, -deskew_angle, L_ROTATE_SHEAR, L_BRING_IN_WHITE, 0, 0);
}
break;
case 3:
cout << "rotating image by 90 degrees\n";
pixd = pixRotate90(image, 1);
if (deskew_angle > 0.0001f) {
cout << "deskewing...\n";
pixd = pixRotate(pixd, -deskew_angle, L_ROTATE_SHEAR, L_BRING_IN_WHITE, 0, 0);
}
break;
}
pixDestroy(&image);
if (pixd != NULL) {
pixWrite(inputfile, pixd, IFF_PNG);
pixDestroy(&pixd);
}
return 0;
}
你可以编译它
g++ -o tesseract_fixposition tesseract_fixposition.cpp -llept -ltesseract
依赖项是 libtesseract 和 libleptonica。我使用 Tesseract 3.03 和 3.04 版本以及 Leptonica 1.72 进行了测试。我处理了几千张图片,没有发现任何错误的标识。
希望这会有所帮助!