垫子-> IplImage * 转换与数据复制
我有以下功能。我想将一些数据从 Mat
复制回 IplImage *
输入并返回给主控。 这很愚蠢,但我找不到合适的方法来做到这一点!骗子 工作表没有提及 Mat->IplImage *
与数据的转换 复制(因为我在函数之外需要它)。
任何想法或指针表示赞赏。 最好的 ——阿里
int test(IplImage **srcImage, int num_images)
{
vector<Mat> images(num_images);
for (int i = 0; i < num_images; ++i)
{
images[i] = Mat(srcImage[i]); // I guess should be correct!
....
// some manipulations on images[i]
}
// this should be wrong!
for (int i = 0; i < num_images; ++i)
{
cvReleaseImage(&srcImage[i]);
srcImage[i] = new IplImage(images[i]);
images[i].clear();
}
return 0;
}
I have following function. I want to copy back some data from Mat
toIplImage *
type and return it to the main control.
This is silly, but I couldn't find a proper way to do this! The cheat
sheet havent say anything about Mat->IplImage *
conversion WITH data
copy (since I need it outside the function).
Any idea or pointer is appreciated.
Best
--Ali
int test(IplImage **srcImage, int num_images)
{
vector<Mat> images(num_images);
for (int i = 0; i < num_images; ++i)
{
images[i] = Mat(srcImage[i]); // I guess should be correct!
....
// some manipulations on images[i]
}
// this should be wrong!
for (int i = 0; i < num_images; ++i)
{
cvReleaseImage(&srcImage[i]);
srcImage[i] = new IplImage(images[i]);
images[i].clear();
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短版本:转换为临时
IplImage
,然后使用cvCopy
。但是,您的代码存在几个问题:
到目前为止,是的。
如果操作就地(不重新分配
Mat
),则不需要将数据复制回来,因为Mat
构造函数没有复制首先是数据。如果你确实重新分配,那么......这可能会出现问题。
images[i]
可能仍在使用相同的内存。new IplImage
不会给你带来任何好处。它没有有意义的构造函数,请使用cvCreateImage
。这是不必要的,因为向量无论如何都会超出范围。
最后一个循环应该如下所示:
Short version: Convert to a temporary
IplImage
, then usecvCopy
.However, there are several issues with your code:
so far, yes.
If the manipulations are in-place (don't reallocate the
Mat
s), you don't need to copy the data back, since theMat
constructor didn't copy the data in the first place. If you do reallocate, then ...This can be problematic.
images[i]
might still be using the same memory.new IplImage
isn't going to do you any good. It doesn't have meaningful constructors, usecvCreateImage
.This is not neccessary, as the vector goes out of scope anyway.
The last loop should look something like this: