cv :: Mat从外部原始指针数据,释放自身 [英] cv::Mat from external raw pointer data, releasing itself

查看:1490
本文介绍了cv :: Mat从外部原始指针数据,释放自身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有从外部数据指针创建 cv :: Mat 的方法,但使该对象负责删除数据?



即,我有一个函数从 void *创建一个 cv :: Mat 指针,

  cv :: Mat createMat(){

* data =(...);

cv :: Mat data_m(rows,cols,CV_8UC1,data);
return data_m;
}

我希望我的返回 cv :: Mat 负责发布数据。 cv :: Mat :: data

所有OpenCV函数都需要 cv :: Mat :: data 是一个void *,所以你不能改变。如果你继承自 cv :: Mat 并且编写你自己的析构函数,你会变得与OpenCV不兼容,因为他们不返回你的派生类型。
我建议将 cv :: Mat 作为类的属性,并保留 std :: shared_ptr 或其他智能指针来管理底层 void * 的生命周期。
只需使用 std :: shared_ptr :: get()
的原始指针初始化OpenCV矩阵 http://www.cplusplus.com/reference/memory/shared_ptr/get/ ,并确保他们具有相同的生命周期,即在同一个类。


is there a way of creating a cv::Mat from external data pointer, but making that object responsible for deleting the data?

i.e., I have a function creating a cv::Mat from a void * pointer,

cv::Mat createMat() {

  void *data = (...);

  cv::Mat data_m(rows, cols, CV_8UC1, data);
  return data_m;
}

and I want that my returned cv::Mat to be responsible for releasing the data. How can I do this?

解决方案

All OpenCV functions expect cv::Mat::data to be a void*, so you cannot change that. If you inherit from cv::Mat and write your own destructor, you become incompatible from OpenCV because they do not return your derived type. I would suggest to contain your cv::Mat as an attribute of a class, and keep a std::shared_ptr or other smart pointer to manage the lifetime of the underlying void*. Just initialize the OpenCV matrix with the raw pointer from the shared pointer with std::shared_ptr::get() http://www.cplusplus.com/reference/memory/shared_ptr/get/ and make sure they have the same lifetime, i.e. in the same class.

这篇关于cv :: Mat从外部原始指针数据,释放自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆