Skip to content

Instantly share code, notes, and snippets.

@noelyahan

noelyahan/my_ocr.cpp

Created Jan 29, 2016
Embed
What would you like to do?
#include <stdio.h>
#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/flann/miniflann.hpp"
#include <tesseract/baseapi.h>
using namespace cv;
using namespace std;
using namespace cv::flann;
// compile >> g++ <file.cpp> -o app `pkg-config --cflags --libs opencv` -ltesseract
// app "image.jpg"
int main( int argc, char** argv ) {
Mat img = imread(argv[1]);
cvtColor(img,img,CV_BGR2GRAY);
cv::Mat img_bw;
cv::threshold(img, img_bw, 128.0, 255.0, THRESH_BINARY);
bitwise_not ( img_bw, img_bw );
tesseract::TessBaseAPI tess;
tess.Init(NULL, "eng", tesseract::OEM_DEFAULT);
tess.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
tess.SetPageSegMode(tesseract::PSM_SINGLE_BLOCK);
tess.SetImage((uchar*)img_bw.data, img_bw.cols, img_bw.rows, 1, img_bw.cols);
char* out = tess.GetUTF8Text();
std::cout << out << std::endl;
imshow("img",img_bw);
waitKey(0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.