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);
}
相关推荐
西猫雷婶1 小时前
pytorch基本运算-Python控制流梯度运算
人工智能·pytorch·python·深度学习·神经网络·机器学习
说私域1 小时前
新零售第一阶段传统零售商的困境突破与二次增长路径:基于定制开发开源AI智能名片S2B2C商城小程序的实践探索
人工智能·开源·零售
寒月霜华2 小时前
机器学习-模型验证
人工智能·深度学习·机器学习
救救孩子把2 小时前
3-机器学习与大模型开发数学教程-第0章 预备知识-0-3 函数初步(多项式、指数、对数、三角函数、反函数)
人工智能·数学·机器学习
CareyWYR2 小时前
每周AI论文速递(250908-250912)
人工智能
张晓~183399481212 小时前
短视频矩阵源码-视频剪辑+AI智能体开发接入技术分享
c语言·c++·人工智能·矩阵·c#·php·音视频
deephub3 小时前
量子机器学习入门:三种数据编码方法对比与应用
人工智能·机器学习·量子计算·数据编码·量子机器学习
AI 嗯啦3 小时前
计算机视觉----opencv实战----指纹识别的案例
人工智能·opencv·计算机视觉
max5006003 小时前
基于多元线性回归、随机森林与神经网络的农作物元素含量预测及SHAP贡献量分析
人工智能·python·深度学习·神经网络·随机森林·线性回归·transformer
trsoliu3 小时前
前端基于 TypeScript 使用 Mastra 来开发一个 AI 应用 / AI 代理(Agent)
前端·人工智能