API介绍
getRotationMatrix2D:生成旋转矩阵
Mat getRotationMatrix2D(Point2f center, double angle, double scale);
center:旋转的中心点坐标。
angle:顺时针旋转的角度。
scale:图像缩放比例。
warpAffine:仿射变换
void warpAffine(InputArray src, OutputArray dst, InputArray M, Size dsize, int flags = INTER_LINEAR, int borderMode = BORDER_CONSTANT, const Scalar& borderValue = Scalar());
src:原始图像。
dst:输出图像。
M:变换矩阵,这里是由getRotationMatrix2D生成的旋转矩阵。
dsize:输出图像的大小。
flags:插值方法,通常使用INTER_LINEAR。
borderMode:边界像素模式。
borderValue:边界填充值,用于边界外的像素。
示例1
void QuickDemo::rotato_demo(Mat &image) {
Mat dst, M;
int w = image.cols;
int h = image.rows;
M = getRotationMatrix2D(Point2f(w / 2, h / 2), 45, 1.0);//旋转矩阵,围绕图像中心旋转45°
warpAffine(image, dst, M, image.size(),INTER_LINEAR,0,Scalar(0,0,255));
imshow("旋转45度", dst);
imwrite("C:/Users/Desktop/opencv-0/warpAffine45.png",dst);
}