二维码识别
#导入标准库os
import os
#导入numpy
import numpy as np
#导入matplotlib
from matplotlib import pyplot as plt
#导入OpenCV
import cv2 as cv
src = cv.imread('tt.png')
#绘制热图
plt.imshow(src)
#图片显示
plt.show()
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
qrcoder = cv.QRCodeDetector()
codeinfo, points, straight_qrcode = qrcoder.detectAndDecode(gray)
print(points)
print(points[0][0])
result = np.copy(src)
cv.putText(result,'%s'%codeinfo,tuple(points[2][0]),cv.FONT_HERSHEY_SIMPLEX, 0.8,(0, 0, 255))
cv.drawContours(result, [np.int32(points)], 0, (0, 0, 255), 2)
print("qrcode : %s"% codeinfo)
#绘制热图
plt.imshow(result)
#图片显示
plt.show()
code_roi = np.uint8(straight_qrcode)
#绘制热图
plt.imshow(code_roi)
#图片显示
plt.show()
cv.waitKey(0)
cv.destroyAllWindows()
([[[ 43. 43. ]])]
[[[ 43. 43. ]]
[[406. 43. ]]
[[407.99347 407.99347]]
[[ 43. 406. ]]]
[43. 43.]
链接: link
解释
OpenCV3.4.4以上版本与OpenCV4.0版本支持该功能!
OpenCV在对象检测模块中QRCodeDetector有两个相关API分别实现二维码检测与二维码解析
二维码检测
retval, points = cv.QRCodeDetector.detect( img[, points] )
img输入图像,灰度或者彩色图像
points 得到的二维码四个点的坐标信息
解析二维码
retval, straight_qrcode = cv.QRCodeDetector.decode( img, points[, straight_qrcode] )
img输入图像,灰度或者彩色图像
points 二维码ROI最小外接矩形顶点坐标
qrcode 输出的是二维码区域ROI图像信息,返回的二维码utf-8字符串
上述两个API功能,可以通过一个API调用实现,该API如下:
retval, points, straight_qrcode = cv.QRCodeDetector.detectAndDecode(img[, points[, straight_qrcode]])