目录
- 实现流程
- 模板数字处理
- 对模板图片进行二值化处理
- 对二值化后的图片进行轮廓检测
- 确定每个模板数字的图像
- 银行卡图片处理
- 预处理
- 确定每组数字
- 轮廓检测每个数字
- 提取数字组中的数字
- 模板匹配
- 附录
- 完整代码
实现流程
- 对数字模板进行处理,提取出单一数字的图片;
数字模板图片:
单一数字图片示例:
- 对银行卡图片进行预处理,提取出每组数字图像;
银行卡图片:
一组数字图片示例:
- 对每组数字图片,提取出单个数字图片,与模板图片进行模板匹配,寻找最优匹配结果,找到对应的数字。
单个数字图片示例:
模板数字处理
数字模板图片:
对模板图片进行二值化处理
#对模板二值化处理
ret , thre_temple = cv2.threshold(temple_gray, 200, 255, cv2.THRESH_BINARY_INV)
二值化后图片:
对二值化后的图片进行轮廓检测
#轮廓检测
temple_contours , hierarchy = cv2.findContours(thre_temple.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
draw_con_temple = cv2.drawContours(temple, temple_contours, -1, (0, 0, 250), 1)
#print(np.array(temple_contours).shape)
cv_show('draw_con_temple', draw_con_temple)
轮廓检测效果:
确定每个模板数字的图像
根据各个轮廓寻找最小外接矩形,根据最小外接矩形的 y y y坐标的大小,对每个数字图片提取并存放在字典的对应序号中。
def get_contours_sort(contours):#找到外接矩形,根据y坐标大小boundingboxs = [cv2.boundingRect(cnt) for cnt in contours]bounding_boxs = sorted(boundingboxs)return bounding_boxs
#确定各个数字模板位置
bounding_boxs = get_contours_sort(temple_contours)#对每个模板提取并存储对应的数字
temple_difits = {}
for (i, c) in enumerate(bounding_boxs):(x, y, w, h) = croi = thre_temple[y:y+h , x:x+w ]#resize成合适大小roi = cv2.resize(roi, (50, 77))temple_difits[i] = roicv_show('temple_number', temple_difits[0])
单个数字图片示意:
银行卡图片处理
预处理
对银行卡图片进行各种预处理操作,滤除背景影响,同时使得每组数字连在一起;
#礼帽操作,突出更明亮的区域
tophat_goal = cv2.morphologyEx(goal, cv2.MORPH_TOPHAT, rectKernel)
cv_show('credit_card', tophat_goal)#sobel算子,边缘检测
gradX = cv2.Sobel(tophat_goal, ddepth = cv2.CV_32F, dx = 1, dy = 0)
gradX = np.abs(gradX)
(maxval , minval) = (np.max(gradX) , np.min(gradX))
gradX = (255*(gradX-minval)/(maxval-minval))
gradX = gradX.astype("uint8")
cv_show('sobel', gradX)#闭操作
close_goal = cv2.morphologyEx(gradX, cv2.MORPH_CLOSE, rectKernel)
cv_show('close', close_goal)#二值化
ret , thresh_goal = cv2.threshold(close_goal, 0, 255, cv2.THRESH_OTSU)
cv_show('otsu', thresh_goal)#闭操作
close2_goal = cv2.morphologyEx(thresh_goal, cv2.MORPH_CLOSE, rectKernel)
cv_show('close2', close_goal)#二值化
ret , thre_goal = cv2.threshold(close2_goal, 200, 255, cv2.THRESH_BINARY)
cv_show('threshold', thre_goal)
要达到的效果示意:
如上图所示,各组数字连在了一起,同时背景也被滤掉。
确定每组数字
对预处理后的图片进行轮廓检测,根据轮廓外接矩形的长宽比确定每组数字位置。
#轮廓检测
goal_contours , hierarchy = cv2.findContours(thre_goal.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
draw_con_goal = cv2.drawContours(color_goal.copy(), goal_contours, -1, (0, 0, 250), 1)
#print(np.array(temple_contours).shape)
cv_show('draw_con_goal', draw_con_goal)goal_boxs = find_goal_part(goal_contours)
num_of_num_part = goal_boxs.shape[0]
#print(goal_boxs)
#存储每个数字片段
goal_difits = {}
color_goal_difits = {}
for i in range(num_of_num_part):(x, y, w, h) = goal_boxs[i,:]roi = goal[y-5:y+h+5 , x-5:x+w+5 ]color_roi = color_goal[y-5:y+h+5 , x-5:x+w+5 ]color_goal_difits[i] = color_roigoal_difits[i] = roi
def find_goal_part(contours):#找到外接矩形,根据长宽比进行选取boundingboxs = [cv2.boundingRect(cnt) for cnt in contours]rate = []for (i , c) in enumerate(boundingboxs):(x, y, w, h) = crate.append(w/h)rate = np.array(rate)number_gath_id = np.argwhere((rate >= 3.55) & (rate <= 3.75))boundingboxs = np.array(boundingboxs)bounding_boxs = boundingboxs[number_gath_id]bounding_boxs = bounding_boxs[:,0]sort_id = np.argsort(bounding_boxs[:,0])bounding_boxs = bounding_boxs[sort_id]return bounding_boxs
提取出的一组数字图片示例:
轮廓检测每个数字
对提取出的每一组图片进行二值化处理,并进行轮廓检测;
#对数字区域二值化处理
ret , num_part_temple = cv2.threshold(number_part, 200, 255, cv2.THRESH_BINARY)
cv_show('num_part_temple', num_part_temple)#轮廓检测
num_part_contours , hierarchy = cv2.findContours(num_part_temple.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
draw_con_num_part = cv2.drawContours(color_num_part.copy(), num_part_contours, -1, (0, 0, 250), 1)
#print(np.array(draw_con_num_part).shape)
#cv_show('draw_con_num_part', draw_con_num_part)
二值化效果:
提取数字组中的数字
根据轮廓的外接矩形的 y y y坐标大小按顺序提取每个数字
#确定各个数字位置
num_part_boxs = get_contours_sort(num_part_contours)
num_in_num_part = np.array(num_part_boxs).shape[0]#对每个数字提取
num_part_difits = {}
for (i, c) in enumerate(num_part_boxs):(x, y, w, h) = croi = num_part_temple[y:y+h , x:x+w ]#resize成合适大小roi = cv2.resize(roi, (50, 77))cv_show('roi', roi)
提取出的数字:
模板匹配
对提取的数字进行模板匹配,最佳匹配图片序号即为当前图片数字。
for j in range(10):match_result[j] = cv2.matchTemplate(roi, temple_difits[j], cv2.TM_SQDIFF_NORMED)
min_val ,max_val ,min_id ,max_id = cv2.minMaxLoc(match_result)
result.append(min_id[1])
输出结果:
附录
完整代码
#识别银行卡上数字import cv2
import numpy as np
import matplotlib.pyplot as pltdef cv_show(name,img):cv2.imshow(name, img)cv2.waitKey(0)cv2.destroyAllWindows()def get_contours_sort(contours):#找到外接矩形,根据x位置排序boundingboxs = [cv2.boundingRect(cnt) for cnt in contours]bounding_boxs = sorted(boundingboxs)return bounding_boxsdef find_goal_part(contours):#找到外接矩形,根据长宽比进行选取boundingboxs = [cv2.boundingRect(cnt) for cnt in contours]rate = []for (i , c) in enumerate(boundingboxs):(x, y, w, h) = crate.append(w/h)rate = np.array(rate)number_gath_id = np.argwhere((rate >= 3.55) & (rate <= 3.75))boundingboxs = np.array(boundingboxs)bounding_boxs = boundingboxs[number_gath_id]bounding_boxs = bounding_boxs[:,0]sort_id = np.argsort(bounding_boxs[:,0])bounding_boxs = bounding_boxs[sort_id]return bounding_boxstemple = cv2.imread('./orc_credit_num/temple.png' , 1)
temple_gray = cv2.imread('./orc_credit_num/temple.png' , 0)
goal = cv2.imread('orc_credit_num/credit_card_picture.png' , 0)
color_goal = cv2.imread('./orc_credit_num/temple.png' , 1)cv_show('temple', temple_gray)#对模板二值化处理
ret , thre_temple = cv2.threshold(temple_gray, 200, 255, cv2.THRESH_BINARY_INV)
cv_show('thre_temple', thre_temple)#轮廓检测
temple_contours , hierarchy = cv2.findContours(thre_temple.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
draw_con_temple = cv2.drawContours(temple, temple_contours, -1, (0, 0, 250), 1)
#print(np.array(temple_contours).shape)
cv_show('draw_con_temple', draw_con_temple)#确定各个数字模板位置
bounding_boxs = get_contours_sort(temple_contours)#对每个模板提取并存储对应的数字
temple_difits = {}
for (i, c) in enumerate(bounding_boxs):(x, y, w, h) = croi = thre_temple[y:y+h , x:x+w ]#resize成合适大小roi = cv2.resize(roi, (50, 77))temple_difits[i] = roicv_show('temple_number', temple_difits[0])#对目标图像进行操作
#初始化卷积核
rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (30,15))
sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))cv_show('credit_card', goal)#礼帽操作,突出更明亮的区域
tophat_goal = cv2.morphologyEx(goal, cv2.MORPH_TOPHAT, rectKernel)
cv_show('credit_card', tophat_goal)#sobel算子,边缘检测
gradX = cv2.Sobel(tophat_goal, ddepth = cv2.CV_32F, dx = 1, dy = 0)
gradX = np.abs(gradX)
(maxval , minval) = (np.max(gradX) , np.min(gradX))
gradX = (255*(gradX-minval)/(maxval-minval))
gradX = gradX.astype("uint8")
cv_show('sobel', gradX)#闭操作
close_goal = cv2.morphologyEx(gradX, cv2.MORPH_CLOSE, rectKernel)
cv_show('close', close_goal)#二值化
ret , thresh_goal = cv2.threshold(close_goal, 0, 255, cv2.THRESH_OTSU)
cv_show('otsu', thresh_goal)#闭操作
close2_goal = cv2.morphologyEx(thresh_goal, cv2.MORPH_CLOSE, rectKernel)
cv_show('close2', close_goal)#二值化
ret , thre_goal = cv2.threshold(close2_goal, 200, 255, cv2.THRESH_BINARY)
cv_show('threshold', thre_goal)#轮廓检测
goal_contours , hierarchy = cv2.findContours(thre_goal.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
draw_con_goal = cv2.drawContours(color_goal.copy(), goal_contours, -1, (0, 0, 250), 1)
#print(np.array(temple_contours).shape)
cv_show('draw_con_goal', draw_con_goal)goal_boxs = find_goal_part(goal_contours)
num_of_num_part = goal_boxs.shape[0]
#print(goal_boxs)
#存储每个数字片段
goal_difits = {}
color_goal_difits = {}
for i in range(num_of_num_part):(x, y, w, h) = goal_boxs[i,:]roi = goal[y-5:y+h+5 , x-5:x+w+5 ]color_roi = color_goal[y-5:y+h+5 , x-5:x+w+5 ]color_goal_difits[i] = color_roigoal_difits[i] = roicv_show('goal_difits', goal_difits[3])
result = []
for k in range(num_of_num_part):#开始识别数字number_part = goal_difits[k]color_num_part = color_goal_difits[k]#对数字区域二值化处理ret , num_part_temple = cv2.threshold(number_part, 200, 255, cv2.THRESH_BINARY)cv_show('num_part_temple', num_part_temple)#轮廓检测num_part_contours , hierarchy = cv2.findContours(num_part_temple.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)draw_con_num_part = cv2.drawContours(color_num_part.copy(), num_part_contours, -1, (0, 0, 250), 1)#print(np.array(draw_con_num_part).shape)#cv_show('draw_con_num_part', draw_con_num_part)#确定各个数字位置num_part_boxs = get_contours_sort(num_part_contours)num_in_num_part = np.array(num_part_boxs).shape[0]#对每个数字提取num_part_difits = {}for (i, c) in enumerate(num_part_boxs):(x, y, w, h) = croi = num_part_temple[y:y+h , x:x+w ]#resize成合适大小roi = cv2.resize(roi, (50, 77))cv_show('roi', roi)match_result = np.zeros((10,1))for j in range(10):match_result[j] = cv2.matchTemplate(roi, temple_difits[j], cv2.TM_SQDIFF_NORMED)min_val ,max_val ,min_id ,max_id = cv2.minMaxLoc(match_result)result.append(min_id[1])print(result)