mat 释放_cv :: Mat从外部原始指针数据,释放自身

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值