args = vars(ap.parse_args())
建立了一个参数解析器,添加两个参数,然后解析它们,将它们存储为变量 args 。 两个必需的命令行参数是:
–image :要进行 OCR 处理的图像的路径。
–reference :参考 OCR-A 图像的路径。 该图像包含 OCR-A 字体中的数字 0-9,从而允许我们稍后在管道中执行模板匹配。
接下来让我们定义信用卡类型:
define a dictionary that maps the first digit of a credit card
number to the credit card type
FIRST_NUMBER = {
“3”: “American Express”,
“4”: “Visa”,
“5”: “MasterCard”,
“6”: “Discover Card”
}
信用卡类型,例如美国运通、Visa 等,可以通过检查 16 位信用卡号中的第一位数字来识别。我们定义了一个字典 FIRST_NUMBER ,它将第一个数字映射到相应的信用卡类型。 让我们通过加载参考 OCR-A 图像来启动我们的图像处理管道:
load the reference OCR-A image from disk, convert it to grayscale,
and threshold it, such that the digits appear as white on a
black background
and invert it, such that the digits appear as white on a black
ref = cv2.imread(args[“reference”])
ref = cv2.cvtColor(ref, cv2.COLOR_BGR2GRAY)
ref = cv2.threshold(ref, 10, 255, cv2.THRESH_BINARY_INV)[1]
首先,我们加载参考 OCR-A 图像,然后将其转换为灰度和阈值 + 反转。 在这些操作中的每一个中,我们存储或覆盖 ref ,我们的参考图像。
现在让我们在 OCR-A 字体图像上定位轮廓:
find contours in the OCR-A image (i.e,. the outlines of the digits)
sort them from left to right, and initialize a dictionary to map
digit name to the ROI
refCnts = cv2.findContours(ref.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
refCnts = imutils.grab_contours(refCnts)
refCnts = contours.sort_contours(refCnts, method=“left-to-right”)[0]
digits = {}
找到了参考图像中的轮廓。 然后,由于 OpenCV 2.4、3 和 4 版本如何不同地存储返回的轮廓信息,我们检查版本并对 refCnts 进行适当更改。 接下来,我们从左到右对轮廓进行排序,并初始化一个字典,digits,它将数字名称映射到感兴趣的区域。
此时,我们应该遍历轮廓,提取ROI并将其与其对应的数字相关联:
loop over the OCR-A reference contours
for (i, c) in enumerate(refCnts):
compute the bounding box for the digit, extract it, and resize
it to a fixed size
(x, y, w, h) = cv2.boundingRect©
roi = ref[y:y + h, x:x + w]
roi = cv2.resize(roi, (57, 88))
update the digits dictionary, mapping the digit name to the ROI
digits[i] = roi
遍历参考图像轮廓。
在循环中, i 保存数字名称/编号, c 保存轮廓。 我们围绕每个轮廓 c 计算一个边界框,用于存储矩形的 (x, y) 坐标和宽度/高度。使用边界矩形参数从 ref(参考图像)中提取 roi。 该 ROI 包含数字。
我们将每个 ROI 大小调整为 57×88 像素的固定大小。 我们需要确保每个数字都调整为固定大小,以便在本教程后面的数字识别中应用模板匹配。
我们将每个数字 0-9(字典键)与每个 roi 图像(字典值)相关联。
在这一点上,我们完成了从参考图像中提取数字并将它们与相应的数字名称相关联的工作。
我们的下一个目标是隔离输入 --image 中的 16 位信用卡号。 我们需要先找到并隔离数字,然后才能启动模板匹配以识别每个数字。 这些图像处理步骤非常有趣且有见地,特别是如果您之前从未开发过图像处理管道,请务必密切关注。
让我们继续初始化几个结构化内核:
initialize a rectangular (wider than it is tall) and square
structuring kernel
rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 3))
sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
您可以将内核视为我们在图像上滑动的小矩阵,以执行(卷积)操作,例如模糊、锐化、边缘检测或其他图像处理操作。
构造了两个这样的内核——一个矩形和一个正方形。 我们将使用矩形作为 Top-hat 形态算子,使用方形作为闭运算。 我们很快就会看到这些。 现在让我们准备要进行 OCR 的图像:
load the input image, resize it, and convert it to grayscale
image = cv2.imread(args[“image”])
image = imutils.resize(image, width=300)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
加载了包含信用卡照片的命令行参数图像。 然后,我们将其调整为 width=300 ,保持纵横比,然后将其转换为灰度。 让我们看看我们的输入图像:
接下来是我们的调整大小和灰度操作:
现在我们的图像已经灰度化并且大小一致,让我们进行形态学操作:
apply a tophat (whitehat) morphological operator to find light
regions against a dark background (i.e., the credit card numbers)
tophat = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, rectKernel)
使用我们的 rectKernel 和我们的灰度图像,我们执行 Top-hat 形态学操作,将结果存储为 tophat。
Top-hat操作在深色背景(即信用卡号)下显示浅色区域,如下图所示:
给定我们的高帽图像,让我们计算沿 x 方向的梯度:
compute the Scharr gradient of the tophat image, then scale
the rest back into the range [0, 255]
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”)
我们努力隔离数字的下一步是计算 x 方向上高帽图像的 Scharr 梯度。完成计算,将结果存储为 gradX 。
在计算 gradX 数组中每个元素的绝对值后,我们采取一些步骤将值缩放到 [0-255] 范围内(因为图像当前是浮点数据类型)。 为此,我们计算 gradX 的 minVal 和 maxVal,然后计算第 73 行所示的缩放方程(即最小/最大归一化)。 最后一步是将 gradX 转换为范围为 [0-255] 的 uint8。 结果如下图所示:
让我们继续改进信用卡数字查找算法:
apply a closing operation using the rectangular kernel to help
cloes gaps in between credit card number digits, then apply
Otsu’s thresholding method to binarize the image
gradX = cv2.morphologyEx(gradX, cv2.MORPH_CLOSE, rectKernel)
thresh = cv2.threshold(gradX, 0, 255,
cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
apply a second closing operation to the binary image, again
to help close gaps between credit card number regions
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, sqKernel)
为了缩小差距,我们执行了一个关闭操作。请注意,我们再次使用了 rectKernel。 随后我们对 gradX 图像执行 Otsu 和二进制阈值,然后是另一个关闭操作。 这些步骤的结果如下所示:
接下来让我们找到轮廓并初始化数字分组位置列表。
find contours in the thresholded image, then initialize the
list of digit locations
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
locs = []
我们找到了轮廓并将它们存储在一个列表 cnts 中 。 然后,我们初始化一个列表来保存数字组位置。
现在让我们遍历轮廓,同时根据每个轮廓的纵横比进行过滤,允许我们从信用卡的其他不相关区域中修剪数字组位置:
loop over the contours
for (i, c) in enumerate(cnts):
compute the bounding box of the contour, then use the
bounding box coordinates to derive the aspect ratio
(x, y, w, h) = cv2.boundingRect©
ar = w / float(h)
since credit cards used a fixed size fonts with 4 groups
of 4 digits, we can prune potential contours based on the
aspect ratio
if ar > 2.5 and ar < 4.0:
contours can further be pruned on minimum/maximum width
and height
if (w > 40 and w < 55) and (h > 10 and h < 20):
append the bounding box region of the digits group
to our locations list
locs.append((x, y, w, h))
我们以与参考图像相同的方式循环遍历轮廓。 在计算每个轮廓的边界矩形 c之后,我们通过将宽度除以高度来计算纵横比 ar 。 使用纵横比,我们分析每个轮廓的形状。 如果 ar 介于 2.5 和 4.0 之间(宽大于高),以及 40 到 55 像素之间的 w 和 10 到 20 像素之间的 h,我们将一个方便的元组中的边界矩形参数附加到 locs。
下图显示了我们找到的分组——出于演示目的,我让 OpenCV 在每个组周围绘制了一个边界框:
接下来,我们将从左到右对分组进行排序并初始化信用卡数字列表:
sort the digit locations from left-to-right, then initialize the
list of classified digits
locs = sorted(locs, key=lambda x:x[0])
output = []
我们根据 x 值对 locs 进行排序,因此它们将从左到右排序。 我们初始化一个列表 output ,它将保存图像的信用卡号。 现在我们知道每组四位数字的位置,让我们循环遍历四个排序的组并确定其中的数字。
这个循环相当长,分为三个代码块——这是第一个块:
loop over the 4 groupings of 4 digits
for (i, (gX, gY, gW, gH)) in enumerate(locs):
initialize the list of group digits
groupOutput = []
extract the group ROI of 4 digits from the grayscale image,
then apply thresholding to segment the digits from the
background of the credit card
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]
detect the contours of each individual digit in the group,
then sort the digit contours from left to right
digitCnts = cv2.findContours(group.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
digitCnts = imutils.grab_contours(digitCnts)
digitCnts = contours.sort_contours(digitCnts,
method=“left-to-right”)[0]
在此循环的第一个块中,我们提取并在每侧填充组 5 个像素,应用阈值处理,并查找和排序轮廓。 详情请务必参考代码。 下面显示的是已提取的单个组:
让我们用嵌套循环继续循环以进行模板匹配和相似度得分提取:
loop over the digit contours
for c in digitCnts:
compute the bounding box of the individual digit, extract
the digit, and resize it to have the same fixed size as
the reference OCR-A images
(x, y, w, h) = cv2.boundingRect©
roi = group[y:y + h, x:x + w]
roi = cv2.resize(roi, (57, 88))
initialize a list of template matching scores
scores = []
loop over the reference digit name and digit ROI
for (digit, digitROI) in digits.items():
apply correlation-based template matching, take the
score, and update the scores list
result = cv2.matchTemplate(roi, digitROI,
cv2.TM_CCOEFF)
(_, score, _, _) = cv2.minMaxLoc(result)
scores.append(score)
the classification for the digit ROI will be the reference
digit name with the largest template matching score
groupOutput.append(str(np.argmax(scores)))
一、Python所有方向的学习路线
Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
二、学习软件
工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。
三、入门学习视频
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!