opencv-图像各种变换和信用卡识别学习记录

在使用OpenCV进行傅里叶变换和信用卡数字识别时遇到问题,包括matplotlib.hist的AttributeError和边界提取的ValueError。解决方法包括正确导入matplotlib.pyplot,调整OpenCV版本或修改代码以适应新版本。在信用卡识别案例中,成功解决版本不兼容和代码拼写错误问题,强调遇到问题时记录的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

遇到的问题

1.傅里叶变换中使用matplotlib中hist遇到的Attribute Error问题
运行以下两行代码时,
在这里插入图片描述
报错,Attribute Error:module “matplotlib” has no attribute “hist”

解决办法
模块导入那里本来是 import matplotlib as plt, 改为 import matplotlib.pyplot as plt
在这里插入图片描述
或者
在这里插入图片描述
即可解决

2. 信用卡数字识别案例
进行边界提取的时候总是报错
ValueError: not enough values to unpack (expected 3, got 2)

解决办法
这个是版本问题,OpenCV旧版,返回三个参数,新版只需要两个,可能看的教程比较老,两种方法可以解决
①把OpenCV 降级成3.4.3.18 就可以了,在终端输入pip install opencv-python==3.4.3.18
②删去第一个多余的参数

以此图为例,删去ref_即可在这里插入图片描述

小插曲
在这里插入图片描述
使用np.uint8的时候一直报错,检查了一遍又一遍,就是找不到原因,困扰自己两天之后,今天突然发现,是因为我把uint打成了unit…被自己蠢哭o( ̄┰ ̄*)ゞ

总结

第一个信用卡识别的案例算是跑通了,期间遇到了很多问题,就一个个慢慢搜索,解决,有个体会就是遇到或解决一个问题一定要当场记录下来,不然就像今天,明明要记录很多问题来着,到写的时候发现基本都忘记了,只记得费时较多的大问题,小问题也是很重要的啊~

害,又是为毕业努力的一天,加油(ง •_•)ง

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、付费专栏及课程。

余额充值