#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/imgproc/imgproc_c.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char ** argv)
{
Mat inputImg,tempImg;
inputImg = imread("cut.jpg"); //用opencv读取图像数据
imshow("inputImg",inputImg);
CV_Assert(!inputImg.empty());
float angle = 15;
float radian = (float) (angle /180.0 * CV_PI);
//填充图像使其符合旋转要求
int uniSize =(int) ( max(inputImg.cols, inputImg.rows)* 1.414 );
int dx = (int) (uniSize - inputImg.cols)/2;
int dy = (int) (uniSize - inputImg.rows)/2;
copyMakeBorder(inputImg, tempImg, dy, dy, dx, dx, BORDER_CONSTANT);
//旋轉中心
Point2f center( (float)(tempImg.cols/2) , (float) (tempImg.rows/2));
Mat affine_matrix = getRotationMatrix2D( center, angle, 1.0 );
//旋轉
warpAffine(tempImg, tempImg, affine_matrix, tempImg.size());
//旋轉后的圖像大小
float sinVal = fabs(sin(radian));
float cosVal = fabs(cos(radian));
Size targetSize( (int)(inputImg.cols * cosVal + inputImg.rows * sinVal),
(int)(inputImg.cols * sinVal + inputImg.rows * cosVal) );
//剪掉四周边框
int x = (tempImg.cols - targetSize.width) / 2;
int y = (tempImg.rows - targetSize.height) / 2;
Rect rect(x, y, targetSize.width, targetSize.height);
tempImg = Mat(tempImg, rect);
imshow("Show",tempImg);
cvWaitKey();
return 0;
}
opencv图像旋转
最新推荐文章于 2024-05-06 13:24:45 发布