预处理图像以在python中进行QR检测 [英] Preprocessing images for QR detection in python
问题描述
我使用Zbar和OpenCV读取下图中的QR码,但两者均未检测到.对于ZBar,我将pyzbar库用作python包装器.有些图像可以正确检测到QR,而图像与失败的图像确实非常相似.我的手机摄像头可以读取上传的图像中的QR码,这意味着它是有效的.下面是代码片段:
来自pyzbar.pyzbar的 导入解码从pyzbar.pyzbar导入ZBarSymbol导入cv2#zbar结果=解码(cv2.imread(image_path),符号= [ZBarSymbol.QRCODE])打印(结果)#opencvqr_decoder = cv2.QRCodeDetector()数据,bbox,rectified_image = qr_decoder.detectAndDecode(cv2.imread(image_path))打印(数据,bbox)
哪种类型的预处理将有助于提高检测QR码的成功率?
zbar
(进行了一些预处理)未检测到QR码,您可以测试QR码以测试正在运行的 zbarimg image.jpg
.
良好的二值化在这里很有用.我使用
I used Zbar and OpenCV to read the QR code in the image below but both failed to detect it. For ZBar, I use pyzbar library as the python wrapper. There are images that QR is detected correctly and images really similar to the successful ones that fail. My phone camera can read the QR code in the uploaded image which means it is a valid one. Below is the code snippet:
from pyzbar.pyzbar import decode
from pyzbar.pyzbar import ZBarSymbol
import cv2
# zbar
results = decode(cv2.imread(image_path), symbols=[ZBarSymbol.QRCODE])
print(results)
# opencv
qr_decoder = cv2.QRCodeDetector()
data, bbox, rectified_image = qr_decoder.detectAndDecode(cv2.imread(image_path))
print(data, bbox)
What type of pre-processing will help to increase the rate of success for detecting QR codes?
zbar
, which does some preprocessing, does not detect the QR code, which you can test running zbarimg image.jpg
.
Good binarization is useful here. I got this to work using the kraken.binarization.nlbin()
function of the Kraken library. The library is for OCR, but works very well for QR codes, too, by using non-linear processing. The Kraken binarization code is here.
Here is the code for the sample:
from kraken import binarization
from PIL import Image
from pyzbar.pyzbar import decode
from pyzbar.pyzbar import ZBarSymbol
image_path = "image.jpg"
# binarization using kraken
im = Image.open(image_path)
bw_im = binarization.nlbin(im)
# zbar
decode(bw_im, symbols=[ZBarSymbol.QRCODE])
[Decoded(data=b'DE-AAA002065', type='QRCODE', rect=Rect(left=1429, top=361, width=300, height=306), polygon=[Point(x=1429, y=361), Point(x=1429, y=667), Point(x=1729, y=667), Point(x=1723, y=365)])]
The following picture shows the clear image of the QR code after binarization:
这篇关于预处理图像以在python中进行QR检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!