通过下面这个函数调用
Rotate90(workImg,270); //顺时针旋转
Rotate90(workImg,90); //逆时针旋转
实现,其实用该函数旋转任意度数对正方形图都ok,只是长方形图旋转后会有拉伸部分,不好掌握新图的长宽。
void Rotate90(IplImage *workImg,int angle)
{
int opt = ; // 1: 加缩放 0: 仅旋转
double factor; // 缩放因子
IplImage *pImage;
IplImage *pImgRotation = NULL; pImage = workImg;
pImgRotation = cvCloneImage(workImg); angle=-angle; // 创建 M 矩阵
float m[];
// Matrix m looks like:
// [ m0 m1 m2 ] ----> [ a11 a12 b1 ]
// [ m3 m4 m5 ] ----> [ a21 a22 b2 ] CvMat M = cvMat(,,CV_32F,m);
int w = workImg->height;
int h = workImg->width; if (opt) factor = (cos(angle*CV_PI/.)+1.0)*;
else factor = ; m[] = (float)(factor*cos(-angle*CV_PI/.));
m[] = (float)(factor*sin(-angle*CV_PI/.));
m[] = -m[];
m[] = m[];
// 将旋转中心移至图像中心
m[] = w*0.5f;
m[] = h*0.5f; //---------------------------------------------------------
// dst(x,y) = A * src(x,y) + b
cvZero(pImgRotation);
cvGetQuadrangleSubPix(pImage,pImgRotation,&M);
//--------------------------------------------------------- cvNamedWindow("Rotation Image");
cvFlip(pImgRotation);
cvShowImage("Rotation Image",pImgRotation); cvReleaseImage( &pImgRotation );
cvWaitKey(); cvDestroyWindow("Rotation Image");
}
from: http://blog.csdn.net/abcjennifer/article/details/7269968