The behaviour of copying cv::Mat
is confusing me.
I understand from the documentation that Mat::copyTo()
is deep copy while the assignment operator is not. My questions:
what should I do to return a
cv::Mat
from a function, such as:cv::Mat func()
?According to the documentation, if I return a
cv::Mat
it'll have no use, because after the function returns the local copy of thecv::Mat
in that function will be destroyed and therefore the one accepting the returned value outside the function should be pointing to some random address. The weird thing is that (most of times) it works correctly. For example, the following works:cv::Mat CopyOneImage(const cv::Mat& orgImage) { cv::Mat image; orgImage.copyTo(image); return image; } int main() { std::string orgImgName("a.jpg"); cv::Mat orgImage; orgImage = cv::imread(orgImgName); cv::Mat aCopy; aCopy = CopyOneImage(orgImage); return 1; }
But why? It's not a deep copy.
Question 3. And also sometimes the assignment operator seems to be deep copy, too:
int main()
{
std::string orgImgName("a.jpg");
cv::Mat orgImage;
orgImage = cv::imread(orgImgName);
cv::Mat aCopy;
orgImage.copyTo(aCopy);
cv::Mat copyCopy1;
copyCopy1 = aCopy;
cv::namedWindow("smallTest", 1);
cv::imshow("smallTest", copyCopy1);
uchar key = (uchar)cv::waitKey();
cv::Mat orgImage2 = cv::imread("b.jpg");
orgImage2.copyTo(aCopy);
cv::imshow("smallTest", copyCopy1);
return 1;
}
Then the two displays shows the same image, a.jpg. Why? And some other times it doesn't work. (The original code is too long but it can be also simplified to the above case). In those times the assignment operator seem to be actually 'shallow' copying. Why?
Thanks a lot!