Ask Your Question
0

Mat rotation center

asked 2016-09-14 05:04:46 -0600

tischenkoalex gravatar image

Hi, I'm rotating the 10x1 image by specifying center at 5,0:

[255, 255, 255, 255, 255, 255, 255, 255, 255, 255]

int size = 10;
Mat test = Mat::zeros(Size(size, 1), CV_8U);
line(test, Point(0, 0), Point(size, 0), Scalar(255));

Mat rot = getRotationMatrix2D(Point(test.cols/2, test.rows/2), 90, 1);
Mat dest;
warpAffine(test, dest, rot, Size(size, size));

imshow("Source", test);
imshow("Result", dest);

I assume that result will be the vertical line at center of 10x10 Mat, but it is shifted and cropped to the top:

[  0,   0,   0,   0,   0, 255,   0,   0,   0,   0;
   0,   0,   0,   0,   0, 255,   0,   0,   0,   0;
   0,   0,   0,   0,   0, 255,   0,   0,   0,   0;
   0,   0,   0,   0,   0, 255,   0,   0,   0,   0;
   0,   0,   0,   0,   0, 255,   0,   0,   0,   0;
   0,   0,   0,   0,   0, 255,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0]

I'm trying to understand why it happens?

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
2

answered 2016-09-14 09:03:11 -0600

kbarni gravatar image

This is normal.

Put a pen on the top border of a sheet of paper. Hold the pen at the middle and turn it 90°. Now half of the pen will be outside the paper, the other half inside. Just like the line in the resulting matrix you posted.

To get a vertical line, you have to translate the line. Think of the pen, how can you move it to be at the middle of the paper vertically. Fortunately it's simple to add translation to the rotation matrix, just modify the elements at [0,2] for the X translation and [1,2] for the Y translation.

edit flag offensive delete link more

Comments

Thanks! I expected that rotation will be centered in resulting mat, but now I see it couldn't without explicitly specified offset:

Mat rot = getRotationMatrix2D(center, 90, 1);
rot.at<double>(0, 2) += 0; // x
rot.at<double>(1, 2) += size/2; // y
tischenkoalex gravatar imagetischenkoalex ( 2016-09-14 14:07:20 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-09-14 05:04:46 -0600

Seen: 460 times

Last updated: Sep 14 '16