#include <opencv2/opencv.hpp>
#include <iostream>
#define WINDOW_NAME "图像旋转"
using namespace std;
using namespace cv;
int g_nAngle = 0;
int g_nP = 80;
Mat srcImg,dstImg;
void valueChange(int,void *)
{
//设置旋转中心
Point center = Point(srcImg.cols/2,srcImg.rows/2);
//旋转角度
double angle = static_cast<double>(g_nAngle);
//图像缩放系数
double p = static_cast<double>(g_nP/100.0);
//获取旋转矩阵
Mat array = getRotationMatrix2D(center,angle,p);
//仿射变换
warpAffine(srcImg,dstImg,array,Size(srcImg.size()),1,0,Scalar(0,255,0));
imwrite("rotation.jpg",dstImg);
imshow(WINDOW_NAME,dstImg);
}
int main(int argc,char *argv[])
{
if(argc!=2)
{
cout<<"input like this:./test .jpg"<<endl;
return 0;
}
srcImg = imread(argv[1]);
if(srcImg.empty())
{
cout<<"srcImg is empty!"<<endl;
return -1;
}
namedWindow(WINDOW_NAME);
//绘制旋转角度和缩放系数的进度条
createTrackbar("angle",WINDOW_NAME,&g_nAngle,360,valueChange);
createTrackbar("np",WINDOW_NAME,&g_nP,100,valueChange);
valueChange(0,0);
waitKey(0);
return 0;
}
![](https://img-blog.csdn.net/20180228103649806)
![](https://img-blog.csdn.net/20180228103810583)