Sug*_*ara 5 python opencv qr-code image-processing
我在使用 Pyzbar 检测二维码时遇到问题。在完美条件下,我能够使用原始 png 图像检测二维码。但是,当我从相机进行视频捕获,然后将该帧保存为图像时,pyzbar 无法检测到 QR 码。
例如,这有效
[Decoded(data=b'GOAL', type='QRCODE', rect=Rect(left=16, top=16, width=168, height=168))]
Run Code Online (Sandbox Code Playgroud)
但即使在我手动裁剪周围环境以仅显示二维码后,以下内容也不会。
[]
Run Code Online (Sandbox Code Playgroud)
对于这两个图像,我正在使用
decode(image, scan_locations=True)
Run Code Online (Sandbox Code Playgroud)
我想知道我需要做什么才能让 pyzbar 解码我的二维码图像?
用于OpenCV将图像阈值设为黑白,然后pyzbar能够解码 QR 码。
首先,使用以下代码对图像进行阈值处理。
from pyzbar import pyzbar
import argparse
import numpy as np
import cv2
image =cv2.imread("QRCode.png")
# thresholds image to white in back then invert it to black in white
# try to just the BGR values of inRange to get the best result
mask = cv2.inRange(image,(0,0,0),(200,200,200))
thresholded = cv2.cvtColor(mask,cv2.COLOR_GRAY2BGR)
inverted = 255-thresholded # black-in-white
Run Code Online (Sandbox Code Playgroud)
以下是处理后的图片。
和,
barcodes = pyzbar.decode(inverted)
print (barcodes)
Run Code Online (Sandbox Code Playgroud)
打印输出显示解码类型为QRCODE,数据为GOAL。
[Decoded(data='GOAL', type='QRCODE', rect=Rect(left=5, top=13, width=228, height=212),
polygon=[Point(x=5, y=222), Point(x=233, y=225), Point(x=220, y=19), Point(x=13, y=13)])]
Run Code Online (Sandbox Code Playgroud)
希望这有帮助。