转换成Mat后我是否需要释放IplImage?

时间:2015-11-05 12:36:18

标签: c++ opencv memory-management memory-leaks

我在代码中的几个地方执行以下操作;

cv::Mat cv_proc_image = ...
IplImage ipl_img = cv_proc_image;
cvSmooth(&ipl_img, &ipl_img, smooth_type, smooth_param1);
cv_proc_image = cv::cvarrToMat(&ipl_img);

在观察到此操作的变化后,我观察到内存膨胀。你认为,我应该在最后一次分配后释放IplImages分配的内存吗?

2 个答案:

答案 0 :(得分:6)

您不应该首先使用旧的C语法。您应该使用C ++语法。另请注意APIGEE Snapshot for LinkedIn's 3 main API categories

  

该功能现已过时。使用GaussianBlur(),blur(),medianBlur()或bilateralFilter()。

<强>更新

执行>>> exampleobject=0 >>> def exampleobject(): ... print('This is a function.') >>> class exampleobject: ... def exampleobject(): ... print('Hello World!') >>> type(exampleobject) <class 'type'> 时,您只是创建一个新标头,而不是数据副本。所以不会耗尽你的记忆力。让我用一个小例子来说明:

IplImage ipl_img = mat_img;

#include <opencv2/opencv.hpp> using namespace cv; int main() { // Your (green) mat Mat mat_img(10, 10, CV_8UC3, Scalar(0,255,0)); // To IplImage IplImage ipl_img = mat_img; mat_img都是等于。没有复制。

cvSmooth

ipl_img

两张图片都改为:

enter image description here

    // It's created only a new header, on the same data

    // In fact, changing ipl_img changes also mat_img
    cvSet2D(&ipl_img, 1, 2, Scalar(255,0,0)); 

即使在平滑之后它们也是平等的:

enter image description here

<强>结论

因此,要回答您的原始问题:,您无需发布 // Same here, changing ipl_img changes also mat_img cvSmooth(&ipl_img, &ipl_img); return 0; } ,因为不涉及任何副本。您只是创建新标头,但指向相同的数据。你的内存泄漏在其他地方。

您甚至不需要使用IplImage,因为cvarrToMat的更改与IplImage中的更改相同。

答案 1 :(得分:0)

来自Opencv文档的

MATCH (n:Word)<-[:CONNECT]-(i:Item) 
WHERE n.name IN {words}
WITH i, count(*) as c
WHERE c = size({words})
RETURN i

在您的情况下,您未设置'copyData',因此默认情况下为false:

>     When copyData=false , the conversion is done really fast (in O(1) time) and the newly created matrix header will have refcount=0 , which
> means that no reference counting is done for the matrix data. In this
> case, you have to preserve the data until the new header is
> destructed. Otherwise, when copyData=true , the new buffer is
> allocated and managed as if you created a new matrix from scratch and
> copied the data there. That is, cvarrToMat(arr, true) is equivalent to
> cvarrToMat(arr, false).clone() (assuming that COI is not set). The
> function provides a uniform way of supporting CvArr paradigm in the
> code that is migrated to use new-style data structures internally. The
> reverse transformation, from Mat to CvMat or IplImage can be done by a
> simple assignment:

所以,你必须自己发布它。

编辑: 我不熟悉旧的OpenCV风格。当超出范围时,lplimage是否自由自由?如果这样做不会发生泄漏