本文主要对OpenCV4.7.0和4.6.0中QRCode检测/解码功能做简单的测试对比,供大家参考。
背景介绍
最近OpenCV更新到了4.7.0版本,在ChangeLog算法部分除了新增Stackblur滤波算法(详细介绍见下面链接),还有对QRCode检测和解码的改进。
吊打高斯模糊的StackBlur来了!
QRCode部分按照changelog说法是对检测和解码质量做了改进,添加对齐标志支持,算法还是依赖WeChatQRCode,关于WeChatQRCode在OpenCV中的C++使用方法,可以参考下面链接,本文主要以Python OpenCV来做演示说明。
OpenCV4.5.1新增微信QRCode解码功能使用步骤与测评 附源码和效果视频
OpenCV4.7 WeChatQRCode使用
【1】OpenCV4.7.0安装(必须安装opencv-contrib)
pip install opencv-python==4.7.0.68
pip install opencv-contrib-python==4.7.0.68
【2】下载模型文件(检测模型和超分辨率模型):
https://github.com/WeChatCV/opencv_3rdparty
【3】图片测试:
# WX公众号:OpenCV与AI深度学习
import cv2
print('cv2.__version__:',cv2.__version__)
detect_proto = './model/detect.prototxt'
detect_model = './model/detect.caffemodel'
sr_proto = './model/sr.prototxt'
sr_model = './model/sr.caffemodel'
detect_obj = cv2.wechat_qrcode_WeChatQRCode(detect_proto,detect_model,sr_proto,sr_model)
img = cv2.imread('imgs/8.jpg')
res,points = detect_obj.detectAndDecode(img)
print('result',res)
print('corners:',points)
for pos in points:
color=(0,0,255)
thick=2
for p in [(0,1),(1,2),(2,3),(3,0)]:
start = int(pos[p[0]][0]),int(pos[p[0]][1])
end = int(pos[p[1]][0]),int(pos[p[1]][1])
cv2.line(img,start,end,color,thick)
cv2.imshow('img',img)
cv2.imwrite('result.jpg',img)
cv2.waitKey()
cv2.destroyAllWindows()
测试结果:
其他图片:
【4】视频测试:
# WX公众号:OpenCV与AI深度学习
import cv2
print('cv2.__version__:',cv2.__version__)
detect_proto = './model/detect.prototxt'
detect_model = './model/detect.caffemodel'
sr_proto = './model/sr.prototxt'
sr_model = './model/sr.caffemodel'
detect_obj = cv2.wechat_qrcode_WeChatQRCode(detect_proto,detect_model,sr_proto,sr_model)
def qr_decode(img):
res,points = detect_obj.detectAndDecode(img)
print('result',res)
for pos in points:
color=(0,0,255)
thick=2
for p in [(0,1),(1,2),(2,3),(3,0)]:
start = int(pos[p[0]][0]),int(pos[p[0]][1])
end = int(pos[p[1]][0]),int(pos[p[1]][1])
cv2.line(img,start,end,color,thick)
cap = cv2.VideoCapture('test.mp4')
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('out.mp4',fourcc, 15.0, (1280,720))
if(cap.isOpened()): #视频打开成功
while(True):
ret,frame = cap.read()#读取一帧
#if(frame is None):
if ret==False: #读取帧失败
break
qr_decode(frame)
cv2.imshow("qrcode decoder", frame)
out.write(frame)
if cv2.waitKey(10)&0xFF ==27: #按下Esc键退出
break
cap.release()
out.release()
cv2.waitKey()
cv2.destroyAllWindows()
OpenCV4.6与OpenCV4.7 QRCode解码功能对比
OpenCV4.7.0 VS 4.6.0
【1】OpenCV4.6.0安装
pip install opencv-python==4.6.0.66
pip install opencv-contrib-python==4.6.0.66
【2】图片测试(模型和代码相同):
上面三张图片4.6.0和4.7.0都可以正确解码,测试一下图片,4.6.0不能解码的,4.7.0也不能,直观看起来解码能力并未有明显提升。检测部分增进了对齐标记支持,所以标注的位置4.7.0比4.6.0更准确。
对比检测解码时间发现4.7.0比4.6.0略有增加,这部分未改进。
OpenCV4.6.0视频测试效果:
OpenCV4.6与OpenCV4.7 QRCode解码功能对比
—THE END—