【问题标题】:Why is this Open CV code leaking?为什么这个 Open CV 代码会泄露?
【发布时间】:2014-06-29 13:37:11
【问题描述】:

我写了一些代码来在图像的左右两侧添加 10% 的灰色条,如下所示:

// Create image 20% wider
cv::Mat widenedImage(image.rows,
                     image.cols * 1.2,
                     CV_8UC1, 
                     127); // Grey colour

// Make a region of interest in the middle of the new image
cv::Mat toROI(widenedImage, cv::Rect((widenedImage.cols - image.cols) / 2.0,
                                     0,
                                     image.cols,
                                     image.rows));

// Copy the image to the region of interest 
image.copyTo(toROI);

无需代码,直接使用image,应用运行良好。添加后,XCode 的内存图不会增长,但我收到了几个警告,然后是这条消息。

有什么想法吗?

【问题讨论】:

  • 图片的类型是CV_8UC1?
  • @Robert:你能不能也分享一下警告信息。此外,您是否可以尝试使用尺寸小于当前图像的图像。
  • 您是否尝试过在cv::Rect() 之前和widenedImage.. 中的列初始化期间使用static_cast<size_t>
  • @marol - 是的,所有图像都是一个通道的 8 位图像。我还仔细检查了NSAssert(image.type()== CV_8UC1, @"");
  • @MantoshKumar - 消息在屏幕截图中。图像是来自相机的一帧,我真的不想对其进行下采样。

标签: c++ ios opencv


【解决方案1】:

我通常会这样创建toROI

cv::Mat toROI(widenedImage(cv::Rect((widenedImage.cols - image.cols) / 2.0,
                                 0,
                                 image.cols,
                                 image.rows)));

或者,如果您以后不需要toROI,我建议您这样做:

image.copyTo(widenedImage( cv::Rect((widenedImage.cols - image.cols) / 2.0,
                                 0,
                                 image.cols,
                                 image.rows)));

或者考虑使用copyMakeBorder()

此外,您的浮点除法可能会产生舍入错误。尝试将大小值保存为整数之前使用它们。

【讨论】:

  • 感谢您的建议,但两者仍然产生相同的崩溃:(。我这样做的 copyMakeBorder 是这样的:int borderSize = image.cols * 0.1;cv::copyMakeBorder(image, image, 0, 0, borderSize, borderSize, cv::BORDER_CONSTANT);
猜你喜欢
  • 2012-06-06
  • 1970-01-01
  • 1970-01-01
  • 2012-11-06
  • 2012-01-30
  • 2013-04-27
  • 1970-01-01
  • 1970-01-01
  • 2011-10-21
相关资源
最近更新 更多