这里有一个小测试程序,专门演示 cv::Mat 对象(即矩阵头)的数据共享属性!
int main()
{
// create input of size 512x512
cv::Mat input = cv::imread("../inputData/Lenna.png");
// create a second input of size 256x256
cv::Mat modifiedInput;
cv::resize(input, modifiedInput, cv::Size(256,256));
std::vector<cv::Mat> images;
// first element will be a "deep copy" where the matrix elements will be copied to a new memory location and a new header will be created, referecing those matrix elements.
images.push_back(input.clone());
// 6 times copy the "input" to "images".
// All the copies will (deep) copy the matrix header but they will share the matrix elements (because their memory LOCATION will be copied)
for(unsigned int i=0; i<6; ++i)
images.push_back(input);
// now some experiments:
// draw a circle to input variable. At this point it should share it's matrix elements with images[1-5]
cv::circle(input, cv::Point(100,100), 30, cv::Scalar(0,0,0), -1);
// draw a circle to a vector element:
cv::circle(images[5], cv::Point(300,100), 30, cv::Scalar(0,0,0), -1);
// use a openCV function that will allocate new memory, if the destination dimensions don't fit:
// to a mat whose dimensions fit:
// remember that input.size() == vector[0..5].size
// compute median blur and target one of the matrices that share their data at the moment:
cv::medianBlur(input, images[3], 11);
cv::imshow("0", images[0]);
cv::imshow("1", images[1]);
cv::imshow("2", images[2]);
cv::imshow("3", images[3]);
cv::imshow("4", images[4]);
cv::imshow("5", images[5]);
cv::waitKey(0);
此时它看起来像这样:除了第一个矩阵之外,所有矩阵都共享其元素的数据,因为使用.clone() 强制进行了深层复制。
现在继续:
// to a mat whose dimensions don't fit (new memory will be allocated, not shared by the other matrix headers anymore):
// images[3] will not share the data with other matrix headers afterwards
cv::medianBlur(modifiedInput, images[3], 11);
// now images[3] and images[4] will share matrix elements
images[4] = images[3];
cv::circle(images[4], cv::Point(128,128), 20, cv::Scalar(255,255,255), 3);
// create a deep-copy of 256x256 input to overwrite images[5] (not modifying any other image's matrix elements)
images[5] = modifiedInput.clone();
cv::circle(images[5], cv::Point(0,0), 30, cv::Scalar(0,255,0), -1);
cv::imshow("0", images[0]);
cv::imshow("1", images[1]);
cv::imshow("2", images[2]);
cv::imshow("3", images[3]);
cv::imshow("4", images[4]);
cv::imshow("5", images[5]);
//cv::imshow("input", input);
//cv::imwrite("../outputData/MainBase.png", input);
cv::waitKey(0);
return 0;
}
看起来像这样:
这一次,medianBlur 的调用没有与所有其他矩阵共享数据,因为目标图像的尺寸不适合,因此必须在 mediumBlur 方法中为images[3] 分配新内存。所以 images[3] 引用了不同的数据元素!
所有这一切可能有点棘手,因为用户可能无法直接看到,哪些函数调用会分配新数据,哪些不会,所以如果你想确保分配新数据,你应该在开始时这样做对于每个垫子,或者使用一个空垫子作为目的地(或者在开始时不共享任何数据)。
还有一件事:
cv::Mat emptyMat;
std::vector<cv::Mat> images(n, emptyMat); // insert n copies of emptyMat header
// or
for(unsigned int i=0; i<n; ++i)
images.push_back(emptyMat) // same result
这既是save to use,所以不共享数据,因为所有emptyMat一开始都没有数据,所以不能共享数据。每当将任何数据分配给任何向量元素时,其他人都不知道它,因此他们不会共享该数据。
// BUT:
cv::Mat notEmptyMat = cv::Mat::zeros(height, width, type);
std::vector<cv::Mat> images(n, notEmptyMat ); // insert n copies of emptyMat header which references the assigned zeroes data of size width x height
// or
for(unsigned int i=0; i<n; ++i)
images.push_back(notEmptyMat ) // same result
在这里,数据是共享的,每当您更改其中一个矩阵的 DATA 时,其他矩阵也会更改。但显然,如果您将新的数据内存分配给其中一个矩阵,其他矩阵仍会引用它们的其他数据内存。