<!-- lang: cpp -->
//根据角度进行旋转,角度为水平正方向,angle为弧度
void ImageRotate(const IplImage src,IplImage dst,
double angle,CvScalar fillVal,int hor)
{
double scale = 1.0;
double a = 0.0;
CvMat* rot_mat = cvCreateMat(2,3,CV_32FC1);
CvPoint2D32f center = cvPoint2D32f(
0,
0
);
if(hor)
{
a= -(angle)*180/CV_PI;
cv2DRotationMatrix(center,a,scale,rot_mat);
}
else
{
cvSet2D(rot_mat,0,0,cvScalar(1));
cvSet2D(rot_mat,0,1,cvScalar(0));
cvSet2D(rot_mat,0,2,cvScalar(0));
cvSet2D(rot_mat,1,0,cvScalar(-1/tan(angle)));
cvSet2D(rot_mat,1,1,cvScalar(1));
cvSet2D(rot_mat,1,2,cvScalar(0));
}
/*cvSet2D(rot_mat,0,0,cvScalar(1/cos(angle)));
cvSet2D(rot_mat,0,1,cvScalar(-tan(angle)));
cvSet2D(rot_mat,0,2,cvScalar(0));
cvSet2D(rot_mat,0,2,cvScalar(1));
cvSet2D(rot_mat,0,2,cvScalar(0));*/
cvWarpAffine(src,dst,rot_mat,CV_WARP_FILL_OUTLIERS,fillVal);
//对超出的边界的部分用原图中的像素点进行填充
/*for(int i=0;i<src->height;i++)
{
uchar* ptr = (uchar*)(dst->imageData+i*dst->widthStep);
uchar* ptr1 = (uchar*)(src->imageData+i*src->widthStep);
for(int j=0;j<src->width;j++)
{
if(ptr[j*3] == 0 && ptr[j*3+1] == 0 && ptr[j*3+2] == 0)
{
ptr[j*3] = ptr1[j*3];
ptr[j*3+1] = ptr1[j*3+1];
ptr[j*3+2] = ptr1[j*3+2];
}
}
}*/
cvReleaseMat(&rot_mat);
}