OpenCV 图像旋转

 1 RotateArbitrarilyAngle(Mat src, Mat &dst, float angle)
 2 {
 3     float radian = (float)(angle / 180.0 * CV_PI);
 4 
 5     //填充图像
 6     int maxBorder = (int)(max(src.cols, src.rows)* 1.414); //即为sqrt(2)*max
 7     int dx = (maxBorder - src.cols) / 2;
 8     int dy = (maxBorder - src.rows) / 2;
 9     copyMakeBorder(src, dst, dy, dy, dx, dx, BORDER_CONSTANT);
10 
11     //旋转
12     Point2f center((float)(dst.cols / 2), (float)(dst.rows / 2));
13     Mat affine_matrix = getRotationMatrix2D(center, angle, 1.0);//求得旋转矩阵
14     warpAffine(dst, dst, affine_matrix, dst.size());
15 
16     //计算图像旋转之后包含图像的最大的矩形
17     float sinVal = abs(sin(radian));
18     float cosVal = abs(cos(radian));
19     Size targetSize((int)(src.cols * cosVal + src.rows * sinVal),
20         (int)(src.cols * sinVal + src.rows * cosVal));
21 
22     //剪掉多余边框
23     int x = (dst.cols - targetSize.width) / 2;
24     int y = (dst.rows - targetSize.height) / 2;
25     Rect rect(x, y, targetSize.width, targetSize.height);
26     dst = Mat(dst, rect);
27 }
原文地址:https://www.cnblogs.com/hsy1941/p/11563253.html