For a full list of BASHing data blog posts see the index page.
zbarimg and blurry QR codes
This is a tinkering post about zbarimg (from the zbar suite of barcode tools) and its ability to read QR code images. Because there are so many possible ways to produce an image of a QR code, I decided to start with a readable image and progressively degrade it, to see what happened.
I encoded the VCard text file "serano"
BEGIN:VCARD
VERSION:4.0
FN:Dr Ernest Serano
N:Serano;Ernest;Dr;
ORG:Serano Genomics
TITLE:CEO
EMAIL:mm1@sergen.com.jp
END:VCARD
with qrencode to generate a different QR code for each of the error-correcting levels, then grayscaled each of the black-white images with ImageMagick. Each code had a block size of 3 pixels, the qrencode default.
This is pseudo-code!
qrencode -l {L,M,Q,H} -o bw{L,M,Q,H}.png < serano
convert bw{L,M,Q,H}.png -type Grayscale gray{L,M,Q,H}.png
zbarimg had no trouble reading the smallest image, "grayL.png", shown below at its native 147x147 pixel size:
For blurring tests I used the ImageMagick blur function, varying just the sigma in 0.1 steps on "grayL.png" to produce a series of images, shown as samples below.
A "0" value for the radius parameter means ImageMagick finds the best radius for the given sigma.
for i in $(seq 0.1 0.1 3); do convert grayL.png -blur 0x"$i" grayL_"$i".png; done
At what sigma does zbarimg fail? Well, "fail" in this case is exit code 4, meaning No barcode was detected in one or more of the images. No other errors occurred (quote from the zbarimg man page). I fed each of the images to zbarimg and recorded the sigma and the exit code:
for j in $(seq 0.1 0.1 3); do zbarimg grayL_"$j".png &>/dev/null; \
echo "0x$j - $?"; done | column
So the sigma threshold for detectability in a blurred "grayL.png" is between 1.0 and 1.1:
Does error-correction level affect the blurring threshold? To find out I repeated the exercise above with the M, Q and H images. The three higher error-correction levels had the same somewhat higher sigma threshold:
Level | Size | Threshold |
L | 147x147 | 1.0 - 1.1 |
M | 171x171 | 1.4 - 1.5 |
Q | 183x183 | 1.4 - 1.5 |
H | 207x207 | 1.4 - 1.5 |
and it's impressive that zbarimg can read these blurry images correctly:
I've gotten some seriously unclear QR code images to a zbarimg-readable state by drastically increasing their contrast. In a future post I'll look more closely at the effect of contrast on readibility.
Last update: 2021-08-25
The blog posts on this website are licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License