C++ 将 cv::Mat 转换为 IplImage*
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4664187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting cv::Mat to IplImage*
提问by amr
The documentation on this seems incredibly spotty.
关于此的文档似乎非常参差不齐。
I've basically got an empty array of IplImage*s (IplImage** imageArray) and I'm calling a function to import an array of cv::Mats - I want to convert my cv::Mat into an IplImage* so I can copy it into the array.
我基本上有一个 IplImage*s (IplImage** imageArray) 的空数组,我正在调用一个函数来导入一个 cv::Mats 数组 - 我想将我的 cv::Mat 转换为 IplImage* 所以我可以将其复制到数组中。
Currently I'm trying this:
目前我正在尝试这个:
while(loop over cv::Mat array)
{
IplImage* xyz = &(IplImage(array[i]));
cvCopy(iplimagearray[i], xyz);
}
Which generates a segfault.
这会产生段错误。
Also trying:
也在尝试:
while(loop over cv::Mat array)
{
IplImage* xyz;
xyz = &array[i];
cvCopy(iplimagearray[i], xyz);
}
Which gives me a compile time error of:
error: cannot convert ‘cv::Mat*' to ‘IplImage*' in assignment
这给了我一个编译时错误:
error: cannot convert ‘cv::Mat*' to ‘IplImage*' in assignment
Stuck as to how I can go further and would appreciate some advice :)
不知道我如何才能走得更远,希望得到一些建议:)
采纳答案by YeenFei
cv::Mat
is the new type introduce in OpenCV2.X while the IplImage*
is the "legacy" image structure.
cv::Mat
是 OpenCV2.X 中引入的新类型,而IplImage*
是“遗留”图像结构。
Although, cv::Mat
does support the usage of IplImage
in the constructor parameters, the default library does not provide function for the other way. You will need to extract the image header information manually. (Do remember that you need to allocate the IplImage structure, which is lack in your example).
虽然,cv::Mat
确实支持IplImage
在构造函数中使用参数,但默认库不提供其他方式的功能。您将需要手动提取图像标题信息。(请记住,您需要分配 IplImage 结构,这在您的示例中是缺失的)。
回答by LovaBill
Mat image1;
IplImage* image2=cvCloneImage(&(IplImage)image1);
Guess this will do the job.
猜猜这会完成这项工作。
Edit:If you face compilation errors, try this way:
编辑:如果您遇到编译错误,请尝试以下方法:
cv::Mat image1;
IplImage* image2;
image2 = cvCreateImage(cvSize(image1.cols,image1.rows),8,3);
IplImage ipltemp=image1;
cvCopy(&ipltemp,image2);
回答by user2103629
(you have cv::Mat old)
IplImage copy = old;
IplImage* new_image = ©
you work with new as an originally declared IplImage*.
您使用 new 作为最初声明的 IplImage*。
回答by minhazur
回答by octavian
Personaly I think it's not the problem caused by type casting but a buffer overflow problem; it is this line
我个人认为这不是类型转换引起的问题,而是缓冲区溢出问题;这是这条线
cvCopy(iplimagearray[i], xyz);
that I think will cause segment fault, I suggest that you confirm the array iplimagearray[i] have enough size of buffer to receive copyed data
我认为会导致段错误,我建议您确认数组 iplimagearray[i] 有足够的缓冲区大小来接收复制的数据
回答by chayan
According to OpenCV cheat-sheetthis can be done as follows:
根据 OpenCV备忘单,这可以按如下方式完成:
IplImage* oldC0 = cvCreateImage(cvSize(320,240),16,1);
Mat newC = cvarrToMat(oldC0);
The cv::cvarrToMatfunction takes care of the conversion issues.
该品种:: cvarrToMat函数采用的转换问题护理。
回答by ksolid
In case of gray image, I am using this function and it works fine! however you must take care about the function features ;)
在灰度图像的情况下,我正在使用此功能并且效果很好!但是你必须注意功能特性;)
CvMat * src= cvCreateMat(300,300,CV_32FC1);
IplImage *dist= cvCreateImage(cvGetSize(dist),IPL_DEPTH_32F,3);
cvConvertScale(src, dist, 1, 0);
回答by Timm
One problem might be: when using external ipl and defining HAVE_IPL in your project, the ctor
一个问题可能是:当使用外部 ipl 并在您的项目中定义 HAVE_IPL 时,构造函数
_IplImage::_IplImage(const cv::Mat& m)
{
CV_Assert( m.dims <= 2 );
cvInitImageHeader(this, m.size(), cvIplDepth(m.flags), m.channels());
cvSetData(this, m.data, (int)m.step[0]);
}
found in ../OpenCV/modules/core/src/matrix.cpp is not used/instanciated and conversion fails.
在 ../OpenCV/modules/core/src/matrix.cpp 中找到未使用/实例化并且转换失败。
You may reimplement it in a way similar to :
您可以以类似于以下方式重新实现它:
IplImage& FromMat(IplImage& img, const cv::Mat& m)
{
CV_Assert(m.dims <= 2);
cvInitImageHeader(&img, m.size(), cvIplDepth(m.flags), m.channels());
cvSetData(&img, m.data, (int)m.step[0]);
return img;
}
IplImage img;
FromMat(img,myMat);