Ask Your Question

Translate Mat by x and y offset

asked 2015-05-12 09:53:33 -0500

updated 2015-05-12 09:57:46 -0500

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 flag offensive close merge delete


yes exactly I don't need them

marios.b gravatar imagemarios.b ( 2015-05-12 10:04:09 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-05-12 10:12:33 -0500

berak gravatar image

updated 2015-05-13 05:24:22 -0500

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

image description

edit flag offensive delete link more



nice @berak ;-)

theodore gravatar imagetheodore ( 2015-05-12 16:57:49 -0500 )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

marios.b gravatar imagemarios.b ( 2015-05-13 04:44:52 -0500 )edit

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

berak gravatar imageberak ( 2015-05-13 04:48:53 -0500 )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

marios.b gravatar imagemarios.b ( 2015-05-13 04:53:52 -0500 )edit

sorry, i don't get it.

berak gravatar imageberak ( 2015-05-13 04:55:43 -0500 )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/?

marios.b gravatar imagemarios.b ( 2015-05-13 04:59:29 -0500 )edit

Never mind I will find it

marios.b gravatar imagemarios.b ( 2015-05-13 05:06:19 -0500 )edit

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

berak gravatar imageberak ( 2015-05-13 05:25:09 -0500 )edit

Perfect that was correct!

marios.b gravatar imagemarios.b ( 2015-05-13 08:30:09 -0500 )edit

needed a second shot, though ;(

berak gravatar imageberak ( 2015-05-13 08:34:40 -0500 )edit

Question Tools

1 follower


Asked: 2015-05-12 09:53:33 -0500

Seen: 1,608 times

Last updated: May 13 '15