码迷,mamicode.com
首页 > 其他好文 > 详细

银行卡的数字检测

时间:2014-06-10 18:17:02      阅读:376      评论:0      收藏:0      [点我收藏+]

标签:数字识别   opencv   

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

#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

银行卡的数字检测

标签:数字识别   opencv   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!