void ljb_cv_rotate_buf_size(IplImage *imgSrc, double degree, int *w_dst, int *h_dst) { double angle, a, b; int w_src, h_src; angle = degree * CV_PI / 180.; a = sin(angle), b = cos(angle); w_src = imgSrc->width; h_src = imgSrc->height; *w_dst = (int)(h_src * fabs(a) + w_src * fabs(b)); *h_dst = (int)(w_src * fabs(a) + h_src * fabs(b)); } void ljb_cv_rotate(IplImage *imgSrc, IplImage *imgDst, double degree) { double angle, a, b; int w_src, h_src, w_dst, h_dst; double map[6]; CvMat map_matrix = cvMat(2, 3, CV_64FC1, map); CvPoint2D32f pt = {0}; angle = degree * CV_PI / 180.; a = sin(angle), b = cos(angle); w_src = imgSrc->width; h_src = imgSrc->height; w_dst = imgDst->width; h_dst = imgDst->height; pt = cvPoint2D32f(w_src / 2, h_src / 2); cv2DRotationMatrix(pt, degree, 1.0, &map_matrix);//旋转中心,角度,尺度,生成2*3旋转矩阵 // Adjust rotation center to dst's center, // otherwise you will get only part of the result map[2] += (w_dst - w_src) / 2; map[5] += (h_dst - h_src) / 2; cvWarpAffine( imgSrc, imgDst, &map_matrix, CV_INTER_LINEAR | CV_WARP_FILL_OUTLIERS, cvScalarAll(0) ); }
IplImage* img = cvLoadImage("E:\bgtest.bmp", CV_LOAD_IMAGE_GRAYSCALE); IplImage* dst = NULL; int dstWidth, dstHeight = 0; IplImage* imgRotate = NULL; CvSize cvsize = {0}; ljb_cv_rotate_buf_size(img, 45, &dstWidth, &dstHeight); cvsize.width = dstWidth; cvsize.height = dstHeight; imgRotate = cvCreateImage(cvsize, IPL_DEPTH_8U, 1); cvZero(imgRotate); ljb_cv_rotate(img, imgRotate, 45); cvSaveImage("E:\saltsingle3000.bmp",imgRotate);
理解可参考:
http://blog.csdn.net/xiaowei_cqu/article/details/7616044
http://blog.csdn.net/augusdi/article/details/9022719