In the following code, the value of t1 is correct before running the cv::Mat t2 = t.clone();
, but after running cv::Mat t2 = t.clone();
, the value of t1 is modified to be the same as t, and the address of t2 becomes the same as t1.
This problem will not occur after changing rr = cv::Mat(rr.rows, 1, CV_64FC3, rr.data);
to rr = rr.reshape(3, rr.rows);
.
I looked up the reshape function, and it just modifies the data layout.
Why does reshape not have this error? Did my operation cause opencv to release the memory of rr by mistake, causing t2 to overwrite the data of t1?
cv::Mat test(cv::Mat t)
{
cv::Mat tt(3, 3, CV_64F, t.data);
cv::Mat rr = tt + 10;
rr = cv::Mat(rr.rows, 1, CV_64FC3, rr.data);
//rr = rr.reshape(3, rr.rows);
return rr;
}
int main()
{
cv::Mat t = cv::Mat::zeros(3, 1, CV_64FC3);
cv::Mat t1 = test(t);
std::cout << "t1 before t clone:" << t1 << std::endl;
cv::Mat t2 = t.clone();
std::cout << "t1 after t clone:" << t1 << std::endl;
return 1;
}
output: