业余瞎搞,废话少说,上代码:
#pragma comment(lib, "libtesseract302.lib")
#pragma comment(lib, "liblept.lib")
#include <iostream>
#include <string>
#include <opencv.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <tesseract\baseapi.h>
#include <tesseract\strngs.h>
using namespace std;
cv::Mat image;
string windowsname = "image";
void OnTracker(int thres, void *user)
{
cv::threshold(image, image, thres, 2.5*thres,CV_THRESH_BINARY);
cv::imshow(windowsname, image);
tesseract::TessBaseAPI api;
api.Init(NULL, "eng", tesseract::OEM_DEFAULT); //识别英文
api.SetVariable( "tessedit_char_whitelist", "0123456789");//限制识别字符范围
api.SetImage(image.data,image.cols, image.rows, image.channels(), image.step);
if (0==api.Recognize(NULL))
{
STRING str = api.GetUTF8Text();
printf(str.string());
string strtemp = str.string();
while (1)
{
int k = strtemp.find_first_of(' ');
if (k<0||k>=strtemp.length())
{
break;
}
strtemp.erase(k,1);
}
cout << strtemp << endl;
while(1)
{
int k = strtemp.find_first_of('\n');
if (k<0||k>=strtemp.length())
{
break;
}
string temp = strtemp.substr(0,k);
cout << temp << endl;
if (temp.length() > 14)
{
cout << "the card numbers is :" << temp << endl;;
break;
}
strtemp.erase(0,k+1);
}
}
}
int main()
{
cv::namedWindow("image", 1);
image = cv::imread("test.jpg");
cv::cvtColor(image, image, CV_RGB2GRAY);
int thres = 100;
cv::createTrackbar("thres:", "image", &thres, 255, OnTracker);
cv::imshow(windowsname, image);
cv::waitKey(0);
return 0;
}
对数字3和8偶尔会出现检测不准确的现象