opencv中图像旋转—getRotationMatrix2D和warpAffine

API介绍

getRotationMatrix2D:生成旋转矩阵

cpp 复制代码
Mat getRotationMatrix2D(Point2f center, double angle, double scale);

center:旋转的中心点坐标。

angle:顺时针旋转的角度。

scale:图像缩放比例。

warpAffine:仿射变换

cpp 复制代码
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

cpp 复制代码
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);
}

示例2

cpp 复制代码
void QuickDemo::rotato_demo(Mat &image) {
	Mat dst, M;
	int  w = image.cols;
	int h = image.rows;
	M = getRotationMatrix2D(Point2f(w / 2, h / 2), 100, 1.0);//旋转矩阵,围绕图像中心旋转45°
	warpAffine(image, dst, M, image.size(),INTER_LINEAR,0,Scalar(0,0,255));
	
	imshow("旋转100度", dst);
	imwrite("C:/Users/Desktop/opencv-0/warpAffine45.png",dst);
}

怎么计算新图像的宽度高度

计算经过仿射变换或旋转后新图像的宽度和高度,尤其是在旋转图像时保持图像的完整性而不裁剪任何部分,需要一些几何计算。
假设原图像的宽度为W,高度为H,旋转角度为theta,旋转后图像的新宽度W'和新高度H'。
1、将角度转换为弧度 **:**

因为大多数数学函数使用弧度制,

cpp 复制代码
theta_r=pi*theta/180;

2、计算四个角点旋转后的位置:

先确定原图像的四个角点的坐标,然后根据旋转公式计算旋转后每个点的新坐标。旋转公式为:
其中,(x, y)是原始坐标,(x', y')是旋转后的坐标。
3、计算新宽度和高度:

旋转后四个角点的最大和最小x、y坐标值之间的差分别给出了新图像的宽度W'和高度H'。

示例3

cpp 复制代码
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°
	double sin = abs(M.at<double>(0, 1));
	double cos = abs(M.at<double>(0, 0));
	int nw = cos*w + sin*h;//新宽度
	int nh = sin*w + cos*h;//新高度
	M.at<double>(0, 2) = M.at<double>(0, 2) + (nw / 2 - w / 2);
	M.at<double>(1, 2) = M.at<double>(1, 2) + (nh / 2 - h / 2);

	//warpAffine(image, dst, M, image.size(),INTER_LINEAR,0,Scalar(0,0,255));
	warpAffine(image, dst, M, Size(nw,nh), INTER_LINEAR, 0, Scalar(0, 0, 255));
	imshow("旋转45度", dst);
	imwrite("C:/Users/Desktop/opencv-0/warpAffine2.png",dst);
}

示例4

cpp 复制代码
void QuickDemo::rotato_demo(Mat &image) {
	Mat dst, M;
	int  w = image.cols;
	int h = image.rows;
	M = getRotationMatrix2D(Point2f(w / 2, h / 2), 100, 1.0);//旋转矩阵,围绕图像中心旋转100°
	double sin = abs(M.at<double>(0, 1));
	double cos = abs(M.at<double>(0, 0));
	int nw = cos*w + sin*h;//新宽度
	int nh = sin*w + cos*h;//新高度
	M.at<double>(0, 2) = M.at<double>(0, 2) + (nw / 2 - w / 2);
	M.at<double>(1, 2) = M.at<double>(1, 2) + (nh / 2 - h / 2);

	//warpAffine(image, dst, M, image.size(),INTER_LINEAR,0,Scalar(0,0,255));
	warpAffine(image, dst, M, Size(nw,nh), INTER_LINEAR, 0, Scalar(0, 0, 255));
	imshow("旋转100度", dst);
	imwrite("C:/Users/Desktop/opencv-0/warpAffine2.png",dst);
}
相关推荐
今天也想MK代码3 分钟前
基于WebRTC的实时语音对话系统:从语音识别到AI回复
人工智能·webrtc·语音识别
Vizio<30 分钟前
基于CNN的猫狗识别(自定义CNN模型)
人工智能·笔记·深度学习·神经网络·cnn
kovlistudio39 分钟前
机器学习第十三讲:独热编码 → 把“红黄蓝“颜色变成001/010/100的数字格式
人工智能·机器学习
豆豆43 分钟前
机器学习 day03
人工智能·机器学习
qyresearch_1 小时前
砷化镓太阳能电池:开启多元领域能源新篇
人工智能
山海不说话1 小时前
深度学习(第3章——亚像素卷积和可形变卷积)
图像处理·人工智能·pytorch·深度学习·目标检测·计算机视觉·超分辨率重建
2201_754918411 小时前
深入理解 OpenCV 的 DNN 模块:从基础到实践
人工智能·opencv·dnn
-一杯为品-2 小时前
【深度学习】#12 计算机视觉
人工智能·深度学习·计算机视觉
蹦蹦跳跳真可爱5892 小时前
Python----神经网络(《Searching for MobileNetV3》论文概括和MobileNetV3网络)
人工智能·python·深度学习·神经网络
妄想成为master2 小时前
如何完美安装GPU版本的torch、torchvision----解决torch安装慢 无法安装 需要翻墙安装 安装的是GPU版本但无法使用的GPU的错误
人工智能·pytorch·python·环境配置