首页 > 其他 > 详细

银行卡的数字检测

时间:2014-06-10 18:16:44      阅读:404      评论:0      收藏:0      [点我收藏+]

业余瞎搞,废话少说,上代码:

#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;
}


检测结果如下:


bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

对数字3和8偶尔会出现检测不准确的现象

bubuko.com,布布扣bubuko.com,布布扣

银行卡的数字检测,布布扣,bubuko.com

银行卡的数字检测

原文:http://blog.csdn.net/yangbingzhou/article/details/29826827

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!