aihot  2017-05-30 22:55:00  OpenCV |   查看评论   

 

  1. /*  程序名:rotate.c

  2. 功能:读入图像文件,做图像旋转转,然后显示图像在屏幕上

  3. */

  4. #include <stdlib.h>

  5. #include <stdio.h>

  6. #include <math.h>

  7. #include <cv.h>

  8. #include <highgui.h>



  9.  



  10. void myRotate(IplImage *img, int x, int y, float degree,int center[2]);





  11. int main(int argc, char *argv[])


  12.    IplImage* img = 0; 

  13.    int height,width,step,channels;

  14.  uchar *data;

  15.  int center[2]={0,0};

  16.  argv[1]="d://a_base_1.jpg";

  17.  //请自己添加图像文件路径

  18.  

  19.  img=cvLoadImage(argv[1],1);

  20.  if(!img)

  21.  {

  22.   printf("Could not load image file: %s/n",argv[1]);

  23.   exit(0);

  24.  }

  25.  // 获取图像信息

  26.  height    = img->height;  

  27.  width     = img->width; 

  28.  step      = img->widthStep; 

  29.  channels  = img->nChannels;

  30.  data      = (uchar *)img->imageData;

  31.  printf("Processing a %dx%d image with %d channels/n",height,width,channels); 

  32.  

  33.  // 创建窗口 

  34.  cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE); 

  35.  cvMoveWindow("mainWin", 100, 100);

  36.  

  37.  center[0]=width/2;//这两句可以设置旋转中心的坐标

  38.  center[1]=height/2;

  39.  

  40.  // 反转图像

  41.  myRotate(img,0,0,-1,center);

  42.  // 显示图像

  43.  cvShowImage("mainWin", img );

  44.  cvWaitKey(0);

  45.  cvReleaseImage(&img );

  46.  return 0;

  47. }



  48.  



  49. void myRotate(IplImage *img, int x, int y, float degree,int center[2])

  50. {

  51.  

  52.  double angle = degree  * CV_PI / 180.; // 角度(弧度)

  53.     double a = sin(angle), b = cos(angle); // 正弦和余弦的角度

  54.  IplImage* imgSrc=cvCloneImage(img);

  55.  int w_src = imgSrc->width;

  56.  int h_src = imgSrc->height;

  57.  

  58.     // 使w_dst和h_dst适合输出图像

  59.  //int w_dst = int(h_src * a + w_src * b);

  60.     //int h_dst = int(w_src * a + h_src * b);

  61.     //int w_dst = int(h_src * abs(a) + w_src * abs(b));

  62.  // int h_dst = int(w_src * abs(a) + h_src * abs(b));

  63.  

  64.     // 用于WarpAffine的映射矩阵,存储在堆栈数组中

  65.     double map[6];

  66.     CvMat map_matrix = cvMat(2, 3, CV_64FC1, map);

  67.     

  68.     // cv2DRotationMatrix需要旋转中心

  69.     CvPoint2D32f pt = cvPoint2D32f(center[0], center[1]);

  70.     cv2DRotationMatrix(pt, degree, 1.0, &map_matrix);

  71.  

  72.     // 否则你将只得到结果的一部分

  73.     map[2] +=x; 

  74.     map[5] +=y; 

  75.  

  76.     // 我们需要一个目的地图像

  77.     cvWarpAffine(

  78.               imgSrc, 

  79.               img,

  80.               &map_matrix,

  81.               CV_INTER_LINEAR | CV_WARP_FILL_OUTLIERS,

  82.               cvScalarAll(0)

  83.               );

  84. }

 

除特别注明外,本站所有文章均为 赢咖4注册 原创,转载请注明出处来自opencv图像旋转

留言与评论(共有 0 条评论)
   
验证码:
[lianlun]1[/lianlun]