Reputation: 79
When I copy a Mat object, the values in other Mat object changes unexpectedly. The change occurs between the two print statement. Why is this?
Mat flow;
calcOpticalFlowFarneback(u_prev, u_curr, u_flow, 0.5, 2, 20, 3, 15, 1.2, OPTFLOW_FARNEBACK_GAUSSIAN);
flow = u_flow.getMat(ACCESS_READ);
cout << "1 " << flow.ptr<Pixel2>(680,192)->x << endl;
Mat out_img;
resized_frame.copyTo (out_img);
cout << "2 " << flow.ptr<Pixel2>(680,192)->x << endl;
Output as following
1 164.812
2 8.42217e-21
Upvotes: 0
Views: 76
Reputation: 383
The parameters for Mat::ptr
are Mat::ptr(int row, int col)
as mentioned in the docs. Since flow
is a 480x852 matrix, flow.ptr<Pixel2>(680, 192)
accesses an out-of-bounds row and reads memory outside the matrix. Most likely that memory location is assigned to a different variable in your program (which might be out_img
in this case) which gets changed over time. It might even segfault on some platforms depending on the memory layout used by the compiler.
Upvotes: 2