dreamlike-zzg

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

被赋值Mat之前已经被分配足够空间时,copyTo()不重新给分配空间,继续使用之前的空间,当已分配空间不足时,则会重新分配空间;clone()则均重新分配空间。

	Mat M(1, 3, CV_8UC1, Scalar(1));   
	cout << "M=" << endl << " " << M << endl << endl;

	Mat MM(1, 6, CV_8UC1, Scalar(2));
	cout << "MM=" << endl << " " << MM << endl << endl;

	Mat B = MM.colRange(0, 3);
	cout << "B=" << endl << " " << B << endl << endl;
 M.copyTo(B);//不重新给B分配空间,B与MM继续共享
 cout << "B=" << endl << " " << B << endl << endl;
 cout << "MM=" << endl << " " << MM << endl << endl;
	B = M.clone(); //重新给B分配空间
	cout << "B=" << endl << " " << B << endl << endl;
	cout << "MM=" << endl << " " << MM << endl << endl;

	Mat M(1, 3, CV_8UC1, Scalar(1));   
	cout << "M=" << endl << " " << M << endl << endl;

	Mat MM(3, 3, CV_8UC1, Scalar(2));
	cout << "MM=" << endl << " " << MM << endl << endl;

	Mat B = MM.colRange(0, 2);
	cout << "B=" << endl << " " << B << endl << endl;

	M.copyTo(B);//大小不兼容,重新分配
	cout << "B=" << endl << " " << B << endl << endl;
	cout << "MM=" << endl << " " << MM << endl << endl;

参考:点击打开链接

posted on 2022-11-05 16:00  梦幻济公  阅读(360)  评论(0编辑  收藏  举报