My task is to periodically update a cv::Mat m
of r
rows and c
cols, in this way:
- shift by
1
column the wholem
to the right, dropping away the last column at positionc-1
- randomly generate the new column at position
0
- refresh the plot of
m
This will result in a sort of belt conveyor simulation, for the sake of clarity. However, the problem arises in point 1 when m
has to be shifted.
I found two different solutions, namely A and B, both ending in the same result. This suggests me that I'm doing something wrong.
Method A follows:
int offset = 1;
cv::Mat tmp = cv::Mat::zeros(m.size(), m.type());
cv::Rect rect_src(0, 0, m.cols-offset, m.rows);
cv::Rect rect_dst(offset, 0, m.cols-offset, m.rows);
cv::Mat in = m(rect_src);
cv::Mat out = tmp(rect_dst);
in.copyTo(out);
m = temp;
Method B follows:
int offset = 1;
cv::Mat trans_mat = (cv::Mat_<double>(2, 3) << 1, 0, offset, 0, 1, 0);
cv::Mat warped;
warpAffine(m, warped, trans_mat, m.size());
m = warped;
And here's the output as an example small m
(random values are spawning on the left):
cycle 1
90 0 0 0 0 0 0 0 0
143 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
cycle 2
0 0 90 0 0 0 0 0 0
0 0 143 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
cycle 3
0 0 144 0 90 0 0 0 0
0 0 161 0 143 0 0 0 0
0 0 0 0 0 0 0 0 0
it is clear that an extra column made of zeros appears somehow... and I really cannot figure how.
P.S. If I set offset = 3
the output scales by a factor 2
, and so on.
90 0 0 0 0 0 0 0 0
143 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
cycle 2
0 0 0 0 0 0 90 0 0
0 0 0 0 0 0 143 0 0
0 0 0 0 0 0 0 0 0