我能够成功地从 zip 加载图像:
with zipfile.ZipFile('test.zip', 'r') as zfile:
data = zfile.read('test.jpg')
# how to open this using imread or imdecode?
问题是:如何使用 imread 或 imdecode 打开它以便在 opencv 中进一步处理而不先保存图像?
Update:
这是我得到的预期错误。我需要将“数据”转换为 opencv 可以使用的类型。
data = zfile.read('test.jpg')
buf = StringIO.StringIO(data)
im = cv2.imdecode(buf, cv2.IMREAD_GRAYSCALE)
# results in error: TypeError: buf is not a numpy array, neither a scalar
a = np.asarray(buf)
cv2.imdecode(a, cv2.IMREAD_GRAYSCALE)
# results in error: TypeError: buf data type = 17 is not supported
use numpy.frombuffer() http://docs.scipy.org/doc/numpy/reference/generated/numpy.frombuffer.html从字符串创建 uint8 数组:
import zipfile
import cv2
import numpy as np
with zipfile.ZipFile('test.zip', 'r') as zfile:
data = zfile.read('test.jpg')
img = cv2.imdecode(np.frombuffer(data, np.uint8), 1)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)