Reputation: 617
As stated in documentation:
IplImage.imageData (from OpenCV library):
A pointer to the aligned image data
Image.tostring() (from Python PIL):
Returns a string containing pixel data, using the standard "raw" encoder.
What is the difference between the results of Image.tostring()
and IplImage.imageData
? What should I do if I want to get exactly the same data as returned by IplImage.imageData
in Python ?
EDIT: Just to clarify, I need to use PIL to load images as I have a python service to receive data from internet and there's no straightforward way in OpenCV to load an image from memory instead of disk.
Upvotes: 1
Views: 5062
Reputation: 31
There is a straightforward way of reading images from memory in opencv.
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)
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)
Upvotes: 3
Reputation: 1881
While Opencv image, pixel data is ordered as BGR, in PIL it is RGB. You can convert a PIL image data to opencv as below.
import Image, cv
pi = Image.open('building.jpg') # PIL image
cv_im = cv.CreateImageHeader(pi.size, cv.IPL_DEPTH_8U, 3)
cv.SetData(cv_im, pi.tostring())
For more information here.
http://opencv.willowgarage.com/documentation/python/cookbook.html
Upvotes: 1