C++ OpenCV實(shí)現(xiàn)銀行卡號(hào)識(shí)別功能
前言
本文將使用OpenCV C++ 進(jìn)行銀行卡號(hào)識(shí)別。主要步驟可以細(xì)分為:
1、 獲取模板圖像
2、銀行卡號(hào)區(qū)域定位
3、字符切割
4、模板匹配
5、效果顯示
接下來(lái)就具體看看是如何一步步實(shí)現(xiàn)的吧。
一、獲取模板圖像

如圖所示,這是我們的模板圖像。我們需要將上面的字符一一切割出來(lái)保存,以便進(jìn)行后續(xù)的字符匹配環(huán)節(jié)。先進(jìn)行圖像灰度、閾值等操作進(jìn)行輪廓提取,這里就不再細(xì)說(shuō)。這里我想說(shuō)的是,由于經(jīng)過(guò)輪廓檢索,提取出來(lái)的字符并不是按(0、1、2…7、8、9)順序排列,所以,在這里我自定義了一個(gè)Card結(jié)構(gòu)體,用于圖像排序。具體請(qǐng)看源碼。
1.1 功能效果

如圖為順序切割出來(lái)的模板字符。
1.2 功能源碼
bool Get_Template(Mat temp, vector<Card>&Card_Temp)
{
?? ?//圖像預(yù)處理
?? ?Mat gray;
?? ?cvtColor(temp, gray, COLOR_BGR2GRAY);
?? ?Mat thresh;
?? ?threshold(gray, thresh, 0, 255, THRESH_BINARY_INV|THRESH_OTSU);
?? ?//輪廓檢測(cè)
?? ?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 false;
?? ?//進(jìn)行字符排序,使其按(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;
}二、銀行卡號(hào)定位

如圖所示,這是本案例需要識(shí)別的銀行卡。從圖中可以看出,我們需要將銀行卡號(hào)切割出來(lái)首先得將卡號(hào)分為4個(gè)小塊切割,之后再需要將每一小塊上的字符切割。接下來(lái)一步步看是如何操作的。
2.1 將銀行卡號(hào)切割成四塊
首先第一步得先進(jìn)行圖像預(yù)處理,通過(guò)灰度、二值化、形態(tài)學(xué)等操作提取出卡號(hào)輪廓。這里的圖像預(yù)處理需要根據(jù)圖像特征自行確定,并不是所有的步驟都是必須的,我們最終的目的是為了定位銀行卡號(hào)所在輪廓位置。這里我使用的是二值化、以及形態(tài)學(xué)閉操作。
?//形態(tài)學(xué)操作、以便找到銀行卡號(hào)區(qū)域輪廓 ?? ?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 close; ?? ?Mat kernel2 = getStructuringElement(MORPH_RECT, Size(15, 5)); ?? ?morphologyEx(thresh, close, MORPH_CLOSE, kernel2);
經(jīng)過(guò)灰度、閾值、形態(tài)學(xué)操作后的圖像如下圖所示。我們已經(jīng)將銀行卡號(hào)分為四個(gè)小矩形塊,接下來(lái)只需通過(guò)輪廓查找、篩選就可以扣出這四個(gè)ROI區(qū)域了。

? vector<vector<Point>>contours;
?? ?findContours(close, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
?? ?for (int i = 0; i < contours.size(); i++)
?? ?{
?? ??? ?//通過(guò)面積、長(zhǎng)寬比篩選出銀行卡號(hào)區(qū)域
?? ??? ?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 });
?? ??? ??? ?}
?? ??? ?}
?? ?}
同理,我們需要將切割下來(lái)的小塊按照它原來(lái)的順序存儲(chǔ)。
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)
{
?? ?//形態(tài)學(xué)操作、以便找到銀行卡號(hào)區(qū)域輪廓
?? ?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 close;
?? ?Mat kernel2 = getStructuringElement(MORPH_RECT, Size(15, 5));
?? ?morphologyEx(thresh, close, MORPH_CLOSE, kernel2);
?? ?vector<vector<Point>>contours;
?? ?findContours(close, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
?? ?for (int i = 0; i < contours.size(); i++)
?? ?{
?? ??? ?//通過(guò)面積、長(zhǎng)寬比篩選出銀行卡號(hào)區(qū)域
?? ??? ?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 false;
?? ?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.2 字符切割
由步驟2.1,我們已經(jīng)將銀行卡號(hào)定位,且順序切割成四個(gè)小塊。接下來(lái),我們只需要將他們依次的將字符切割下來(lái)就可以了。其實(shí)切割字符跟上面的切割小方塊是差不多的,這里就不再多說(shuō)了。在這里我著重要說(shuō)明的是,切割出來(lái)的字符相對(duì)于銀行卡所在位置。

由步驟2.1,我們順序切割出來(lái)四個(gè)小方塊。以其中一個(gè)小方塊為例,當(dāng)時(shí)我們存儲(chǔ)了rect變量,它表示該小方塊相對(duì)于圖像起點(diǎn)(X,Y),寬W,高H。而步驟2.2我們需要做的就是將這個(gè)小方塊的字符切割出來(lái),那么每一個(gè)字符相對(duì)于小方塊所在位置為起點(diǎn)(x,y),寬w,高h(yuǎn)。所以,這些字符相當(dāng)于銀行卡所在位置就是起點(diǎn)(X+x,Y+y),寬 (w),高(h)。具體請(qǐng)細(xì)看源碼。也比較簡(jiǎn)單容易理解。
?//循環(huán)上面切割出來(lái)的四個(gè)小塊,將上面的字符一一切割出來(lái)。
?? ?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]);
?? ??? ??? ?//字符相對(duì)于銀行卡所在的位置
?? ??? ??? ?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 });?? ??? ?
?? ??? ?}
?? ?}同樣,在這里我們也需要將切割出來(lái)的字符順序排序。即銀行卡上的號(hào)碼是怎樣排序的,我們就需要怎樣排序保存
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 功能效果

如圖為順序切割出來(lái)的字符
2.2.2 功能源碼
bool Cut_Slice(vector<Card>&Block_ROI,vector<Card>&Slice_ROI)
{
?? ?//循環(huán)上面切割出來(lái)的四個(gè)小塊,將上面的字符一一切割出來(lái)。
?? ?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]);
?? ??? ??? ?//字符相對(duì)于銀行卡所在的位置
?? ??? ??? ?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 false;
?? ?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;
}三、字符識(shí)別
3.1.讀取文件

如圖所示,為模板圖像對(duì)應(yīng)的label。我們需要讀取文件,進(jìn)行匹配。
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 false;
?? ?}
?? ?int data[10] = { 0 };
?? ?for (int i = 0; i < 10; i++)
?? ?{
?? ??? ?fin >> data[i];
?? ?}
?? ?fin.close();
?? ?for (int i = 0; i < 10; i++)
?? ?{
?? ??? ?label.push_back(data[i]);
?? ?}
?? ?return true;
}3.2.字符匹配
在這里,我的思路是:使用一個(gè)for循環(huán),將我們切割出來(lái)的字符與現(xiàn)有的模板一一進(jìn)行匹配。使用的算法是圖像模板匹配matchTemplate。具體用法請(qǐng)大家自行查找相關(guān)資料。具體請(qǐng)看源碼
3.3.功能源碼
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成合適大小,利于識(shí)別
?? ??? ?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);
?? ??? ??? ?//進(jìn)行模板匹配,識(shí)別數(shù)字
?? ??? ??? ?Mat result;
?? ??? ??? ?matchTemplate(gray, temp_gray, result, TM_SQDIFF_NORMED);
?? ??? ??? ?double minVal, maxVal;
?? ??? ??? ?Point minLoc, maxLoc;
?? ??? ??? ?minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc);
?? ??? ??? ?
?? ??? ??? ?//得分最大的視為匹配結(jié)果
?? ??? ??? ?if (maxVal > Max)
?? ??? ??? ?{
?? ??? ??? ??? ?Max = maxVal;
?? ??? ??? ??? ?maxIndex = j; //匹配結(jié)果
?? ??? ??? ?}
?? ??? ?}
?? ??? ?result_index.push_back(maxIndex);//將匹配結(jié)果進(jìn)行保存
?? ?}
?? ?if (result_index.size() != 16)return false;
?? ?return true;
}四、效果顯示
4.1 功能源碼
bool Show_Result(Mat src,?
?? ?vector<Card>&Block_ROI,
?? ?vector<Card>&Slice_ROI,?
?? ?vector<int>&result_index)
{
?? ?//讀取label標(biāo)簽
?? ?vector<int>label;
?? ?if (!ReadData("label.txt", label))return false;
?? ?//將匹配結(jié)果進(jìn)行顯示
?? ?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;
}4.2 效果顯示


如圖所示,為本案例最終的效果展示。
五、源碼
5.1 hpp文件
#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);
//將銀行卡卡號(hào)部分切成四塊
bool Cut_Block(cv::Mat src, std::vector<Card>&Block_ROI);
//將每一塊數(shù)字區(qū)域切分出單獨(dú)數(shù)字
bool Cut_Slice(std::vector<Card>&Block_ROI, std::vector<Card>&Slice_ROI);
//將數(shù)字與模板進(jìn)行模板匹配
bool Template_Matching(std::vector<Card>&Card_Temp,
std::vector<Card>&Block_ROI,
std::vector<Card>&Slice_ROI,
std::vector<int>&result_index);
//顯示最終結(jié)果
bool Show_Result(cv::Mat src,
std::vector<Card>&Block_ROI,
std::vector<Card>&Slice_ROI,
std::vector<int>&result_index);
5.2 cpp文件
#include<iostream>
#include"CardDectection.h"
#include<fstream>
using namespace std;
using namespace cv;
bool Get_Template(Mat temp, vector<Card>&Card_Temp)
{
//圖像預(yù)處理
Mat gray;
cvtColor(temp, gray, COLOR_BGR2GRAY);
Mat thresh;
threshold(gray, thresh, 0, 255, THRESH_BINARY_INV|THRESH_OTSU);
//輪廓檢測(cè)
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 false;
//進(jìn)行字符排序,使其按(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)
{
//形態(tài)學(xué)操作、以便找到銀行卡號(hào)區(qū)域輪廓
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 close;
Mat kernel2 = getStructuringElement(MORPH_RECT, Size(15, 5));
morphologyEx(thresh, close, MORPH_CLOSE, kernel2);
vector<vector<Point>>contours;
findContours(close, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contours.size(); i++)
{
//通過(guò)面積、長(zhǎng)寬比篩選出銀行卡號(hào)區(qū)域
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 false;
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)
{
//循環(huán)上面切割出來(lái)的四個(gè)小塊,將上面的字符一一切割出來(lái)。
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]);
//字符相對(duì)于銀行卡所在的位置
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 false;
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;
}
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 false;
}
int data[10] = { 0 };
for (int i = 0; i < 10; i++)
{
fin >> data[i];
}
fin.close();
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成合適大小,利于識(shí)別
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);
//進(jìn)行模板匹配,識(shí)別數(shù)字
Mat result;
matchTemplate(gray, temp_gray, result, TM_SQDIFF_NORMED);
double minVal, maxVal;
Point minLoc, maxLoc;
minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc);
//得分最大的視為匹配結(jié)果
if (maxVal > Max)
{
Max = maxVal;
maxIndex = j; //匹配結(jié)果
}
}
result_index.push_back(maxIndex);//將匹配結(jié)果進(jìn)行保存
}
if (result_index.size() != 16)return false;
return true;
}
bool Show_Result(Mat src,
vector<Card>&Block_ROI,
vector<Card>&Slice_ROI,
vector<int>&result_index)
{
//讀取label標(biāo)簽
vector<int>label;
if (!ReadData("label.txt", label))return false;
//將匹配結(jié)果進(jìn)行顯示
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;
}
5.3 main文件
#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 !" << endl;
system("pause");
return -1;
}
vector<Card>Card_Temp;
if (!Get_Template(temp, Card_Temp))
{
cout << "模板切割失??!" << endl;
system("pause");
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);
}
else
{
cout << "識(shí)別失?。? << endl;
system("pause");
return -1;
}
}
else
{
cout << "切片失??!" << endl;
system("pause");
return -1;
}
}
else
{
cout << "切塊失敗!" << endl;
system("pause");
return -1;
}
system("pause");
return 0;
}
總結(jié)
本文使用OpenCV C++進(jìn)行銀行卡號(hào)識(shí)別,關(guān)鍵步驟有以下幾點(diǎn)。
1、銀行卡號(hào)定位。根據(jù)本案例中的銀行卡圖像特征,我們先將銀行卡號(hào)所在位置定位。根據(jù)圖像特征,我們可以將銀行卡號(hào)分為四個(gè)小方塊進(jìn)行定位切割。
2、字符分割。根據(jù)前面得到的銀行卡號(hào)四個(gè)小方塊,我們需要將它們順序切割出每一個(gè)字符。
3、字符識(shí)別。我們將得到的字符與我們準(zhǔn)備好的模板一一進(jìn)行匹配。這里使用的匹配算法是圖像模板匹配。
以上就是C++ OpenCV實(shí)現(xiàn)銀行卡號(hào)識(shí)別功能的詳細(xì)內(nèi)容,更多關(guān)于C++ OpenCV銀行卡號(hào)識(shí)別的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C語(yǔ)言中const和C++中的const 區(qū)別詳解
貪吃蛇C語(yǔ)言代碼實(shí)現(xiàn)(難度可選)
C++?STL中五個(gè)常用算法使用教程及實(shí)例講解
Qt 實(shí)現(xiàn)畫(huà)線筆鋒效果詳細(xì)原理及示例代碼
使用Qt的QChartView實(shí)現(xiàn)縮放和放大功能
C++ LeetCode1796字符串中第二大數(shù)字

