问题描述
有从外部数据指针创建 cv :: Mat :: data
所有OpenCV函数都需要 cv :: Mat :: data 是一个void *,所以你不能改变。如果你继承自 cv :: Mat 并且编写你自己的析构函数,你会变得与OpenCV不兼容,因为他们不返回你的派生类型。我建议将 cv :: Mat 作为类的属性,并保留 std :: shared_ptr 或其他智能指针来管理底层 void * 的生命周期。
只需使用 std :: shared_ptr :: get()
的原始指针初始化OpenCV矩阵,并确保他们具有相同的生命周期,即在同一个类。
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从外部原始指针数据,释放自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!