[edit: original code was wrong, see comments below]
Mat a(480, 640, CV_8U, Scalar::all(0));
Mat b(a.size(), a.type(), Scalar::all(0));
circle( a, Point(500,400), 40, Scalar(255), -1); // filled circle
rectangle( a, Point(1,1), Point(a.size()), Scalar(255), 2); // border
int dx=-300, dy=-300;
// calculate the new Rects:
Rect moved_dst(dx, dy, a.cols, a.rows);
Rect moved_src(-dx, -dy, a.cols, a.rows);
// clip against original bounds:
Rect bounds(0, 0, a.cols, a.rows);
Rect clipped_src = moved_src & bounds;
Rect clipped_dst = moved_dst & bounds;
a(clipped_src).copyTo(b(clipped_dst)); // copy rois
cerr << clipped_src << endl;
cerr << clipped_dst << endl;
Mat show;
show.push_back(a);
show.push_back(b);
yes exactly I don't need them