【问题标题】:can't decode QR codes from OpenCV image after it has been converted to PIL转换为 PIL 后,无法从 OpenCV 图像中解码 QR 码
【发布时间】:2012-11-14 15:57:28
【问题描述】:

我一直在使用 OpenCV 方法从我的相机中获取图像。我想使用 zbar 库从这些图像中解码 QR 码,但是在我将图像转换为 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()

【问题讨论】:

    标签: python opencv python-imaging-library zbar


    【解决方案1】:

    您无需将 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
    

    看来您基本上必须执行以下操作才能使其工作:

    • 使用 LoadImageM 代替 LoadImage
    • 确保图像是灰度的,所以使用 cv.CV_LOAD_IMAGE_GRAYSCALE

    【讨论】:

      【解决方案2】:

      如果你在 python 中工作,我建议你看看SimpleCV。您可以使用我们的条形码读取实现,也可以自己使用该库。 Here is the source 用于使用 zbar 拉出条形码。

      【讨论】:

      • 我想避免使用简单的 CV,因为我一直在将开放式 CV 用于其他目的,并且我不想重新实现我已经完成的功能。有一个简单的 CV 方法,我想找到它的等价物。这是 .getpil() 方法。
      • SimpleCV 应该可以与 OpenCV 很好地互操作(因为它是建立在 OpenCV 之上的),您唯一关心的是依赖关系。如果你能处理好这些部门,你应该没问题。
      • 嗯,更多的是我正在对一个开放的 CV 捕获的图像做一些事情,比如颜色跟踪,我需要使用相同的图像来进行解码。
      猜你喜欢
      • 2012-12-17
      • 2012-08-25
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 2017-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多