Mat的引用计数
作者:
小小蒟蒻
,
2021-11-09 03:50:46
,
所有人可见
,
阅读 236
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat a = (Mat_<double>(2, 3) << 1, 2, 3, 4, 5, 6);
cout << "a ref count = " << a.u->refcount << endl; // 1
Mat b = a; // b只构建Mat头部, 与a共享数据, 引用计数加1, 引用计数结果为2
cout << "a ref count = " << a.u->refcount << endl; // 2
cout << "b ref count = " << b.u->refcount << endl; // 2
cout << endl;
Mat c = b; // c只构建Mat头部, 与a共享数据, 引用计数加1, 引用计数结果为3
cout << "a ref count = " << a.u->refcount << endl; // 3
cout << "b ref count = " << b.u->refcount << endl; // 3
cout << "c ref count = " << c.u->refcount << endl; // 3
cout << endl;
c = b.clone(); // c从b克隆一份数据, 不再与a, b共享数据, 引用计数减1, 引用计数结果为2
cout << "a ref count = " << a.u->refcount << endl; // 2
cout << "b ref count = " << b.u->refcount << endl; // 2
cout << "c ref count = " << c.u->refcount << endl; // 1
cout << endl;
Mat d;
c.copyTo(d); // c将数据拷贝到d
cout << "a ref count = " << a.u->refcount << endl; // 2
cout << "b ref count = " << b.u->refcount << endl; // 2
cout << "c ref count = " << c.u->refcount << endl; // 1
cout << "d ref count = " << d.u->refcount << endl; // 1
cout << endl;
return 0;
}