图像旋转
IplImage *rotateImage(const IplImage *src, float angleDegrees)
{
// Create a map_matrix, where the left 2x2 matrix
// is the transform and the right 2x1 is the dimensions.
float m[6];
CvMat M = cvMat(2, 3, CV_32F, m);
int w = src->width;
int h = src->height;
float angleRadians = angleDegrees * ((float)CV_PI / 180.0f);
m[0] = (float)( cos(angleRadians) );
m[1] = (float)( sin(angleRadians) );
m[3] = -m[1];
m[4] = m[0];
m[2] = w*0.5f;
m[5] = h*0.5f;
// Make a spare image for the result
CvSize sizeRotated;
sizeRotated.width = cvRound(w);
sizeRotated.height = cvRound(h);
// Rotate
IplImage *imageRotated = cvCreateImage( sizeRotated,
src->depth, src->nChannels );
// Transform the image
cvGetQuadrangleSubPix( src, imageRotated, &M);
cvNamedWindow("dst",1);
cvShowImage("dst",imageRotated);
return imageRotated;
}
void main(int argc,char** argv)
{
IplImage *src = cvLoadImage("qb.jpg",1);
cvNamedWindow("src",1);
cvShowImage("src",src);
//rotateImage(src, 30);
IplImage *dst = cvCloneImage(src);
dst->origin=src->origin;
cvZero(dst);
CvPoint2D32f srcTri[4],dstTri[4];
CvMat* rot_mat = cvCreateMat(3,3,CV_32FC1);
CvMat* warp_mat = cvCreateMat(3,3,CV_32FC1);
srcTri[0].x=0;
srcTri[0].y=0;
srcTri[1].x=src->width-1;
srcTri[1].y=0;
srcTri[2].x=0;
srcTri[2].y=src->height-1;
srcTri[3].x=src->width-1;
srcTri[3].y=src->height-1;
dstTri[0].x=src->width*0.0;
dstTri[0].y=src->height*0.0;
dstTri[1].x=src->width*0.85;
dstTri[1].y=src->height*0.15;
dstTri[2].x=src->width*0.15;
dstTri[2].y=src->height*0.7;
dstTri[3].x=src->width*0.8;
dstTri[3].y=src->height*0.9;
//cvGetAffineTransform(srcTri,dstTri,warp_mat);
//cvWarpAffine(src,dst,warp_mat);
cvGetPerspectiveTransform(srcTri,dstTri,warp_mat);
cvWarpPerspective(src,dst,warp_mat);
cvNamedWindow("dst0",1);
cvShowImage("dst0",dst);
//cvCopy(dst,src);
cvZero(dst);
/* CvPoint2D32f center=cvPoint2D32f(src->width/2,src->height/2);
double angle=-30.0;//旋转角度
double scale=1;//缩放比例
cv2DRotationMatrix(center,angle,scale,rot_mat);
cvWarpAffine(src,dst,rot_mat,CV_INTER_LINEAR);
cvNamedWindow("dst1",1);
cvShowImage("dst1",dst); */
cvWaitKey(0);
cvDestroyWindow("src");
cvReleaseImage(&src);
cvDestroyWindow("dst");
cvReleaseImage(&dst);
/* cvDestroyWindow("g_plane");
cvReleaseImage(&r_plane);
cvDestroyWindow("g_plane");
cvReleaseImage(&b_plane);
cvDestroyWindow("b_plane");
*/
}