我正在尝试使用 OPENCV 从 io.BytesIO() 结构加载图像。 最初,代码使用 PIL 加载图像,如下所示:
image_stream = io.BytesIO()
image_stream.write(connection.read(image_len))
image_stream.seek(0)
image = Image.open(image_stream)
print('Image is %dx%d' % image.size)
我试过像那样用 OPENCV 打开:
image_stream = io.BytesIO()
image_stream.write(connection.read(image_len))
image_stream.seek(0)
img = cv2.imread(image_stream,0)
cv2.imshow('image',img)
但是好像imread没有处理BytesIO()。我遇到了一个错误。
我正在使用 OPENCV 3.3 和 Python 2.7。请问,有人可以帮助我吗?
最佳答案
恩里克 试试这个:
import numpy as np
import cv2 as cv
import io
image_stream = io.BytesIO()
image_stream.write(connection.read(image_len))
image_stream.seek(0)
file_bytes = np.asarray(bytearray(image_stream.read()), dtype=np.uint8)
img = cv.imdecode(file_bytes, cv.IMREAD_COLOR)
关于python - 使用 opencv 加载 BytesIO 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46624449/