Opencv实现图像旋转,非常简单,几行代码
opencv实现图片旋转功能,非常简单,几行代码即可:
#include<iostream>
#include<time.h>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
inline double cpu_time(){
return clock()*1000/CLOCKS_PER_SEC;
}
//图片旋转操作
void imrotate(Mat& img, Mat& newIm, double angle){
int len = max(img.cols, img.rows);
Point2f pt(len/2.,len/2.);
Mat r = getRotationMatrix2D(pt,angle,1.0);
warpAffine(img,newIm,r,Size(len,len));
//better performance :
//Point2f pt(img.cols/2.,img.rows/2.);
//Mat r = getRotationMatrix2D(pt,angle,1.0);
//warpAffine(img,newIm,r,img.size());
}
int main(){
Mat img = imread("1.jpg");
//resize(img,img,Size(256,256));
Mat newIm;
double t1,t2;
t1 = cpu_time();
//调用旋转方法,模仿matlab,取名为imrotate
imrotate(img,newIm,15);
t2 = cpu_time();
cout<<"Rotate one image cost: "<<t2-t1<<" ms"<<endl;
namedWindow("Rotate");
imshow("Rotate",newIm);
waitKey(10000);
const string save_name = "rotate.jpg";
imwrite(save_name,newIm);
return 0;
}
run in ubuntu 12.04