gpt4 book ai didi

c++ - Python PIL Image.tostring ('raw' ) 和 OpenCV 的 IplImage->imageData

转载 作者:太空宇宙 更新时间:2023-11-03 22:06:42 24 4
gpt4 key购买 nike

如文档中所述:

IplImage.imageData(来自 OpenCV 库):

A pointer to the aligned image data

Image.tostring()(来自 Python PIL):

Returns a string containing pixel data, using the standard "raw" encoder.

Image.tostring()IplImage.imageData 的结果有什么区别?如果我想得到与Python中IplImage.imageData返回的完全一样的数据怎么办?

编辑:澄清一下,我需要使用 PIL 来加载图像,因为我有一个 python 服务来从互联网接收数据,而 OpenCV 中没有直接的方法从内存而不是磁盘加载图像。

最佳答案

在 opencv 中有一种从内存中读取图像的直接方法。

使用 cv(版本 1)

import cv

# First get the compressed image data. This can be retrieved from
# a socket, a file, or whatever you want.
jpegdata = open('myimage.jpg','r').read()

# Create an opencv matrix to hold the compressed data
cvmat = cv.CreateMatHeader(1, len(jpegdata), cv.CV_8U)
cv.SetData(cvmat, jpegdata, len(jpegdata))

# Now let opencv decompress your image
cvimage = cv.DecodeImage(cvmat, cv.CV_LOAD_IMAGE_COLOR)

使用 cv2(和 numpy)

import cv2
import numpy as np

# First get the compressed image data. This can be retrieved from
# a socket, a file, or whatever you want.
jpegdata = open('myimage.jpg','r').read()

# Convert your compressed data to a numpy array
nparr = np.fromstring(jpegdata, np.uint8)

# Now use imdecode to decompress it
cvimage = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR)

关于c++ - Python PIL Image.tostring ('raw' ) 和 OpenCV 的 IplImage->imageData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11824542/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com