发布时间:2024-01-11 18:00
本文将使用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 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;
}
}
}
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;
}
}
}
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
#include
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
#include\"CardDectection.h\"
#include
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
#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;
}
本文使用OpenCV C++进行银行卡号识别,关键步骤有以下几点。
1、银行卡号定位。根据本案例中的银行卡图像特征,我们先将银行卡号所在位置定位。根据图像特征,我们可以将银行卡号分为四个小方块进行定位切割。
2、字符分割。根据前面得到的银行卡号四个小方块,我们需要将它们顺序切割出每一个字符。
3、字符识别。我们将得到的字符与我们准备好的模板一一进行匹配。这里使用的匹配算法是图像模板匹配。
需要说明的是:本案例是根据特定图像、特定需求设定的算法。并不具有鲁棒性。所以在图像预处理阶段很重要。我们需要提取出我们需要的图像特征,这样才能够进行后续的工作。所以本案例也只是使用传统的图像处理手段实现银行卡号识别功能。将大致流程作了一个说明,这里只提供一个参考作用!!!本案例还有一个特别需要注意的就是,我们需要保存切割下来的字符相对于原图像所在的坐标位置。本案例跟OpenCV C++案例实战十《车牌号识别》算法思路非常相似,如果大家感兴趣的话也可以进行参考一下。欢迎大家进行交流学习!!!