CV2 OpenCL,cv2.UMat 对象不可迭代
CV2 OpenCL, cv2.UMat object is not iterable
我正在使用 pyzbar 库来解码二维码。现在我正在尝试使用 uMat 来加快这个过程。问题是 pyzbar 解码不能接受 umat 变量。
File "C:\Python\lib\site-packages\pyzbar\pyzbar.py", line 175, in decode
pixels, width, height = image
TypeError: 'cv2.UMat' object is not iterable
这是我的代码示例
import cv2
import numpy as np
from pyzbar.pyzbar import decode
import matplotlib.pyplot as plt
cv2.ocl.setUseOpenCL(True)
for subdir, dirs, files in os.walk("Images"):
for file in sorted(files):
filepath = subdir + os.sep + file
if filepath.endswith(".JPG"):
image = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
image = cv2.UMat(image)
symbols = decode(image)
if symbols:
plt.title(symbols[0][0])
plt.imshow(image)
plt.show()
我猜这个错误的原因可以在 decode from pyzbar 的文档中找到:
def decode(image, symbols=None, scan_locations=False):
"""Decodes datamatrix barcodes in `image`.
Args:
image: `numpy.ndarray`, `PIL.Image` or tuple (pixels, width, height)
symbols (ZBarSymbol): the symbol types to decode; if `None`, uses
`zbar`'s default behaviour, which is to decode all symbol types.
scan_locations (bool): If `True`, results will include scan
locations.
解码需要包含特定顺序数据的图像或矩阵,由 OpenCV 支持的 mat 实例填充。 UMat格式不满足这个要求所以会出现错误。
我正在使用 pyzbar 库来解码二维码。现在我正在尝试使用 uMat 来加快这个过程。问题是 pyzbar 解码不能接受 umat 变量。
File "C:\Python\lib\site-packages\pyzbar\pyzbar.py", line 175, in decode pixels, width, height = image TypeError: 'cv2.UMat' object is not iterable
这是我的代码示例
import cv2
import numpy as np
from pyzbar.pyzbar import decode
import matplotlib.pyplot as plt
cv2.ocl.setUseOpenCL(True)
for subdir, dirs, files in os.walk("Images"):
for file in sorted(files):
filepath = subdir + os.sep + file
if filepath.endswith(".JPG"):
image = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
image = cv2.UMat(image)
symbols = decode(image)
if symbols:
plt.title(symbols[0][0])
plt.imshow(image)
plt.show()
我猜这个错误的原因可以在 decode from pyzbar 的文档中找到:
def decode(image, symbols=None, scan_locations=False):
"""Decodes datamatrix barcodes in `image`.
Args:
image: `numpy.ndarray`, `PIL.Image` or tuple (pixels, width, height)
symbols (ZBarSymbol): the symbol types to decode; if `None`, uses
`zbar`'s default behaviour, which is to decode all symbol types.
scan_locations (bool): If `True`, results will include scan
locations.
解码需要包含特定顺序数据的图像或矩阵,由 OpenCV 支持的 mat 实例填充。 UMat格式不满足这个要求所以会出现错误。