How to convert a Mat variable type in an IplImage variable type in OpenCV 2.0?

我是研究僧i 提交于 2019-11-28 09:57:19

In the new OpenCV 2.0 C++ interface it's not really necessary to change from back and forth between Mat and IplImage, but if you want to you can use the IplImage operator:

IplImage dst_img = dst;

Note that only the IplImage header is created and the data (pixels) will be shared. For more info see the OpenCV C++ interface or the image.cpp example in the OpenCV-2.0/samples/c directory.

For having the whole IplImage object, I've used this code:

Mat matImage;
IplImage* iplImage;

iplImage = cvCreateImage(cvSize(640,480), IPL_DEPTH_8U, 1);
iplImage->imageData = (char *) matImage.data;

You can also copy the data instead of pointer:

memcpy(iplImage->imageData, matimage.data, 640*480);

Norman in his blog describes the following (Although it is not 2.0, it should apply to your problem.):

To transform from CvMat to IplImage, Use function:

IplImage* cvGetImage( const CvArr* arr, IplImage* image_header );  

The function cvGetImage returns image header for the input array that can be matrix - CvMat*, or image - IplImage*. In the case of image the function simply returns the input pointer. In the case of CvMat* it initializes image_header structure with parameters of the input matrix. Usage:

IplImage stub, *dst_img;
dst_img = cvGetImage(src_mat, &stub);

In later versions of OpenCV 2.4 and above, we can convert it simply by

cv::Mat inMat;
//do the stuffs
IplImage* outIPL = (IplImage*)(&IplImage(inMat));
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!