欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++ OpenCV實(shí)現(xiàn)銀行卡號識別功能

 更新時間:2022年01月22日 15:44:43   作者:Zero___Chen  
這篇文章主要介紹了如何使用OpenCV C++實(shí)現(xiàn)銀行卡號識別功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)OpenCV有一定幫助,需要的可以參考一下

前言

本文將使用OpenCV C++ 進(jìn)行銀行卡號識別。主要步驟可以細(xì)分為:

1、 獲取模板圖像

2、銀行卡號區(qū)域定位

3、字符切割

4、模板匹配

5、效果顯示

接下來就具體看看是如何一步步實(shí)現(xiàn)的吧。

一、獲取模板圖像

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

1.1 功能效果

如圖為順序切割出來的模板字符。

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);

?? ?//輪廓檢測
?? ?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;
}

二、銀行卡號定位

如圖所示,這是本案例需要識別的銀行卡。從圖中可以看出,我們需要將銀行卡號切割出來首先得將卡號分為4個小塊切割,之后再需要將每一小塊上的字符切割。接下來一步步看是如何操作的。

2.1 將銀行卡號切割成四塊

首先第一步得先進(jìn)行圖像預(yù)處理,通過灰度、二值化、形態(tài)學(xué)等操作提取出卡號輪廓。這里的圖像預(yù)處理需要根據(jù)圖像特征自行確定,并不是所有的步驟都是必須的,我們最終的目的是為了定位銀行卡號所在輪廓位置。這里我使用的是二值化、以及形態(tài)學(xué)閉操作。  

?//形態(tài)學(xué)操作、以便找到銀行卡號區(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)過灰度、閾值、形態(tài)學(xué)操作后的圖像如下圖所示。我們已經(jīng)將銀行卡號分為四個小矩形塊,接下來只需通過輪廓查找、篩選就可以扣出這四個ROI區(qū)域了。

? vector<vector<Point>>contours;
?? ?findContours(close, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
?? ?for (int i = 0; i < contours.size(); i++)
?? ?{
?? ??? ?//通過面積、長寬比篩選出銀行卡號區(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 });
?? ??? ??? ?}
?? ??? ?}
?? ?}

同理,我們需要將切割下來的小塊按照它原來的順序存儲。

    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é)操作、以便找到銀行卡號區(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++)
?? ?{
?? ??? ?//通過面積、長寬比篩選出銀行卡號區(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)將銀行卡號定位,且順序切割成四個小塊。接下來,我們只需要將他們依次的將字符切割下來就可以了。其實(shí)切割字符跟上面的切割小方塊是差不多的,這里就不再多說了。在這里我著重要說明的是,切割出來的字符相對于銀行卡所在位置。

由步驟2.1,我們順序切割出來四個小方塊。以其中一個小方塊為例,當(dāng)時我們存儲了rect變量,它表示該小方塊相對于圖像起點(diǎn)(X,Y),寬W,高H。而步驟2.2我們需要做的就是將這個小方塊的字符切割出來,那么每一個字符相對于小方塊所在位置為起點(diǎn)(x,y),寬w,高h(yuǎn)。所以,這些字符相當(dāng)于銀行卡所在位置就是起點(diǎn)(X+x,Y+y),寬 (w),高(h)。具體請細(xì)看源碼。也比較簡單容易理解。  

?//循環(huán)上面切割出來的四個小塊,將上面的字符一一切割出來。
?? ?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)
{
?? ?//循環(huán)上面切割出來的四個小塊,將上面的字符一一切割出來。
?? ?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;
}

三、字符識別

3.1.讀取文件

如圖所示,為模板圖像對應(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.字符匹配

在這里,我的思路是:使用一個for循環(huán),將我們切割出來的字符與現(xiàn)有的模板一一進(jìn)行匹配。使用的算法是圖像模板匹配matchTemplate。具體用法請大家自行查找相關(guān)資料。具體請看源碼

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成合適大小,利于識別
?? ??? ?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ù)字
?? ??? ??? ?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);

//將銀行卡卡號部分切成四塊
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);

	//輪廓檢測
	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é)操作、以便找到銀行卡號區(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++)
	{
		//通過面積、長寬比篩選出銀行卡號區(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)上面切割出來的四個小塊,將上面的字符一一切割出來。
	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);

			//進(jìn)行模板匹配,識別數(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 << "識別失??!" << 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)行銀行卡號識別,關(guān)鍵步驟有以下幾點(diǎn)。

1、銀行卡號定位。根據(jù)本案例中的銀行卡圖像特征,我們先將銀行卡號所在位置定位。根據(jù)圖像特征,我們可以將銀行卡號分為四個小方塊進(jìn)行定位切割。

2、字符分割。根據(jù)前面得到的銀行卡號四個小方塊,我們需要將它們順序切割出每一個字符。

3、字符識別。我們將得到的字符與我們準(zhǔn)備好的模板一一進(jìn)行匹配。這里使用的匹配算法是圖像模板匹配。

以上就是C++ OpenCV實(shí)現(xiàn)銀行卡號識別功能的詳細(xì)內(nèi)容,更多關(guān)于C++ OpenCV銀行卡號識別的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論