在OpenCV中,目前并没有现成的函数直接用来实现图像旋转,它是用仿射变换函数cv::warpAffine来实现的,此函数目前支持4种插值算法,最近邻、双线性、双三次、兰索斯插值,如果传进去的参数为基于像素区域关系插值算法(INTER_AREA),则按双线性插值。
通常使用2*3矩阵来表示仿射变换
利用getRotationMatrix2D(Point center,angle,scale)来创建一个2x3的矩阵
参数列表:
center:要围绕的旋转中心
angle:旋转的角度
scale:旋转后缩放大小
getRotationMatrix2D函数实现公式为:看着有点懵
1 Mat dst, M; 2 //resize(image,image,Size(500,500));//可以设置你想要的图像大小 3 //取图像的宽高 4 int w = image.cols; 5 int h = image.rows; 6 7 M = getRotationMatrix2D(Point2f(w / 2, h / 2), 45, 1.0);//创建一个2x3的矩阵 8 /* 9 三个参数,旋转中心,旋转角度,旋转后图像的缩放比例 10 */ 11 double cos = abs(M.at<double>(0, 0)); 12 double sin = abs(M.at<double>(0, 1)); 13 int nw = w * cos + h * sin; 14 int nh = h * cos + w * sin; 15 16 //更新矩阵——因为图像旋转的话该图像实际所占用的(要想把旋转后的完整图像显示出来的话)空间变大 17 M.at<double>(0, 2) += (nw - w) / 2; 18 M.at<double>(1, 2) += (nh - h) / 2; 19 //仿射变换 20 warpAffine(image, dst, M, Size(nw, nh), 1, 0, Scalar(255, 0, 255)); 21 /* 22 23 */ 24 imshow("旋转演示", dst);
原文:https://www.cnblogs.com/Alan-Wangyoubiao/p/14586273.html