本文将使用opencv c++ 进行银行卡号识别。主要步骤可以细分为:
1、 获取模板图像
2、银行卡号区域定位
3、字符切割
4、模板匹配
5、效果显示
接下来就具体看看是如何一步步实现的吧。
如图所示,这是我们的模板图像。我们需要将上面的字符一一切割出来保存,以便进行后续的字符匹配环节。先进行图像灰度、阈值等操作进行轮廓提取,这里就不再细说。这里我想说的是,由于经过轮廓检索,提取出来的字符并不是按(0、1、2…7、8、9)顺序排列,所以,在这里我自定义了一个card结构体,用于图像排序。具体请看源码。
如图为顺序切割出来的模板字符。
bool get_template(mat temp, vector<card>&card_temp){ //图像预处理 mat gray; cvtcolor(temp, gray, color_bgr2gray); mat thresh; threshold(gray, thresh, 0, 255, thresh_binary_inv|thresh_otsu); //轮廓检测 vector <vector<point>> contours; findcontours(thresh, contours, retr_external, chain_approx_simple); for (int i = 0; i < contours.size(); i++) { rect rect = boundingrect(contours[i]); double ratio = double(rect.width) / double(rect.height); //筛选出字符轮廓 if (ratio > 0.5 && ratio < 1) { /*rectangle(temp, rect, scalar(0, 255, 0));*/ mat roi = temp(rect); //将字符扣出,放入card_temp容器备用 card_temp.push_back({ roi ,rect }); } } if (card_temp.empty())return fal; //进行字符排序,使其按(0、1、2...7、8、9)顺序排序 for (int i = 0; i < card_temp.size()-1; i++) { for (int j = 0; j < card_temp.size() - 1 - i; j++) { if (card_temp[j].rect.x > card_temp[j + 1].rect.x) { card temp = card_temp[j]; card_temp[j] = card_temp[j + 1]; card_temp[j + 1] = temp; } } } return true;}
如图所示,这是本案例需要识别的银行卡。从图中可以看出,我们需要将银行卡号切割出来首先得将卡号分为4个小块切割,之后再需要将每一小块上的字符切割。接下来一步步看是如何操作的。
首先第一步得先进行图像预处理,通过灰度、二值化、形态学等操作提取出卡号轮廓。这里的图像预处理需要根据图像特征自行确定,并不是所有的步骤都是必须的,我们最终的目的是为了定位银行卡号所在轮廓位置。这里我使用的是二值化、以及形态学闭操作。
//形态学操作、以便找到银行卡号区域轮廓 mat gray;水中画 cvtcolor(src, gray, color_bgr2gray); mat gaussian; gaussianblur(g赐金放还ray, gaussian, size(3, 3), 0); mat thresh; threshold(gaussian, thresh, 0, 255, thresh_binary | thresh_otsu); mat clo; mat kernel2 = getstructuringelement(morph_rect, size(15, 5)); morphologyex(thresh, clo, morph_clo, kernel2);
经过灰度、阈值、形态学操作后的图像如下图所示。我们已经将银行卡号分为四个小矩形块,接下来只需通过轮廓查找、筛选就可以扣出这四个roi区域了。
vector<vector<point>>contours; findcontours(clo, contours, retr_external, chain_approx_simple); for (int i = 0; i < contours.size(); i++) { //通过面积、长宽比筛选出银行卡号区域 double area = contourarea(contours[i]); if (area > 800 && area < 1400) { rect rect = boundingrect(contours[i]); float ratio = double(rect.width) / double(rect.height); if (ratio > 2.8 && ratio < 3.1) { mat roi = src(rect); block_roi.push_back({ roi ,rect }); } } }
同理,我们需要将切割下来的小块按照它原来的顺序存储。
for (int i = 0; i < block_roi.size()-1; i++) { for (int j = 0; j < block_roi.size() - 1 - i; j++) { if (block_roi[j].rect.x > block_roi[j + 1].rect.x) { card temp = block_roi[j]; block_roi[j] = block_roi[j + 1]; block_roi[j + 1] = temp; } } }
2.1.1 功能效果
2.1.2 功能源码
bool cut_block(mat src, vector<card>&block_roi){ //形态学操作、以便找到银行卡号区域轮廓 mat gray; cvtcolor(src, gray, color_bgr2gray); mat gaussian; gaussianblur(gray, gaussian, size(3, 3), 0); mat thresh; threshold(gaussian, thresh, 0, 255, thresh_binary | thresh_otsu); mat clo; mat kernel2 = getstructuringelement(morph_rect, size(15, 5)); morphologyex(thresh, clo, morph_clo, kernel2); vector<vector<point>>contours; findcontours(clo, contours, retr_external, chain_approx_simple); for (int i = 0; i < contours.size(); i++) { //通过面积、长宽比筛选出银行卡号区域 double area = contourarea(contours[i]); if (area > 800 && area < 1400) { rect rect = boundingrect(contours[i]); float ratio = double(rect.width) / double(rect.height); if (ratio > 2.8 && ratio < 3.1) { //rectangle(src, rect, scalar(0, 255, 0), 2); mat roi = src(rect); block_roi.push_back({ roi ,rect }); } } } if (block_roi.size()!=4)return fal; for (int i = 0; i < block_roi.size()-1; i++) { for (int j = 0; j < block_roi.size() - 1 - i; j++) { if (block_roi[j].rect.x > block_roi[j + 1].rect.x) { card temp = block_roi[j]; block_roi[j] = block_roi[j + 1]; block_roi[j + 1] = temp; } } } //for (int i = 0; i < block_roi.size(); i++) //{ // imshow(to_string(i), block_roi[i].mat); // waitkey(0); //} return true;}
由步骤2.1,我们已经将银行卡号定位,且顺序切割成四个小块。接下来,我们只需要将他们依次的将字符切割下来就可以了。其实切割字符跟上面的切割小方块是差不多的,这里就不再多说了。在这里我着重要说明的是,切割出来的字符相对于银行卡所在位置。
由步骤2.1,我们顺序切割出来四个小方块。以其中一个小方块为例,当时我们存储了rect变量,它表示该小方块相对于图像起点(x,y),宽w,高h。而步骤2.2我们需要做的就是将这个小方块的字符切割出来,那么每一个字符相对于小方块所在位置为起点(x,y),宽w,高h。所以,这些字符相当于银行卡所在位置就是起点(x+x,y+y),宽 (w),高(h)。具体请细看源码。也比较简单容易理解。
//循环上面切割出来的四个小块,将上面的字符一一切割出来。 for (int i = 0; i < block_roi.size(); i++) { mat roi_gray; cvtcolor(block_roi[i].mat, roi_gray, color_bgr2gray); mat roi_thresh; threshold(roi_gray, roi_thresh, 0, 255, thresh_binary|thresh_otsu); vector <vector<point>> contours; findcontours(roi_thresh, contours, retr_external, chain_approx_simple); for (int j = 0; j < contours.size(); j++) { rect rect = boundingrect(contours[j]); //字符相对于银行卡所在的位置 rect roi_rect(rect.x + block_roi[i].rect.x, rect.y + block_roi[i].rect.y, rect.width, rect.height); mat r_roi = block_roi[i].mat(rect); slice_roi.push_back({ r_roi ,roi_rect }); } }
同样,在这里我们也需要将切割出来的字符顺序排序。即银行卡上的号码是怎样排序的,我们就需要怎样排序保存
for (int i = 0; i < slice_roi.size() - 1; i++) { for (int j = 0; j < slice_roi.size() - 1 - i; j++) { if (slice_roi[j].rect.x > slice_roi[j + 1].rect.x) { card temp = slice_roi[j]; slice_roi[j] = slice_roi[j + 1]; slice_roi[j + 1] = temp; } } }
2.2.1 功能效果
如图为顺序切割出来的字符
2.2.2 功能源码
bool cut_slice(vector<card>&block_roi,vector<card>&slice_roi){ //循环上面切割出来的四个小块,将上面的字符一一切割出来。 for (int i = 0; i < block_roi.size(); i++) { mat roi_gray; cvtcolor(block_roi[i].mat, roi_gray, color_bgr2gray); mat roi_thresh; threshold(roi_gray, roi_thresh, 0, 255, thresh_binary|thresh_otsu); vector <vector<point>> contours; findcontours(roi_thresh, contours, retr_external, chain_approx_simple); for (int j = 0; j < contours.size(); j++) { rect rect = boundingrect(contours[j]); //字符相对于银行卡所在的位置 rect roi_rect(rect.x + block_roi[i].rect.x, rect.y + block_roi[i].rect.y, rect.width, rect.height); mat r_roi = block_roi[i].mat(rect); slice_roi.push_back({ r_roi ,roi_rect }); } } if (slice_roi.size() != 16) return fal; for (int i = 0; i < slice_roi.size() - 1; i++) { for (int j = 0; j < slice_roi.size() - 1 - i; j++) { if (slice_roi[j].rect.x > slice_roi[j + 1].rect.x) { card temp = slice_roi[j]; slice_roi[j] = slice_roi[j + 1]; slice_roi[j + 1] = temp; } } } //for (int i = 0; i < slice_roi.size(); i++) //{ // imshow(to_string(i), slice_roi[i].mat); // waitkey(0); //} return true;}
如图所示,为模板图像对应的label。我们需要读取文件,进行匹配。
bool readdata(string filename, vector<int>&label){ fstream fin; fin.open(filename, ios::in); if (!fin.is_open()) { cout << "can not open the file!" << endl; return fal; } int data[10] = { 0 }; for (int i = 0; i < 10; i++) { fin >> data[i]; } fin.clo(); for (int i = 0; i < 10; i++) { label.push_back(data[i]); } return true;}
在这里,我的思路是:使用一个for循环,将我们切割出来的字符与现有的模板一一进行匹配。使用的算法是图像模板匹配matchtemplate。具体用法请大家自行查找相关资料。具体请看源码
bool template_matching(vector<card>&card_temp, vector<card>&block_roi, vector<card>&slice_roi, vector<int>&result_index){ for (int i = 0; i < slice_roi.size(); i++) { //将字符resize成合适大小,利于识别 resize(slice_roi[i].mat, slice_roi[i].mat, size(60, 80), 1, 1, inter_linear); mat gray; cvtcolor(slice_roi[i].mat, gray, color_bgr2gray); int maxindex = 0; double max = 0.0; for (int j = 0; j < card_temp.size(); j++) { resize(card_temp[j].mat, card_temp[j].mat, size(60, 80), 1, 1, inter_linear); mat temp_gray; cvtcolor(card_temp[j].mat, temp_gray, color_bgr2gray); //进行模板匹配,识别数字 mat result; matchtemplate(gray, temp_gray, result, tm_sqdiff_normed); double minval, maxval; point minloc, maxloc; minmaxloc(result, &minval, &maxval, &minloc, &maxloc); //得分最大的视为匹配结果 if (maxval > max) { max = maxval; maxindex = j; //匹配结果 } } result_index.push_back(maxindex);//将匹配结果进行保存 } if (result_index.size() != 16)return fal; return true;}
bool show_result(mat src, vector<card>&block_roi, vector<card>&slice_roi, vector<int>&result_index){ //读取label标签 vector<int>label; if (!readdata("label.txt", label))return fal; //将匹配结果进行显示 for (int i = 0; i < block_roi.size(); i++) { rectangle(src, rect(block_roi[i].rect.tl(), block_roi[i].rect.br()), scalar(0, 255, 0), 2); } for (int i = 0; i < slice_roi.size(); i++) { cout << label[result_index[i]] << " "; puttext(src, to_string(label[result_index[i]]), point(slice_roi[i].rect.tl()), font_hershey_simplex, 1, scalar(0, 0, 255), 2); } imshow("demo", src); waitkey(0); destroyallwindows(); return true;}
如图所示,为本案例最终的效果展示。
#pragma once#include<opencv2/opencv.hpp>#include<iostream>struct card{cv::mat mat;cv::rect rect;};//获取模板图像bool get_template(cv::mat temp, std::vector<card>&card_temp);//将银行卡卡号部分切成四块bool cut_block(cv::mat src, std::vector<card>&block_roi);//将每一块数字区域切分出单独数字bool cut_slice(std::vector<card>&block_roi, std::vector<card>&slice_roi);//将数字与模板进行模板匹配bool template_matching(std::vector<card>&card_temp, std::vector<card>&block_roi,std::vector<card>&slice_roi,std::vector<int>&result_index);//显示最终结果bool show_result(cv::mat src,std::vector<card>&block_roi, std::vector<card>&slice_roi,std::vector<int>&result_index);
#include<iostream>#include"carddectection.h"#include<fstream>using namespace std;using namespace cv;bool get_template(mat temp, vector<card>&card_temp){//图像预处理mat gray;cvtcolor(temp, gray, color_bgr2gray);mat thresh;threshold(gray, thresh, 0, 255, thresh_binary_inv|thresh_otsu);//轮廓检测vector <vector<point>> contours;findcontours(thresh, contours, retr_external, chain_approx_simple);for (int i = 0; i < contours.size(); i++){rect rect = boundingrect(contours[i]);double ratio = double(rect.width) / double(rect.height);//筛选出字符轮廓if (ratio > 0.5 && ratio < 1){/*rectangle(temp, rect, scalar(0, 255, 0));*/mat roi = temp(rect); //将字符扣出,放入card_temp容器备用card_temp.push_back({ roi ,rect });}}if (card_temp.empty())return fal;//进行字符排序,使其按(0、1、2...7、8、9)顺序排序for (int i = 0; i < card_temp.size()-1; i++){for (int j = 0; j < card_temp.size() - 1 - i; j++){if (card_temp[j].rect.x > card_temp[j + 1].rect.x){card temp = card_temp[j];card_temp[j] = card_temp[j + 1];card_temp[j + 1] = temp;}}}//for (int i = 0; i < card_temp.size(); i++)//{//imshow(to_string(i), card_temp[i].mat);//waitkey(0);//}return true;}bool cut_block(mat src, vector<card>&block_roi){//形态学操作、以便找到银行卡号区域轮廓mat gray;cvtcolor(src, gray, color_bgr2gray);mat gaussian;gaussianblur(gray, gaussian, size(3, 3), 0);mat thresh;threshold(gaussian, thresh, 0, 255, thresh_binary | thresh_otsu);mat clo;mat kernel2 = getstructuringelement(morph_rect, size(15, 5));morphologyex(thresh, clo, morph_clo, kernel2);vector<vector<point>>contours;findcontours(clo, contours, retr_external, chain_approx_simple);for (int i = 0; i < contours.size(); i++){//通过面积、长宽比筛选出银行卡号区域double area = contourarea(contours[i]);if (area > 800 && area < 1400){rect rect = boundingrect(contours[i]);float ratio = double(rect.width) / double(rect.height);if (ratio > 2.8 && ratio < 3.1){//rectangle(src, rect, scalar(0, 255, 0), 2);mat roi = src(rect);block_roi.push_back({ roi ,rect });}}}if (block_roi.size()!=4)return fal;for (int i = 0; i < block_roi.size()-1; i++){for (int j = 0; j < block_roi.size() - 1 - i; j++){if (block_roi[j].rect.x > block_roi[j + 1].rect.x){card temp = block_roi[j];block_roi[j] = block_roi[j + 1];block_roi[j + 1] = temp;}}}//for (int i = 0; i < block_roi.size(); i++)//{//imshow(to_string(i), block_roi[i].mat);//waitkey(0);//}return true;}bool cut_slice(vector<card>&block_roi,vector<card>&slice_roi){//循环上面切割出来的四个小块,将上面的字符一一切割出来。for (int i = 0; i < block_roi.size(); i++){mat roi_gray;cvtcolor(block_roi[i].mat, roi_gray, color_bgr2gray);mat roi_thresh;threshold(roi_gray, roi_thresh, 0, 255, thresh_binary|thresh_otsu);vector <vector<point>> contours;findcontours(roi_thresh, contours, retr_external, chain_approx_simple);for (int j = 0; j < contours.size(); j++){rect rect = boundingrect(contours[j]);//字符相对于银行卡所在的位置rect roi_rect(rect.x + block_roi[i].rect.x, rect.y + block_roi[i].rect.y, rect.width, rect.height);mat r_roi = block_roi[i].mat(rect);slice_roi.push_back({ r_roi ,roi_rect });}}if (slice_roi.size() != 16) return fal;for (int i = 0; i < slice_roi.size() - 1; i++){for (int j = 0; j < slice_roi.size() - 1 - i; j++){if (slice_r花千骨杀阡陌oi[j].rect.x > slice_roi[j + 1].rect.x){card temp = slice_roi[j];slice_roi[j] = slice_roi[j + 1];slice_roi[j + 1] = temp;}}}//for (int i = 0; i < slice_roi.size(); i++)//{//imshow(to_string(i), slice_roi[i].mat);//waitkey(0);//}return true;}bool readdata(string filename, vector<int>&label){fstream fin;fin.open(filename, ios::in);if (!fin.is_open()){cout << "can not open the file!" << endl;return fal;}int data[10] = { 0 };for (int i = 0; i < 10; i++){fin >> data[i];}fin.clo();for (int i = 0; i < 10; i++){label.push_back(data[i]);}return true;}bool template_matching(vector<card>&card_temp,vector<card>&block_roi, vector<card>&slice_roi,vector<int>&result_index){for (int i = 0; i < slice_roi.size(); i++){//将字符resize成合适大小,利于识别resize(slice_roi[i].mat, slice_roi[i].mat, size(60, 80), 1, 1, inter_linear);mat gray;cvtcolor(slice_roi[i].mat, gray, color_bgr2gray);int maxindex = 0;double max = 0.0;for (int j = 0; j < card_temp.size(); j++){resize(card_temp[j].mat, card_temp[j].mat, size(60, 80), 1, 1, inter_linear);mat temp_gray;cvtcolor(card_temp[j].mat, temp_gray, color_bgr2gray);//进行模板匹配,识别数字mat result;matchtemplate(gray, temp_gray, result, tm_sqdiff_normed);double minval, maxval;point minloc, maxloc;minmaxloc(result, &minval, &maxval, &minloc, &maxloc);//得分最大的视为匹配结果if (maxval > max){max = maxval;maxindex = j; //匹配结果}}result_index.push_back(maxindex);//将匹配结果进行保存}if (result_index.size() != 16)return fal;return true;}bool show_result(mat src, vector<card>&block_roi,vector<card>&slice_roi, vector<int>&result_index){//读取label标签vector<int>label;if (!readdata("label.txt", label))return fal;//将匹配结果进行显示for (int i = 0; i < block_roi.size(); i++){rectangle(src, rect(block_roi[i].rect.tl(), block_roi[i].rect.br()), scalar(0, 255, 0), 2);}for (int i = 0; i < slice_roi.size(); i++){cout << label[result_index[i]] << " ";puttext(src, to_string(label[result_index[i]]), point(slice_roi[i].rect.tl()), font_hershey_simplex, 1, scalar(0, 0, 255), 2);}imshow("demo", src);waitkey(0);destroyallwindows();return true;}
#include<iostream>#include"carddectection.h"using namespace std;using namespace cv;int main(){mat src = imread("card.png"); //源图像 银行卡mat temp = imread("number.png"); //模板图像if (src.empty() || temp.empty()){cout << "no image data !" <&服务生lt; endl;system("pau");return -1;}vector<card>card_temp;if (!get_template(temp, card_temp)){cout << "模板切割失败!" << endl;system("pau");return -1;}vector<card>block_roi;if (cut_block(src, block_roi)){vector<card>slice_roi;if (cut_slice(block_roi, slice_roi)){vector<int>result_index;if (template_matching(card_temp, block_roi, slice_roi, result_index)){show_result(src, block_roi, slice_roi, result_index);}el{cout << "识别失败!" << endl;system("pau");return -1;}}el{cout << "切片失败!" << endl;system("pau");return -1;}}el{cout << "切块失败!" << endl;system("pau");return -1;}system("pau");return 0;}
本文使用opencv c++进行银行卡号识别,关键步骤有以下几点。
1、银行卡号定位。根据本案例中的银行卡图像特征,我们先将银行卡号所在位置定位。根据图像特征,我们可以将银行卡号分为四个小方块进行定位切割。
2、字符分割。根据前面得到的银行卡号四个小方块,我们需要将它们顺序切割出每一个字符。
3、字符识别。我们将得到的字符与我们准备好的模板一一进行匹配。这里使用的匹配算法是图像模板匹配。
以上就是c++ opencv实现银行卡号识别功能的详细内容,更多关于c++ opencv银行卡号识别的资料请关注www.887551.com其它相关文章!
本文发布于:2023-04-04 17:18:53,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/0bb94f9fc814b3a8c4ede67077510892.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:C++ OpenCV实现银行卡号识别功能.doc
本文 PDF 下载地址:C++ OpenCV实现银行卡号识别功能.pdf
留言与评论(共有 0 条评论) |