被赋值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;
参考:点击打开链接