How to get the x, y position of a detected QR code

2020-08-08 12:56发布

I encoded the number 1639 in the two QR codes of the image below (downloadable here). I printed it, took a photo and tried to detect it:

import zbar
from PIL import Image 

scanner = zbar.ImageScanner()
pil = Image.open('20180520_170027_2.jpg').convert('L')
width, height = pil.size
raw = pil.tobytes()
image = zbar.Image(width, height, 'Y800', raw)
result = scanner.scan(image)

for symbol in image:
    print symbol.data.decode(u'utf-8')     # 1639

It works, even if the size of the QR code is small (~1x1 cm), which is great!

Question: how to get the x, y position of the corners of the QR codes?

It's sure that zbar has this information internally (mandatory to be able to decode the QR code!), but how to get access to it?

Note: here is how to install zbar on Windows and Python 2.7

enter image description here

2条回答
我想做一个坏孩纸
2楼-- · 2020-08-08 13:49

It looks like the zbar::Symbol class in the C++ docs of zlib has the methods get_location_x(), get_location_y() and get_location_size(), so your intuition that this data exists underneath was right.

Coming back to Python, when reading the documentation of the zbar Python binding, it looks like a position field is available to get the location of the QR code:

import zbar
image = read_image_into_numpy_array(...) # whatever function you use to read an image file into a numpy array
scanner = zbar.Scanner()
results = scanner.scan(image)
for result in results:
    print(result.type, result.data, result.quality, result.position)

The size of the QR code is probably also available as a field in result (e.g. result.size), and you can use it to find the 3 other corners.

查看更多
再贱就再见
3楼-- · 2020-08-08 13:54

As suggested by a hint in a comment,

print(symbol.location)

gives the coordinates.

查看更多
登录 后发表回答