简体   繁体   English

将二进制图像直接读取到 OpenCV Mat / numpy mat

[英]Reading a binary image directly to OpenCV Mat / numpy mat

Through some http requests I have been able to receive an image in binary form as b'\xff\xd8\xff\xe0\x00\... and with:通过一些 http 请求,我已经能够接收二进制形式的图像b'\xff\xd8\xff\xe0\x00\...并具有:

with open('image.jpg', 'wb') as out_file:
    out_file.write(binary_content)

where binary_content is a string containing the data received through request I saved an image into a file.其中binary_content是一个字符串,其中包含通过请求接收到的数据,我将图像保存到文件中。

Afterwards I can read this image with OpenCV methods.之后我可以使用 OpenCV 方法读取此图像。 But I wanted to do a direct pass from binary string to OpenCV Mat without any in-betweens.但我想直接从二进制字符串传递到 OpenCV Mat 而没有任何中间。 cv2.decode method didn't work. cv2.decode方法不起作用。

io.BytesIO and PIL worked well. io.BytesIOPIL运行良好。 Closing this q.关闭这个 q。

If you want to stay in the SciPy ecosystem, then the imageio library (previously part of SciPy) works well.如果您想留在 SciPy 生态系统中,那么imageio库(以前是 SciPy 的一部分)可以很好地工作。

from imageio import imread

image_array = imread("image_path.jpg")

The code above gives you an uint8 array, if you want a float array, you can cast it easily上面的代码给了你一个 uint8 数组,如果你想要一个浮点数组,你可以很容易地转换它

from imageio import imread

image_array = imread("image_path.jpg").astype(float)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM