您好,登錄后才能下訂單哦!
這篇文章主要介紹了C++ OpenCV如何實現銀行卡號識別功能,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
如圖所示,這是我們的模板圖像。我們需要將上面的字符一一切割出來保存,以便進行后續的字符匹配環節。先進行圖像灰度、閾值等操作進行輪廓提取,這里就不再細說。這里我想說的是,由于經過輪廓檢索,提取出來的字符并不是按(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 false; //進行字符排序,使其按(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(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);
經過灰度、閾值、形態學操作后的圖像如下圖所示。我們已經將銀行卡號分為四個小矩形塊,接下來只需通過輪廓查找、篩選就可以扣出這四個ROI區域了。
vector<vector<Point>>contours; findContours(close, 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 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++) { //通過面積、長寬比篩選出銀行卡號區域 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.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 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; }
如圖所示,為模板圖像對應的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 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; }
在這里,我的思路是:使用一個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 false; 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 false; //將匹配結果進行顯示 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 false; //進行字符排序,使其按(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 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++) { //通過面積、長寬比篩選出銀行卡號區域 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) { //循環上面切割出來的四個小塊,將上面的字符一一切割出來。 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 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成合適大小,利于識別 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 false; 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 false; //將匹配結果進行顯示 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 !" << 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 << "識別失敗!" << endl; system("pause"); return -1; } } else { cout << "切片失敗!" << endl; system("pause"); return -1; } } else { cout << "切塊失敗!" << endl; system("pause"); return -1; } system("pause"); return 0; }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“C++ OpenCV如何實現銀行卡號識別功能”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。