Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

ah, don't loop. totally not the opencv way.

Mat a(10,10,CV_8U,Scalar(3));

int dx=-3, dy=2; // new Rect: Rect moved(dx, dy, a.cols, a.rows);

// clip against original bounds: Rect bounds(0, 0, a.cols, a.rows); moved = moved & bounds;

Mat b(a.size(), a.type(), Scalar(0));

a(moved).copyTo(b(moved));

cerr << moved << endl; cerr << a << endl; cerr << b << endl;

[7 x 8 from (0, 2)] [ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3; 3, 3, 3, 3, 3, 3, 3, 3, 3, 3; 3, 3, 3, 3, 3, 3, 3, 3, 3, 3; 3, 3, 3, 3, 3, 3, 3, 3, 3, 3; 3, 3, 3, 3, 3, 3, 3, 3, 3, 3; 3, 3, 3, 3, 3, 3, 3, 3, 3, 3; 3, 3, 3, 3, 3, 3, 3, 3, 3, 3; 3, 3, 3, 3, 3, 3, 3, 3, 3, 3; 3, 3, 3, 3, 3, 3, 3, 3, 3, 3; 3, 3, 3, 3, 3, 3, 3, 3, 3, 3] [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0; 0, 0, 0, 0, 0, 0, 0, 0, 0, 0; 3, 3, 3, 3, 3, 3, 3, 0, 0, 0; 3, 3, 3, 3, 3, 3, 3, 0, 0, 0; 3, 3, 3, 3, 3, 3, 3, 0, 0, 0; 3, 3, 3, 3, 3, 3, 3, 0, 0, 0; 3, 3, 3, 3, 3, 3, 3, 0, 0, 0; 3, 3, 3, 3, 3, 3, 3, 0, 0, 0; 3, 3, 3, 3, 3, 3, 3, 0, 0, 0; 3, 3, 3, 3, 3, 3, 3, 0, 0, 0]

ah, don't loop. totally not not the opencv way.way ;).

Mat a(10,10,CV_8U,Scalar(3));

a(10, 10, CV_8U, Scalar(3)); // src image, filled with 3 for demo here Mat b(a.size(), a.type(), Scalar(0)); // dst image (pre-filled with zero) int dx=-3, dy=2; dy=2; // 3 to the left, 2 down // calculate the new Rect: Rect moved(dx, dy, a.cols, a.rows);

a.rows); // clip against original bounds: Rect bounds(0, 0, a.cols, a.rows); moved = moved & bounds;

Mat b(a.size(), a.type(), Scalar(0));

a(moved).copyTo(b(moved));

bounds; // <-- here's the trick. a(moved).copyTo(b(moved)); // copy rois cerr << moved << endl; cerr << a << endl; cerr << b << endl;

endl;


[7 x 8 from (0, 2)]
 [  3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3]
 [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0]

0]

ah, don't loop. totally not the opencv way ;).

Mat a(10, 10, CV_8U, Scalar(3));      Scalar::all(3)); // src image, filled with 3 for demo here
Mat b(a.size(), a.type(), Scalar(0)); // dst image (pre-filled with zero)

int dx=-3, dy=2;                      // 3 to the left, 2 down
// calculate the new Rect:
Rect moved(dx, dy, a.cols, a.rows);

// clip against original bounds:
Rect bounds(0, 0, a.cols, a.rows);
moved = moved & bounds;               // <-- here's the trick.

a(moved).copyTo(b(moved));            // copy rois 

cerr << moved << endl;
cerr << a << endl;
cerr << b << endl;

[7 x 8 from (0, 2)]

[  3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3]

[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0]

ah, don't loop. totally not the opencv way ;).

Mat a(10, 10, CV_8U, Scalar::all(3));  // src image, filled with 3 for demo here
Mat b(a.size(), a.type(), Scalar(0)); Scalar::all(0)); // dst image (pre-filled with zero)

int dx=-3, dy=2;                       // 3 to the left, 2 down
// calculate the new Rect:
Rect moved(dx, dy, a.cols, a.rows);

// clip against original bounds:
Rect bounds(0, 0, a.cols, a.rows);
moved = moved & bounds;                // <-- here's the trick.

a(moved).copyTo(b(moved));             // copy rois 

cerr << moved << endl;
cerr << a << endl;
cerr << b << endl;

[7 x 8 from (0, 2)]

[  3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3]

[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0]

ah, don't loop. totally not the opencv way ;).[edit: original code was wrong, see comments below]

Mat a(10, 10, a(480, 640, CV_8U, Scalar::all(3)); Scalar::all(0));  // src image, filled with 3 for demo here
 Mat b(a.size(), a.type(), Scalar::all(0)); 

circle( a, Point(500,400), 40, Scalar(255), -1); // dst image (pre-filled with zero)
filled circle
rectangle( a, Point(1,1), Point(a.size()), Scalar(255), 2);  // border


int dx=-3, dy=2;                           // 3 to the left, 2 down
dx=-300, dy=-300; 
// calculate the new Rect:
Rects:
Rect moved(dx, 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);
moved Rect clipped_src = moved moved_src & bounds;       
Rect clipped_dst = moved_dst & bounds;        // <-- here's the trick.
 a(moved).copyTo(b(moved)); 
a(clipped_src).copyTo(b(clipped_dst));                // copy rois 

cerr << moved clipped_src << endl;
cerr << a clipped_dst << endl;
cerr << b << endl;

Mat show;
show.push_back(a);
show.push_back(b);

image description

[7 x 8 from (0, 2)]

[  3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3]

[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0;
   3,   3,   3,   3,   3,   3,   3,   0,   0,   0]