本文介绍了用opencv加载BytesIO映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从io.BytesIO()结构中使用OPENCV加载图像.最初,代码使用PIL加载图像,如下所示:
I'm trying to load an image with OPENCV from an io.BytesIO() structure.Originally, the code loads the image with PIL, like below:
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打开
I tried to open with OPENCV like that:
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.拜托,有人可以帮我吗?
But it seems that imread doesn't deal with BytesIO(). I'm getting an error.
I'm using OPENCV 3.3 and Python 2.7. Please, could someone help me?
推荐答案
Henrique试试这个:
HenriqueTry this:
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)
这篇关于用opencv加载BytesIO映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!