任务:使用opencv利用模板实现对信用卡卡号的识别
模板图像如图:
使用opencv对图像处理得到图像对应的数字并使用字典存储
def template_process(img):
temp_img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# 转换颜色
ret, temp_img = cv.threshold(temp_img, 127, 255, cv.THRESH_BINARY_INV)
# show('temp', temp_img)
# 模板轮廓检测
contours, hierarchy = cv.findContours(temp_img, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
# 画出轮廓-->在原来的图像上才能画出彩色
res = cv.drawContours(img, contours, -1, (255, 0, 0), 3)
show('temp-counteour', res)
# 对轮廓进行排序
contours, bounding_loc = sort_contour(contours)
# 图形与数字匹配
digitals = {}
for i in range(len(bounding_loc)):
x, y, w, h = bounding_loc[i]
digit_area = temp_img[y:y + h, x:x + w]
digit_area = cv.resize(digit_area, (70, 120))
# show("num", digit_area)
digitals[i] = digit_area
return digitals
处理信用卡流程:
信用卡原图:
对信用卡进行顶帽操作突出数字区域
计算水平梯度将4个数字为一组让其更像一个块
进行闭操作将数字连在一起