Skip to content

Commit 8569b6a

Browse files
committed
模板匹配 fix1
1 parent 7fc56f2 commit 8569b6a

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

my01-OCR文字识别/使用-模板匹配-识别信用卡号码/matchTemplate_credit_card_num1.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
Credit Card Type: Visa
1515
Credit Card #: 4000123456789010
1616
17+
18+
检测图像中信用卡的位置。
19+
本地化四位数字,与信用卡上十六位数相关。
20+
应用OCR来识别信用卡上的十六位数字。
21+
识别信用卡类型(即Visa,万事达卡,美国运通等)。
22+
1723
"""
1824

1925
# import the necessary packages
@@ -23,7 +29,7 @@
2329
import imutils
2430
import cv2
2531

26-
# construct the argument parser and parse the arguments
32+
# construct the argument parser and parse the arguments解析命令行参数
2733
ap = argparse.ArgumentParser()
2834
ap.add_argument("-i", "--image", required=True,
2935
help="path to input image")
@@ -49,13 +55,20 @@
4955
ref = cv2.cvtColor(ref, cv2.COLOR_BGR2GRAY)
5056
ref = cv2.threshold(ref, 10, 255, cv2.THRESH_BINARY_INV)[1]
5157

58+
cv2.imshow('ref',ref)
59+
cv2.waitKey(0)
60+
61+
'''
5262
# find contours in the OCR-A image (i.e,. the outlines of the digits)
5363
# sort them from left to right, and initialize a dictionary to map
5464
# digit name to the ROI
55-
refCnts = cv2.findContours(ref.copy(), cv2.RETR_EXTERNAL,
56-
cv2.CHAIN_APPROX_SIMPLE)
57-
refCnts = refCnts[0] if imutils.is_cv2() else refCnts[1]
65+
# refCnts = cv2.findContours(ref.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)#有问题
66+
refCnts = cv2.findContours(ref.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
67+
# refCnts = refCnts[0] if imutils.is_cv2() else refCnts[1]
68+
refCnts = refCnts[1]
69+
print('len cnt:',len(refCnts))
5870
refCnts = contours.sort_contours(refCnts, method="left-to-right")[0]
71+
print('sort_contours len cnt:',len(refCnts))
5972
digits = {}
6073
6174
# 循环浏览轮廓,提取ROI并将其与相应的数字相关联
@@ -66,11 +79,29 @@
6679
(x, y, w, h) = cv2.boundingRect(c)
6780
roi = ref[y:y + h, x:x + w]
6881
roi = cv2.resize(roi, (57, 88))
82+
cv2.imshow('roi', roi)
83+
cv2.waitKey(500)
6984
7085
# update the digits dictionary, mapping the digit name to the ROI
7186
digits[i] = roi
7287
# 从参考图像中提取数字,并将其与相应的数字名称相关联
88+
print('digits:',digits.keys())
89+
'''
7390

91+
#try1
92+
digits = {}
93+
rows,cols=ref.shape
94+
per=int(cols/10)
95+
for x in range(10):
96+
roi = ref[:, x*per:(x+1)*per]
97+
roi = cv2.resize(roi, (57, 88))
98+
cv2.imshow('roi', roi)
99+
cv2.waitKey(500)
100+
101+
# update the digits dictionary, mapping the digit name to the ROI
102+
digits[x] = roi
103+
# 从参考图像中提取数字,并将其与相应的数字名称相关联
104+
print('digits:',digits.keys())
74105

75106
# 初始化一对结构化的内核:
76107
# initialize a rectangular (wider than it is tall) and square
@@ -155,8 +186,7 @@
155186
digitCnts = cv2.findContours(group.copy(), cv2.RETR_EXTERNAL,
156187
cv2.CHAIN_APPROX_SIMPLE)
157188
digitCnts = digitCnts[0] if imutils.is_cv2() else digitCnts[1]
158-
digitCnts = contours.sort_contours(digitCnts,
159-
method="left-to-right")[0]
189+
# digitCnts = contours.sort_contours(digitCnts,method="left-to-right")[0]
160190

161191
# loop over the digit contours
162192
for c in digitCnts:
@@ -191,7 +221,7 @@
191221
output.extend(groupOutput)
192222

193223
# display the output credit card information to the screen
194-
print("Credit Card Type: {}".format(FIRST_NUMBER[output[0]]))
224+
print("Credit Card Type: {}".format(FIRST_NUMBER.get(output[0],'None')))
195225
print("Credit Card #: {}".format("".join(output)))
196226
cv2.imshow("Image", image)
197227
cv2.waitKey(0)

0 commit comments

Comments
 (0)