http://blog.csdn.net/tulun/article/details/7161885#cpp
原文中会存在内存泄露,我稍稍改下
#include "cv.h"
#include "highgui.h"
#include "math.h"
// clockwise 为true则顺时针旋转,否则为逆时针旋转
IplImage* rotateImage(IplImage* src, int angle, bool clockwise)
{
angle = abs(angle) % 180;
if (angle > 90)
{
angle = 90 - (angle % 90);
}
IplImage* dst1 = NULL;
IplImage* dst2 = 0;
int width =
(double)(src->height * sin(angle * CV_PI / 180.0)) +
(double)(src->width * cos(angle * CV_PI / 180.0 )) + 1;
int height =
(double)(src->height * cos(angle * CV_PI / 180.0)) +
(double)(src->width * sin(angle * CV_PI / 180.0 )) + 1;
int tempLength = sqrt((double)src->width * src->width + src->height * src->height) + 10;
int tempX = (tempLength + 1) / 2 - src->width / 2;
int tempY = (tempLength + 1) / 2 - src->height / 2;
int flag = -1;
dst1 = cvCreateImage(cvSize(width, height), src->depth, src->nChannels);
cvZero(dst1);
IplImage* temp = cvCreateImage(cvSize(tempLength, tempLength), src->depth, src->nChannels);
cvZero(temp);
cvSetImageROI(temp, cvRect(tempX, tempY, src->width, src->height));
cvCopy(src, temp, NULL);
cvResetImageROI(temp);
if (clockwise)
flag = 1;
float m[6];
int w = temp->width;
int h = temp->height;
m[0] = (float) cos(flag * angle * CV_PI / 180.);
m[1] = (float) sin(flag * angle * CV_PI / 180.);
m[3] = -m[1];
m[4] = m[0];
// 将旋转中心移至图像中间
m[2] = w * 0.5f;
m[5] = h * 0.5f;
//
CvMat M = cvMat(2, 3, CV_32F, m);
cvGetQuadrangleSubPix(temp, dst1, &M);
cvReleaseImage(&temp);
dst2 = dst1;
cvReleaseImage(&dst1);
return dst1;
}
int main(int argc, char **argv)
{
IplImage *src = 0;
IplImage *dst = 0;
// 旋转角度
int angle = 90;
src = cvLoadImage("F:\\1.jpg",CV_LOAD_IMAGE_COLOR);
cvNamedWindow("src", 1);
cvShowImage("src", src);
int i;
for (i = 0; i < 10000; i++)
{
dst = rotateImage(src, angle, false);
cvNamedWindow("dst", 2);
cvShowImage("dst", dst);
cvWaitKey(20);
}
cvReleaseImage(&src);
cvReleaseImage(&dst);
return 0;
}