如何在OpenCV 2.0中将Mat变量类型转换为IplImage变量类型?

发布于 2024-08-25 18:21:58 字数 380 浏览 3 评论 0原文

我正在尝试在 OpenCV 中旋转图像。

我使用了在 Stack Overflow 上找到的这段代码:

Mat source(img);
Point2f src_center(source.cols/2.0, source.rows/2.0);
Mat rot_mat = getRotationMatrix2D(src_center, 40.0, 1.0);
Mat dst;
warpAffine(source, dst, rot_mat, source.size());

一旦我填满了 dst Mat 变量类型,我想将其放回 IplImage 变量类型,知道如何做到这一点吗?

I am trying to rotate an image in OpenCV.

I've used this code that I found here on Stack Overflow:

Mat source(img);
Point2f src_center(source.cols/2.0, source.rows/2.0);
Mat rot_mat = getRotationMatrix2D(src_center, 40.0, 1.0);
Mat dst;
warpAffine(source, dst, rot_mat, source.size());

Once I have my dst Mat variable type filled up I would like to put it back to an IplImage variable type, any idea about how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

年少掌心 2024-09-01 18:21:58

在新的 OpenCV 2.0 C++ 接口中,实际上没有必要在 MatIplImage 之间来回更改,但如果您愿意,可以使用 IplImage< /code> 运算符:

IplImage dst_img = dst;

请注意,仅创建 IplImage 标头并且将共享数据(像素)。有关详细信息,请参阅 OpenCV C++ 接口图片OpenCV-2.0/samples/c 目录中的 .cpp 示例。

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.

绾颜 2024-09-01 18:21:58

为了拥有整个 IplImage 对象,我使用了以下代码:

Mat matImage;
IplImage* iplImage;

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

您还可以复制数据而不是指针:

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

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);
寄与心 2024-09-01 18:21:58

Norman 在他的博客中描述了以下内容(虽然它不是 2.0,但它应该适用于您的问题。):

要从 CvMat 转换为 IplImage,请使用函数:

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

函数 cvGetImage 返回输入数组的图像头,该输入数组可以是矩阵 - CvMat* 或图像 - IplImage*。
对于图像,该函数仅返回输入指针。
在 CvMat* 的情况下,它使用输入矩阵的参数初始化 image_header 结构。
用法:

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

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);
仅此而已 2024-09-01 18:21:58

在OpenCV 2.4及以上版本中,我们可以简单地通过以下方式转换它

cv::Mat inMat;
//do the stuffs
IplImage* outIPL = (IplImage*)(&IplImage(inMat));

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文