Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV: Converting from NumPy to IplImage in Python

Tags:

python

opencv

I have an image that I load using cv2.imread(). This returns an NumPy array. However, I need to pass this into a 3rd party API that requires the data in IplImage format.

I've scoured everything I could and I've found instances of converting from IplImage to CvMat,and I've found some references to converting in C++, but not from NumPy to IplImage in Python. Is there a function that is provided that can do this conversion?

like image 475
steve8918 Avatar asked Jul 17 '12 17:07

steve8918


1 Answers

You can do like this.

source = cv2.imread() # source is numpy array 
bitmap = cv.CreateImageHeader((source.shape[1], source.shape[0]), cv.IPL_DEPTH_8U, 3)
cv.SetData(bitmap, source.tostring(), 
           source.dtype.itemsize * 3 * source.shape[1])

bitmap here is cv2.cv.iplimage

like image 168
Froyo Avatar answered Nov 04 '22 01:11

Froyo