图像的旋转
#include<iostream>
#include<opencv2/opencv.hpp>
#include<cmath>
using namespace cv;
using namespace std;
Mat anglerotate(Mat& image1, float angle);
int main()
{
Mat image;
image = imread("C:/Users/MECHREVO/Desktop/11.jpeg");//读入数据
if (image.empty())
{
cout << "未能成功读取文件" << endl;
return 0;
}
Mat result = anglerotate(image,60);
namedWindow("resource", CV_WINDOW_NORMAL);
namedWindow("result", CV_WINDOW_NORMAL);
imshow("resource", image);
imshow("result", result);
waitKey(0);
return 0;
}
Mat anglerotate(Mat& image1, float angle)
{
float alpha = angle * CV_PI / 180;
//构造旋转矩阵
float rotatemat[3][3] = { {cos(alpha),-sin(alpha),0},{sin(alpha),cos(alpha),0},{0,0,1} };
int rows = image1.rows;
int cols = image1.cols;
//计算旋转后图像各个顶点坐标
float a1 = cols * rotatemat[0][0];
float b1 = cols * rotatemat[1][0];
float a2 = cols * rotatemat[0][0] + rows * rotatemat[0][1];
float b2= cols * rotatemat[1][0]+ rows * rotatemat[1][1];
float a3 = rows * rotatemat[0][1];
float b3 = rows * rotatemat[1][1];
//计算极值点
float kxmin = min(min(min(0.0f, a1), a2), a3);
float kxmax = max(max(max(0.0f, a1), a2), a3);
float kymin = min(min(min(0.0f, b1), b2), b3);
float kymax = max(max(max(0.0f, b1), b2), b3);
//计算输出矩阵大小
int ouputcols = abs(kymax - kymin);
int outputrows = abs(kxmax - kxmin);
Mat dst(outputrows, ouputcols, image1.type());
for (int i = 0; i < outputrows; i++)
{
for (int j = 0; j < ouputcols; j++)
{
//旋转坐标变换
int x = (j + kxmin) * rotatemat[0][0] - (i + kymin) * rotatemat[0][1];
int y = -(j + kxmin) * rotatemat[1][0] + (i + kymin) * rotatemat[1][1];
//区域旋转
if ((x >= 0) && (x < cols) && (y >= 0) && (y < rows))
{
dst.at<Vec3b>(i, j) = image1.at<Vec3b>(y, x);
}
}
}
return dst;
}