opencv实现图像旋转

1.旋转后图像变大,但是原图像部分保持不变

法1:

  1. #include "cv.h"  
  2. #include "highgui.h"  
  3.   
  4. int main()  
  5. {  
  6.     double degree = 30; // rotate 30 degree  
  7.     double angle = degree  * CV_PI / 180.; // angle in radian  
  8.     double a = sin(angle), b = cos(angle); // sine and cosine of angle  
  9.   
  10.     // Load source image as you wish  
  11.     IplImage *imgSrc = cvLoadImage("l:\\test\\5.jpg");  
  12.     int w_src = imgSrc->width;  
  13.     int h_src = imgSrc->height;  
  14.     cvNamedWindow ("src", 1);  
  15.     cvShowImage ("src", imgSrc);  
  16.     // Make w_dst and h_dst to fit the output image  
  17.     int w_dst = int(h_src * fabs(a) + w_src * fabs(b));  
  18.     int h_dst = int(w_src * fabs(a) + h_src * fabs(b));  
  19.   
  20.     // map matrix for WarpAffine, stored in statck array  
  21.     double map[6];  
  22.     CvMat map_matrix = cvMat(2, 3, CV_64FC1, map);  
  23.   
  24.     // Rotation center needed for cv2DRotationMatrix  
  25.     CvPoint2D32f pt = cvPoint2D32f(w_src / 2, h_src / 2);  
  26.     cv2DRotationMatrix(pt, degree, 1.0, &map_matrix);  
  27.   
  28.     // Adjust rotation center to dst's center,  
  29.     // otherwise you will get only part of the result  
  30.     map[2] += (w_dst - w_src) / 2;  
  31.     map[5] += (h_dst - h_src) / 2;  
  32.   
  33.   
  34.     // We need a destination image  
  35.   
  36.     IplImage *imgDst = cvCreateImage(cvSize(w_dst, h_dst), 8, 3);  
  37.     cvWarpAffine(  
  38.         imgSrc,   
  39.         imgDst,  
  40.         &map_matrix,  
  41.         CV_INTER_LINEAR | CV_WARP_FILL_OUTLIERS,  
  42.         cvScalarAll(0)  
  43.         );  
  44.   
  45.     // Don't forget to release imgSrc and imgDst if you no longer need them  
  46.     cvNamedWindow( "dst_big", 1 );  
  47.     cvShowImage( "dst_big", imgDst);  
  48.     cvWaitKey(0);  
  49.     cvReleaseImage(&imgSrc);  
  50.     cvReleaseImage(&imgDst);  
  51.     return 0;  
  52. }  

 

法2:

  1. #include "cv.h"  
  2. #include "highgui.h"  
  3. #include "math.h"  
  4.   
  5. // clockwise 为true则顺时针旋转,否则为逆时针旋转  
  6. IplImage* rotateImage(IplImage* src, int angle, bool clockwise)  
  7. {  
  8.     angle = abs(angle) % 180;  
  9.     if (angle > 90)  
  10.     {  
  11.         angle = 90 - (angle % 90);  
  12.     }  
  13.     IplImage* dst = NULL;  
  14.     int width =  
  15.         (double)(src->height * sin(angle * CV_PI / 180.0)) +  
  16.         (double)(src->width * cos(angle * CV_PI / 180.0 )) + 1;  
  17.     int height =  
  18.         (double)(src->height * cos(angle * CV_PI / 180.0)) +  
  19.         (double)(src->width * sin(angle * CV_PI / 180.0 )) + 1;  
  20.     int tempLength = sqrt((double)src->width * src->width + src->height * src->height) + 10;  
  21.     int tempX = (tempLength + 1) / 2 - src->width / 2;  
  22.     int tempY = (tempLength + 1) / 2 - src->height / 2;  
  23.     int flag = -1;  
  24.   
  25.     dst = cvCreateImage(cvSize(width, height), src->depth, src->nChannels);  
  26.     cvZero(dst);  
  27.     IplImage* temp = cvCreateImage(cvSize(tempLength, tempLength), src->depth, src->nChannels);  
  28.     cvZero(temp);  
  29.   
  30.     cvSetImageROI(temp, cvRect(tempX, tempY, src->width, src->height));  
  31.     cvCopy(src, temp, NULL);  
  32.     cvResetImageROI(temp);  
  33.   
  34.     if (clockwise)  
  35.         flag = 1;  
  36.   
  37.     float m[6];  
  38.     int w = temp->width;  
  39.     int h = temp->height;  
  40.     m[0] = (float) cos(flag * angle * CV_PI / 180.);  
  41.     m[1] = (float) sin(flag * angle * CV_PI / 180.);  
  42.     m[3] = -m[1];  
  43.     m[4] = m[0];  
  44.     // 将旋转中心移至图像中间  
  45.     m[2] = w * 0.5f;  
  46.     m[5] = h * 0.5f;  
  47.     //  
  48.     CvMat M = cvMat(2, 3, CV_32F, m);  
  49.     cvGetQuadrangleSubPix(temp, dst, &M);  
  50.     cvReleaseImage(&temp);  
  51.     return dst;  
  52. }  
  53.   
  54. int main(int argc, char **argv)  
  55. {  
  56.     IplImage *src = 0;  
  57.     IplImage *dst = 0;  
  58.   
  59.     int angle = 75;  
  60.   
  61.     src = cvLoadImage("L:\\Test\\6.JPG");  
  62.   
  63.     dst = rotateImage(src, angle, false);  
  64.     cvNamedWindow("dst", 1);  
  65.     cvShowImage("dst", dst);  
  66.     cvWaitKey(0);  
  67.   
  68.     cvReleaseImage(&src);  
  69.     cvReleaseImage(&dst);  
  70.     return 0;  
  71. }  



2.图像大小始终不变,实现裁剪

法1:

  1. <span style="color:#000000;">#include "cv.h"  
  2. #include "highgui.h"  
  3. #include "math.h"  
  4. void main( )  
  5. {  
  6.     IplImage *Img_old=cvLoadImage("L:\\Test\\6.JPG");  
  7.     IplImage* Img_tmp =cvCloneImage( Img_old);   
  8.   
  9.     int angle=45;  
  10.     float m[6];              
  11.     CvMat M = cvMat( 2, 3, CV_32F, m );  
  12.     CvPoint2D32f center;  
  13.     center.x=float (Img_old->width/2.0+0.5);  
  14.     center.y=float (Img_old->height/2.0+0.5);     
  15.     cv2DRotationMatrix( center, angle,1, &M);  
  16.   
  17.     cvWarpAffine(Img_old,Img_tmp, &M,CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS,cvScalarAll(0) );  
  18.   
  19.     cvNamedWindow ("src", 1);  
  20.     cvShowImage ("src", Img_old);  
  21.     cvNamedWindow( "dst", 1 );  
  22.     cvShowImage( "dst", Img_tmp );  
  23.   
  24.     cvWaitKey(0);  
  25.     cvReleaseImage(&Img_old);  
  26.     cvReleaseImage(&Img_tmp);  
  27. }</span>  

 

法2:

  1. #include "cv.h"  
  2. #include "highgui.h"  
  3. #include "math.h"  
  4. void main( )  
  5. {  
  6.     IplImage *Img_old=cvLoadImage("L:\\Test\\6.JPG");  
  7.     IplImage* Img_tmp = NULL;   
  8.   
  9.     int angle=45;  
  10.     double anglerad  = (CV_PI* (angle/180)) ;  
  11.     int newheight =int (fabs(( sin(anglerad)*Img_old->width )) + fabs(( cos(anglerad)*Img_old->height )) );//出现旋转后外围矩形大小哦!  
  12.     int newwidth  =int (fabs(( sin(anglerad)*Img_old->height)) + fabs(( cos(anglerad)*Img_old->width)) );  
  13.     Img_tmp = cvCreateImage(cvSize(newwidth,newheight), IPL_DEPTH_8U, 3);  
  14.     cvFillImage(Img_tmp,0);  
  15.   
  16.     //对图像进行扩展   
  17.     //只能一定角度以内 不同象限的不同对待  
  18.     int dx=int((newwidth -Img_old->width )/2+0.5);  
  19.     int dy=int((newheight-Img_old->height)/2+0.5);   
  20.      //为了不越界   
  21.     uchar* old_ptr,*temp_ptr;  
  22.     forint y=0 ; y<Img_old->height; y++)  
  23.     {   
  24.         for (int x=0 ; x< Img_old->width; x++)  
  25.         {  
  26.             old_ptr = &((uchar*)(Img_old->imageData + Img_old->widthStep*y))[(x)*3];  
  27.             temp_ptr = &((uchar*)(Img_tmp->imageData + Img_tmp->widthStep*(y+dy)))[(x+dx)*3];  
  28.             temp_ptr[0]=old_ptr[0]; //green   
  29.             temp_ptr[1]=old_ptr[1]; //blue  
  30.             temp_ptr[2]=old_ptr[2]; //Red  
  31.         }  
  32.     }//这里去掉就不行了  
  33.   
  34.     float m[6];              
  35.     CvMat M = cvMat( 2, 3, CV_32F, m );  
  36.     CvPoint2D32f center;  
  37.     center.x=float (Img_tmp->width/2.0+0.5);  
  38.     center.y=float (Img_tmp->height/2.0+0.5);     
  39.     cv2DRotationMatrix( center, angle,1, &M);  
  40.   
  41.     IplImage* temp = cvCloneImage( Img_tmp );  
  42.     cvWarpAffine( Img_tmp, temp, &M,CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS,cvScalarAll(0) );  
  43.     Img_tmp=cvCloneImage( temp );  
  44.   
  45.     cvNamedWindow ("src", 1);  
  46.     cvShowImage ("src", Img_old);  
  47.     cvNamedWindow( "dst", 1 );  
  48.     cvShowImage( "dst", Img_tmp );  
  49.     cvWaitKey(0);  
  50.     cvReleaseImage(&Img_old);  
  51.     cvReleaseImage(&Img_tmp);  
  52. }  


 

3.图像大小不变,原图像内容也均保留,但是是由缩放所达到的

  1. #include "cv.h"  
  2. #include "highgui.h"  
  3.   
  4. int _tmain(int argc, _TCHAR* argv[])  
  5. {  
  6.    CvPoint2D32f srcTri[3],dstTri[3];  
  7.    CvMat* rot_mat=cvCreateMat(2,3,CV_32FC1);  
  8.    CvMat* warp_mat=cvCreateMat(2,3,CV_32FC1);  
  9.    IplImage *src,*dst;  
  10.   
  11.    if(argc==2 && ((src=cvLoadImage(argv[1],1))!=0))  
  12.    {  
  13.       dst=cvCloneImage(src);  
  14.       dst->origin =src->origin ;  
  15.       cvZero(dst);  
  16.       //compute warp matrix  
  17.       srcTri[0].x =0;                            //src top left  
  18.       srcTri[0].y=0;  
  19.       srcTri[1].x =src->width -1;         //src top right  
  20.       srcTri[1].y=0;  
  21.       srcTri[2].x=0;                            //src bottom left offset  
  22.       srcTri[2].y=src->height -1;  
  23.       dstTri[0].x=src->width *0.0;       //dst top left  
  24.       dstTri[0].y=src->height *0.33;  
  25.       dstTri[1].x=src->width *0.85;     //dst top right  
  26.       dstTri[1].y=src->height *0.25;  
  27.       dstTri[2].x=src->width *0.15;     //dst bottom left offset  
  28.       dstTri[2].y=src->height *0.7;  
  29.       cvGetAffineTransform(srcTri,dstTri,warp_mat);  
  30.       cvWarpAffine(src,dst,warp_mat);  
  31.       cvCopy(dst,src);  
  32.       //compute rotation matrix  
  33.       CvPoint2D32f center=cvPoint2D32f(src->width /2,src->height /2);  
  34.       double angle=-50.0;  
  35.       double scale=0.6;  
  36.       cv2DRotationMatrix(center,angle,scale,rot_mat);  
  37.       //do the transformation  
  38.       cvWarpAffine(src,dst,rot_mat);  
  39.       cvNamedWindow("Affine_Transform",1);  
  40.       cvShowImage("Affine_Transform",dst);  
  41.       cvWaitKey();  
  42.     }  
  43.   
  44.    cvReleaseImage(&dst);  
  45.    cvReleaseMat(&rot_mat);  
  46.    cvReleaseMat(&warp_mat);  
  47.    return 0;  
  48. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值