# Translate Mat by x and y offset

Hi! I want to be able to translate a mat by a x and y offset and form a new Mat of the same size.Also I want the value of pixels that belong to offset to become 0. How can I do it? I tried it for every element of matrix but I failed.

edit retag close merge delete

yes exactly I don't need them

( 2015-05-12 10:04:09 -0600 )edit

Sort by ยป oldest newest most voted

[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);


more

1

nice @berak ;-)

( 2015-05-12 16:57:49 -0600 )edit

Hmm yes however if I want to make the image go y-up when I used - in fron of the dy value, I didn't get the result I wanted

( 2015-05-13 04:44:52 -0600 )edit

"I didn't get the result I wanted" - which is ?

( 2015-05-13 04:48:53 -0600 )edit

Ok first of all,lets say I have a hand blob, so a mat with only 1 or 0 as values and dimensions 640x480.I want to translate by x,y the whole image and then create a new mat which will also be 640x480 but now I can see the blob translated since the image also moved

( 2015-05-13 04:53:52 -0600 )edit

sorry, i don't get it.

( 2015-05-13 04:55:43 -0600 )edit

Ok I have a 640x480 image with a white circle on the bottom right.Now I want a new mat which will have this circle in the top left corner and every other value will be 0(black) and the final size will be the same.How can I do it/?

( 2015-05-13 04:59:29 -0600 )edit

Never mind I will find it

( 2015-05-13 05:06:19 -0600 )edit

^^ yea , sorry, my bad. the src rect has to move into the opposite direction as the dst rect.

( 2015-05-13 05:25:09 -0600 )edit

Perfect that was correct!

( 2015-05-13 08:30:09 -0600 )edit

needed a second shot, though ;(

( 2015-05-13 08:34:40 -0600 )edit

Official site

GitHub

Wiki

Documentation

## Stats

Seen: 2,146 times

Last updated: May 13 '15