我一直在使用OpenCV方法从我的相机中获取图像。我想使用zbar库从这些图像中解码二维码,但在我将图像转换为PIL以供zbar处理后,解码似乎不起作用。
import cv2.cv as cv
import zbar
from PIL import Image
cv.NamedWindow("camera", 1)
capture = cv.CaptureFromCAM(0)
while True:
img = cv.QueryFrame(capture)
cv.ShowImage("camera", img)
if cv.WaitKey(10) == 27:
break
# create a reader
scanner = zbar.ImageScanner()
# configure the reader
scanner.parse_config('enable')
# obtain image data
pil = Image.fromstring("L", cv.GetSize(img), img.tostring())
width, height = pil.size
raw = pil.tostring()
# wrap image data
image = zbar.Image(width, height, 'Y800', raw)
# scan the image for barcodes
scanner.scan(image)
# extract results
for symbol in image:
# do something useful with results
print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data
cv.DestroyAllWindows()发布于 2013-01-15 07:26:26
您不需要将OpenCV图像转换为PIL图像,以便将其与zbar一起使用。您可以直接从OpenCV图像转到zbar,并完全避免使用PIL。
现在我不知道当图像源来自相机时,你是如何做到这一点的,但如果你从磁盘加载图像,你所要做的就是:
cv_img = cv.LoadImageM(image, cv.CV_LOAD_IMAGE_GRAYSCALE)
width = cv_img.width
height = cv_img.height
raw = cv_img.tostring()
# wrap image data
image = zbar.Image(width, height, 'Y800', raw)
# scan the image for barcodes
scanner.scan(image)
# extract results
for symbol in image:
# do something useful with results
print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data看起来你基本上必须做以下事情才能让它起作用:
发布于 2012-11-27 22:51:01
如果你正在使用python,我建议你看看SimpleCV。您可以模仿我们的条形码读取实现,也可以自己使用该库。用于使用zbar拉出条形码的Here is the source。
https://stackoverflow.com/questions/13577302
复制相似问题