垫子-> IplImage * 转换与数据复制

发布于 2024-11-09 16:40:25 字数 692 浏览 0 评论 0原文

我有以下功能。我想将一些数据从 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 to
IplImage * 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 技术交流群。

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

发布评论

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

评论(1

晨曦慕雪 2024-11-16 16:40:25

简短版本:转换为临时 IplImage,然后使用 cvCopy

但是,您的代码存在几个问题:

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] 

如果操作就地(不重新分配 Mat),则不需要将数据复制回来,因为 Mat 构造函数没有复制首先是数据。如果你确实重新分配,那么......

  } 

  // this should be wrong!
  for (int i = 0; i < num_images; ++i) 
  { 
     cvReleaseImage(&srcImage[i]);

这可能会出现问题。 images[i] 可能仍在使用相同的内存。

     srcImage[i] = new IplImage(images[i]);

new IplImage 不会给你带来任何好处。它没有有意义的构造函数,请使用cvCreateImage

     images[i].clear();

这是不必要的,因为向量无论如何都会超出范围。

   } 
  return 0;
 }

最后一个循环应该如下所示:

for (int i = 0; i < num_images; ++i) { 
     IplImage* old = srcImage[i]; // store pointer for later deallocation in case of shared data
     IplImage src = images[i];
     srcImage[i] = cvCreateImage(...); // fill appropriate parameters here. If you didn't change size/type/etc, you might not need to create/deallocate(*)
     cvCopy(&src, srcImage[i]);
     cvReleaseImage(&old); // now it is safe to deallocate(*)
} 

Short version: Convert to a temporary IplImage, then use cvCopy.

However, there are several issues with your code:

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!

so far, yes.

      .... 
     // some manipulations on images[i] 

If the manipulations are in-place (don't reallocate the Mats), you don't need to copy the data back, since the Mat constructor didn't copy the data in the first place. If you do reallocate, then ...

  } 

  // this should be wrong!
  for (int i = 0; i < num_images; ++i) 
  { 
     cvReleaseImage(&srcImage[i]);

This can be problematic. images[i] might still be using the same memory.

     srcImage[i] = new IplImage(images[i]);

new IplImage isn't going to do you any good. It doesn't have meaningful constructors, use cvCreateImage.

     images[i].clear();

This is not neccessary, as the vector goes out of scope anyway.

   } 
  return 0;
 }

The last loop should look something like this:

for (int i = 0; i < num_images; ++i) { 
     IplImage* old = srcImage[i]; // store pointer for later deallocation in case of shared data
     IplImage src = images[i];
     srcImage[i] = cvCreateImage(...); // fill appropriate parameters here. If you didn't change size/type/etc, you might not need to create/deallocate(*)
     cvCopy(&src, srcImage[i]);
     cvReleaseImage(&old); // now it is safe to deallocate(*)
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文