How to get the x, y position of a detected QR code on an image with zbar?

余生颓废 提交于 2019-12-12 10:19:24

问题


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


回答1:


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.




回答2:


As suggested by a hint in a comment,

print(symbol.location)

gives the coordinates.



来源:https://stackoverflow.com/questions/50436423/how-to-get-the-x-y-position-of-a-detected-qr-code-on-an-image-with-zbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!