import numpy as np
import argparse
import cv2
# 定义图片显示
def cv_show(name, img):
cv2.imshow(name, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def takeSecond(elem):
return elem[0]
# 定义模板的排序,默认从左到右
def sort_contours(cnts, method="left-to-right"):
reverse = False
if method == "right-to-left" or method == "bottom-to-top":
reverse = True
if method == "top-to-bottom" or method == "bottom-to-top":
i = 1
# 算出外接矩形的的坐标x,y,w,h
boundingBoxes = [cv2.boundingRect(c) for c in cnts]
# 将cnts和外接矩形的坐标轴根据第boundingBoxes[1][i]进行排序
(cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes), key=lambda b: b[1][i], reverse=reverse))
return cnts, boundingBoxes
# 对图像进行变换
def resize(image, width=None, height=None, inter=cv2.INTER_AREA):
dim = None
# 将图像二维数组分开为h、w
(h, w) = image.shape[:2]
# 如果width、height不带参数则直接返回图片
if width is None and height is None:
return image
# 如果w为空,则使用((height/h)*w,height)
if width is None:
r = height / float(h)
dim = (int(w * r), height)
else:
# 如果height为空,则使用(width,(width/w)*h)
r = width / float(w)
dim = (width, int(h * r))
resized = cv2.resize(image, dim, interpolation=inter)
return resized
# 读取模板文件
img = cv2.imread('ocr_a_reference.png')
# 将模板文件置为灰度图
ref = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 阈值处理,二值化
ref = cv2.threshold(ref, 10, 255, cv2.THRESH_BINARY_INV)[1]
# 轮廓检测,只检测最外面的轮廓,压缩水平的、垂直的和斜的部分,也就是说函数保留他们的重点部分
contours, hierarchy = cv2.findContours(ref.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 在原图像画出检测出的轮廓
cv2.drawContours(img, contours, -1, (0, 0, 255), 2)
# 排序,从左到右,从上到下
# refCnts = sort_contours(contours, method="left-to-right")[0]# 算出外接矩形的的坐标x,y,w,h
boundingBoxes = [cv2.boundingRect(c) for c in contours]
# 将cnts和外接矩形的坐标轴进行排序
refCnts = sorted(boundingBoxes, key=takeSecond, reverse=False)
digits = {}
# 遍历每一个模板轮廓
for (i, c) in enumerate(refCnts):
(x, y, w, h) = c
roi = ref[y:y + h, x:x + w]
roi = cv2.resize(roi, (60, 60))
# 每一个数字对应每一个模板
digits[i] = roi
cv_show('', roi)
# 初始化卷积核
rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 3))
sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
# 读取输入行用卡图像,预处理
image = cv2.imread('credit_card_01.png')
image = 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)
sobelx = cv2.convertScaleAbs(gradX)
grady = cv2.Sobel(tophat, ddepth=cv2.CV_32F, dx=0, dy=1, ksize=-1)
sobely = cv2.convertScaleAbs(grady)
sobelxy = cv2.addWeighted(sobelx, 0.5, sobely, 0.5, 0)
# 通过闭操作(先膨胀,再腐蚀)将数字连在一起
sobelxy = cv2.morphologyEx(sobelxy, cv2.MORPH_CLOSE, rectKernel)
# THRESH_OTSU会自动寻找合适的阈值,适合双峰,需把阈值参数设置为0
sobelxy = cv2.threshold(sobelxy, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
# 通过闭操作(先膨胀,再腐蚀)
thresh = cv2.morphologyEx(sobelxy, cv2.MORPH_CLOSE, sqKernel)
# 计算轮廓
threshCnts, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cur_img = image.copy()
# 把所有轮廓画在原图上
cv2.drawContours(cur_img, threshCnts, -1, (0, 0, 255), 3)
locs = []
# 遍历轮廓
for (i, c) in enumerate(threshCnts):
# 计算外接矩形
(x, y, w, h) = cv2.boundingRect(c)
# 计算长宽比
ar = w / float(h)
# 选择合适的区域,这里的基本都是四个数字一组
if ar > 2 and ar < 4:
if (w > 38 and w < 62) and (h > 10 and h < 24):
# 符合的留下来
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.resize(group, (60, 60))
group = group[0:56, 0:60]
# 阈值处理
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 = [cv2.boundingRect(c) for c in digitCnts]
# 将cnts和外接矩形的坐标轴根据第boundingBoxes[1][i]进行排序
digitCnts = sorted(digitCnts, key=takeSecond, reverse=False)
# 计算每一组中的每一个数值
for c in digitCnts:
# 找到当前数值的轮廓,resize成合适的的大小
(x, y, w, h) = c
roi = group[y:y + h, x:x + w]
roi = cv2.resize(roi, (60, 60))
# 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)))
# 画出来0
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)
cv_show('',image)
# 得到结果
output.extend(groupOutput)
print("Credit Card #: {}".format("".join(output)))
cv2.imshow("Image", image)
cv2.waitKey(0)