Preprocessing images for QR detection in python

Question:

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?

the image attached

Asked By: MFA

||

Answers:

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:

Binarized picture

Answered By: seanpue

I had a similar issue, and Seanpue’s answer got me on the right track for this problem. Since I was already using the OpenCV library for image processing rather than PIL, I used it to perform Otsu’s Binarization using the directions in an OpenCV tutorial on Image Thresholding. Here’s my code:

import cv2
from pyzbar.pyzbar import decode
from pyzbar.pyzbar import ZBarSymbol

image_path = "qr.jpg"
# preprocessing using opencv
im = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
blur = cv2.GaussianBlur(im, (5, 5), 0)
ret, bw_im = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# zbar
decode(bw_im, symbols=[ZBarSymbol.QRCODE])

[Decoded(data=b'DE-AAA002065', type='QRCODE', rect=Rect(left=1429, top=362, width=300, height=305), polygon=[Point(x=1429, y=362), Point(x=1430, y=667), Point(x=1729, y=667), Point(x=1724, y=366)])]

Applying the gaussian blur is supposed to remove noise from the picture to make the binarization more effective, but for my application it didn’t actually make much difference. What was vital was to convert the image to grayscale to make the threshold function work (done here by opening the file with the cv2.IMREAD_GRAYSCALE flag).

Answered By: JRI

QReader use to work quite well for these cases.

from qreader import QReader
import cv2

if __name__ == '__main__':
    # Initialize QReader
    detector = QReader()
    img = cv2.cvtColor(cv2.imread('92iKG.jpg'), cv2.COLOR_BGR2RGB)
    # Detect and Decode the QR
    print(detector.detect_and_decode(image=img))

This code output for this QR:

DE-AAA002065
Answered By: Haru Kaeru