在 opencv 中有一种直接从内存中读取图像的方法。
使用简历(版本 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)