信用卡识别项目中:轮廓排序函数解读

1.refCnts中存放了轮廓信息,执行轮廓排序

refCnts = myutils.sort_contours(refCnts, method="left-to-right")[0]
# 排序,从左到右,从上到下

2.myutils.sort_contours函数详解

def sort_contours(cnts, method="left-to-right"):
    reverse = False
    i = 0

    if method == "right-to-left" or method == "bottom-to-top":
        reverse = True

    if method == "top-to-bottom" or method == "bottom-to-top":
        i = 1
    boundingBoxes = [cv2.boundingRect(c) for c in cnts]
    # 计算外接矩形,用一个最小的矩形,把找到的形状包起来x,y,h,w
    # 对 "cnts" 中的每个轮廓,计算其外接矩形,并将外接矩形存储在 "boundingBoxes" 元组中
    (cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes),
                                        key=lambda b: b[1][i], reverse=reverse))

    return cnts, boundingBoxes

对核心代码的详解

(cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes), key=lambda b: b[1][i], reverse=reverse))

zip 函数:它接受多个可迭代对象作为参数,并将它们的元素打包成一个元组的列表。
sorted 函数:它接受一个可迭代对象作为参数,并返回该对象中元素的排序后的列表。
lambda 表达式:它是一种匿名函数(anonymous function),用于临时定义函数。

这段代码的作用是:将两个列表 cnts 和 boundingBoxes 中的元素对应地打包成一个元组列表,并将这个列表按照边界框的左上角坐标排序。然后,将排序后的列表中的元组解压缩(unpack),并分别赋值给变量 cnts 和 boundingBoxes。

具体来说:
1. 使用 **zip** 函数将 cnts 和 boundingBoxes 中的元素对应地打包成元组,形成一个元组列表。

2. 使用 **sorted** 函数对这个元组列表进行排序。
排序的顺序由参数 reverse 确定,
reverse = True,倒序排序; reverse = False,正序排序。
排序的依据是使用 lambda 表达式定义的匿名函数 lambda b: b[1][0]。
这个函数的输入是一个元组 b,输出是该元组的第二个元素的第一个元素(即边界框的左上角坐标)。因此,排序后的列表中,元素的顺序就是按照边界框的左上角坐标排序的。

3. 使用多元赋值语句将排序后的列表中的元组解压缩,并分别赋值给变量 cnts 和 boundingBoxes。

注意:这段代码使用了 Python 的解压缩语法。具体来说,在多元赋值语句的左边使用了星号(*)操作符,用于将排序后的列表中的元组解压缩。例如,如果有一个列表 [(1, 2), (3, 4), (5, 6)],那么将其解压缩后,就可以得到三个变量 a、b 和 c,分别被赋值为 1、3 和 5。

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
from imutils import contours import numpy as np import argparse import cv2 as cv import myutils def cv_show(name,img): cv.imshow(name,img) cv.waitKey(0) cv.destroyAllWindows() # 先处理template tempalte_img = cv.imread("E:/opencv/picture/ocr_a_reference.png") tempalte_gray = cv.cvtColor(tempalte_img, cv.COLOR_BGR2GRAY) tempalte_thres = cv.threshold(tempalte_gray, 0, 255, cv.THRESH_OTSU | cv.THRESH_BINARY_INV)[1] temp_a, tempalte_contours, temp_b = cv.findContours(tempalte_thres.copy (), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) cv.drawContours(tempalte_img, tempalte_contours, -1, (0, 255, 0), 2) tempalte_contours = contours.sort_contours(tempalte_contours, method="left-to-right")[0] digits = {} # 构建一个字典 for (i, c) in enumerate(tempalte_contours): (x, y, w, h) = cv.boundingRect(c) tempalte_roi = tempalte_thres[y:y + h, x:x + w] #之前一直检测不出正确答案,原因是这里的roi应该是tempalte_thres一部分 #而不是template_gray的一部分! tempalte_roi = cv.resize(tempalte_roi, (57, 88)) digits[i] = tempalte_roi cv_show('template_single',tempalte_roi) #cv_show('template_single',tempalte_roi) #对银行卡进行处理,之所以要做成数字长条,是想通过长条的尺寸比例大小来将自己想要的数字给抠出来。 rectkernel = cv.getStructuringElement(cv.MORPH_RECT,(9,3)) squrkernel = cv.getStructuringElement(cv.MORPH_RECT,(5,5)) image = cv.imread("E:/opencv/picture/credit_card_02.png") image = myutils.resize(image, width=300) image_gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY) image_tophat= cv.morphologyEx(image_gray,cv.MORPH_TOPHAT,rectkernel) image_close = cv.morphologyEx(image_tophat,cv.MORPH_CLOSE,rectkernel) cv.imshow("image_tophat",image_tophat) cv.imshow('image_close',image_close) image_thres= cv.threshold(image_close,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU)[1] image_contours= cv.findContours(image_thres.copy(),cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)[1] locs = [] for(n,con) in enumerate(image_contours): (gx,

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值