模板匹配
- I 整体代码
- II 代码解释
- 1.导入开发用的工具包
- 2.定义一个信用卡名称的字典,用于识别这是什么信用卡
- 3.cv_show函数的定义
- 4.把模板读取进来
- 5.转化为灰度图
- 6.将灰度图转化为二值图片
- 7.绘制外侧轮廓并打印个数
- 8.将轮廓信息以从左到右从上到下的方式排序
- 9.遍历排序完的轮廓并绘制固定大小的外接矩形,并把这些矩形保留为模板
- 10.准备导入信用卡图片,先定义两个不同大小的卷积核以便形态学操作使用
- 11.导入信用卡图片,并进行预处理
- 12.进行一次礼帽操作,突出更明亮的区域
- 13使用sobel算子进行梯度检测,这里只用了x
- 14.通过闭操作(先膨胀,再腐蚀)将每四个数字连在一起
- 15.进行二值化处理
- 16.再次闭操作,使四个数字连接的更为清晰
- 17.计算轮廓
- 18.遍历轮廓,过滤掉不需要的轮廓
- 19.将符合要求的四个轮廓进行排序
- 20.(最后的迭代处理)一个大for循环来将这四个轮廓:处理-拆分-处理
- 21.最终效果
I 整体代码
from imutils import contours
import numpy as np
import argparse
import imutils
import cv2
import myutils
FIRST_NUMBER = {
"3": "American Express",
"4": "Visa",
"5": "MasterCard",
"6": "Disco5ver Card"
}
def cv_show(name,img):
cv2.imshow(name, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread('1.png',cv2.IMREAD_ANYCOLOR)#导入模板
ref = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ref = cv2.threshold(ref, 10, 255, cv2.THRESH_BINARY_INV)[1]
refCnts, hierarchy = cv2.findContours(ref.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img,refCnts,-1,(0,0,255),3)
refCnts = myutils.sort_contours(refCnts, method="left-to-right")[0]
digits = {}
for (i, c) in enumerate(refCnts):
(x, y, w, h) = cv2.boundingRect(c)
roi = ref[y:y + h, x:x + w]
roi = cv2.resize(roi, (57, 88))
digits[i] = roi
rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 3))
sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
image = cv2.imread('123.png') #导入目标
image = myutils.resize(image, width=300)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
tophat = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, rectKernel)
gradX = cv2.Sobel(tophat, ddepth=cv2.CV_32F, dx=1, dy=0,
ksize=-1)
gradX = np.absolute(gradX)
(minVal, maxVal) = (np.min(gradX), np.max(gradX))
gradX = (255 * ((gradX - minVal) / (maxVal - minVal)))
gradX = gradX.astype("uint8")
gradX = cv2.morphologyEx(gradX, cv2.MORPH_CLOSE, rectKernel)
thresh = cv2.threshold(gradX, 0, 255,
cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, sqKernel)
threshCnts, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = threshCnts
cur_img = image.copy()
cv2.drawContours(cur_img,cnts,-1,(0,0,255),3)
locs = []
for (i, c) in enumerate(cnts):
(x, y, w, h) = cv2.boundingRect(c)
ar = w / float(h)
if ar > 2.5 and ar < 4.0:
if (w > 40 and w < 55) and (h > 10 and h < 20):
locs.append((x, y, w, h))
locs = sorted(locs, key=lambda x:x[0])
output = []
for (i, (gX, gY, gW, gH)) in enumerate(locs):
groupOutput = []
group = gray[gY - 5:gY + gH + 5, gX - 5:gX + gW + 5]
group = cv2.threshold(group, 0, 255,
cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
digitCnts,hierarchy = cv2.findContours(group.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
digitCnts = contours.sort_contours(digitCnts,
method="left-to-right")[0]
for c in digitCnts:
(x, y, w, h) = cv2.boundingRect(c)
roi = group[y:y + h, x:x + w]
roi = cv2.resize(roi, (57, 88))
scores = []
for (digit, digitROI) in digits.items():
result = cv2.matchTemplate(roi, digitROI,
cv2.TM_CCOEFF)
(_, score, _, _) = cv2.minMaxLoc(result)
scores.append(score)
groupOutput.append(str(np.argmax(scores)))
cv2.rectangle(image, (gX - 5, gY - 5),
(gX + gW + 5, gY + gH + 5), (0, 0, 255), 1)
cv2.putText(image, "".join(groupOutput), (gX, gY - 15),
cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2)
output.extend(groupOutput)
print(groupOutput)
print("Credit Card Type: {}".format(FIRST_NUMBER[output[0]]))
print("Credit Card #: {}".format("".join(output)))
cv2.imshow("Image", image)
cv2.waitKey(0)
II 代码解释
1.导入开发用的工具包
其中myutils是第八步所需要的工具包(在这里):https://download.csdn.net/download/helloworld573/12329376
from imutils import contours
import numpy as np
import argparse
import imutils
import cv2
import myutils
2.定义一个信用卡名称的字典,用于识别这是什么信用卡
(在最后我们知道,看信用卡的第1位就行了)
FIRST_NUMBER = {
"3": "American Express",
"4": "Visa",
"5": "MasterCard",
"6": "Disco5ver Card"
}
3.cv_show函数的定义
def cv_show(name,img):
cv2.imshow(name, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
4.把模板读取进来
img = cv2.imread('1.png',cv2.IMREAD_ANYCOLOR)
cv_show('img',img)
5.转化为灰度图
ref = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv_show('ref',ref)
6.将灰度图转化为二值图片
ref = cv2.threshold(ref, 10, 255, cv2.THRESH_BINARY_INV)[1]#取第二个返回值给ref
cv_show('ref',ref)
7.绘制外侧轮廓并打印个数
refCnts, hierarchy = cv2.findContours(ref.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
#将ref备份,进行轮廓检测,只检测最外轮廓,只保留终点坐标(便于知道轮廓位置)
#refCnts是储存每一个轮廓的列表
cv2.drawContours(img,refCnts,-1,(0,0,255),3)
#将所有的轮廓都画出来
cv_show('img',img)
#统计个数
print (np.array(refCnts).shape)
8.将轮廓信息以从左到右从上到下的方式排序
refCnts = myutils.sort_contours(refCnts, method="left-to-right")[0]
#refCnts是排序完的轮廓
9.遍历排序完的轮廓并绘制固定大小的外接矩形,并把这些矩形保留为模板
digits = {}
# 遍历每一个轮廓
for (i, c) in enumerate(refCnts):
# 计算外接矩形并且resize成合适大小
(x, y, w, h) = cv2.boundingRect(c)
roi = ref[y:y + h, x:x + w]
roi = cv2.resize(roi, (57, 88))
#保留为模板
digits[i] = roi
print(digits[i])
10.准备导入信用卡图片,先定义两个不同大小的卷积核以便形态学操作使用
rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 3)) #9x3
sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) #5x5
11.导入信用卡图片,并进行预处理
image = cv2.imread('123.png')
cv_show('image',image)
image = myutils.resize(image, width=300)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv_show('gray',gray)
12.进行一次礼帽操作,突出更明亮的区域
tophat = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, rectKernel)
cv_show('tophat',tophat)
13使用sobel算子进行梯度检测,这里只用了x
关于梯度检测: https://blog.csdn.net/helloworld573/article/details/105284024
gradX = cv2.Sobel(tophat, ddepth=cv2.CV_32F, dx=1, dy=0, #ksize=-1相当于用3*3的
ksize=-1)
gradX = np.absolute(gradX)
(minVal, maxVal) = (np.min(gradX), np.max(gradX))
gradX = (255 * ((gradX - minVal) / (maxVal - minVal)))
gradX = gradX.astype("uint8")
print (np.array(gradX).shape)
cv_show('gradX',gradX)
可以得到不明显的梯度
14.通过闭操作(先膨胀,再腐蚀)将每四个数字连在一起
关于闭操作: https://blog.csdn.net/helloworld573/article/details/105284024
gradX = cv2.morphologyEx(gradX, cv2.MORPH_CLOSE, rectKernel)
cv_show('gradX',gradX)
15.进行二值化处理
进行二值化处理,THRESH_OTSU会自动寻找合适的阈值,适合双峰,需把阈值参数设置为0
适合我不太清楚这这个阈值是多少,这是一种自动寻找阈值的方法
关于二值化处理的基本:https://blog.csdn.net/helloworld573/article/details/105263693
thresh = cv2.threshold(gradX, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
cv_show('thresh',thresh)
这样就更清晰了
16.再次闭操作,使四个数字连接的更为清晰
关于闭操作: https://blog.csdn.net/helloworld573/article/details/105284024
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, sqKernel) #再来一个闭操作
cv_show('thresh',thresh)
17.计算轮廓
threshCnts, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = threshCnts
cur_img = image.copy()
cv2.drawContours(cur_img,cnts,-1,(0,0,255),3)
cv_show('img',cur_img)
18.遍历轮廓,过滤掉不需要的轮廓
for (i, c) in enumerate(cnts):
# 计算轮廓的外接矩形
(x, y, w, h) = cv2.boundingRect(c)
# 算一下长宽比例
ar = w / float(h)
# 根据长宽比例来过滤不符合的区域。
# 根据实际任务来,这里的基本都是四个数字一组
if ar > 2.5 and ar < 4.0:
if (w > 40 and w < 55) and (h > 10 and h < 20):
#符合的留下来
locs.append((x, y, w, h))
print(locs)
最后得到四个矩形
19.将符合要求的四个轮廓进行排序
locs = sorted(locs, key=lambda x:x[0])
20.(最后的迭代处理)一个大for循环来将这四个轮廓:处理-拆分-处理
output = []
# 遍历每一个轮廓中的数字
for (i, (gX, gY, gW, gH)) in enumerate(locs):
# initialize the list of group digits
groupOutput = []
# 根据坐标提取每一个组
group = gray[gY - 5:gY + gH + 5, gX - 5:gX + gW + 5]#稍微把这个轮廓坐标+5-5把这个轮廓扩大一点给他点余缝
cv_show('group',group)
# 预处理
group = cv2.threshold(group, 0, 255,
cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
cv_show('group',group)
# 计算每一组的轮廓并排序
digitCnts,hierarchy = cv2.findContours(group.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
digitCnts = contours.sort_contours(digitCnts,
method="left-to-right")[0]
# 开始计算每一组中的每一个数值:准备模板匹配
for c in digitCnts:
# 找到当前数值的轮廓,resize成合适的的大小(需要和模板大小一模一样)
(x, y, w, h) = cv2.boundingRect(c)
roi = group[y:y + h, x:x + w]
roi = cv2.resize(roi, (57, 88))#模板大小也是57,88
cv_show('roi',roi)
# 计算匹配得分:开始进行模板匹配
scores = []
# 在模板中计算每一个得分
for (digit, digitROI) in digits.items():
# 模板匹配
result = cv2.matchTemplate(roi, digitROI,
cv2.TM_CCOEFF)
(_, score, _, _) = cv2.minMaxLoc(result)
scores.append(score)
# 得到最合适的数字
groupOutput.append(str(np.argmax(scores)))
# 在图片上画出来
cv2.rectangle(image, (gX - 5, gY - 5),
(gX + gW + 5, gY + gH + 5), (0, 0, 255), 1)
# 在图片上写出来
cv2.putText(image, "".join(groupOutput), (gX, gY - 15),
cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2)
# 得到结果
output.extend(groupOutput)
print(groupOutput)
21.最终效果
print("Credit Card Type: {}".format(FIRST_NUMBER[output[0]]))
print("Credit Card #: {}".format("".join(output)))
cv2.imshow("Image", image)
cv2.waitKey(0)
转载:https://blog.csdn.net/helloworld573/article/details/105541450
查看评论