如何使用 zbar 获取图像上检测到的二维码的 x、y 位置?
How to get the x, y position of a detected QR code on an image with zbar?
我在下图的两个二维码中编码了数字 1639(可下载 here)。我把它打印出来,拍照并尝试检测它:
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
即使 QR 码的尺寸很小(~1x1 厘米)也能正常工作,太棒了!
问题:如何获取二维码角点的x,y位置?
肯定zbar
内部有这个信息(必须能够解码QR码!),但是如何访问它?
注意:here is how to install zbar
在 Windows 和 Python 2.7
根据评论中的提示,
print(symbol.location)
给出坐标。
看起来 zlib 的 C++ 文档中的 zbar::Symbol
class 有方法 get_location_x()
、get_location_y()
和 get_location_size()
,所以你的直觉是此数据存在于下方是正确的。
回到 Python,当读取 zbar Python 绑定的 documentation 时,看起来 position
字段可用于获取二维码:
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)
QR码的大小可能也可以作为result
中的一个字段(例如result.size
),您可以用它来找到其他3个角。
我在下图的两个二维码中编码了数字 1639(可下载 here)。我把它打印出来,拍照并尝试检测它:
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
即使 QR 码的尺寸很小(~1x1 厘米)也能正常工作,太棒了!
问题:如何获取二维码角点的x,y位置?
肯定zbar
内部有这个信息(必须能够解码QR码!),但是如何访问它?
注意:here is how to install zbar
在 Windows 和 Python 2.7
根据评论中的提示,
print(symbol.location)
给出坐标。
看起来 zlib 的 C++ 文档中的 zbar::Symbol
class 有方法 get_location_x()
、get_location_y()
和 get_location_size()
,所以你的直觉是此数据存在于下方是正确的。
回到 Python,当读取 zbar Python 绑定的 documentation 时,看起来 position
字段可用于获取二维码:
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)
QR码的大小可能也可以作为result
中的一个字段(例如result.size
),您可以用它来找到其他3个角。