Opencv 任意比例旋转图像

本文转自:http://blog.sina.com.cn/s/blog_400b87c20100cdr4.html

最关键的设置是:cvMat 2行3列的矩阵的值,这是就是计算机图形学里面的变换矩阵。具体见帮助手册 cvGetQuadrangleSubPix 词条

<span style="font-size:18px;">void CCannyDoc::OnConvert() 
{
 //声明IplImage指针
 IplImage * src = NULL;
 IplImage * dst = NULL;
 
 src = cvLoadImage(i,-1);
 int delta = 1;
 int angle = 0;
 int opt = 1;  // 1:旋转加缩放,0:  仅仅旋转
 double factor;
 dst = cvCloneImage (src);  //这里是通过函数来对dst初始化,直接“dst=src”可能导致浅拷贝
 cvNamedWindow ("src", 1);
 cvShowImage ("src", src);
 
 for (;;)
 {
  float m[6];
  // Matrix m looks like:
  //
  // [ m0  m1  m2 ] ===>  [ A11  A12   b1 ]
  // [ m3  m4  m5 ]       [ A21  A22   b2 ]
  //
  CvMat M = cvMat (2, 3, CV_32F, m);   //
  int w = src->width;
  int h = src->height;
  if (opt)  // 旋转加缩放,用到了cos肯定是循环变化
   factor = (cos (angle * CV_PI / 180.) + 1.0) * 2; 
  else   //  仅仅旋转,每次增加一个angle值
   factor = 1;
  m[0] = (float) (factor * cos (-angle * 2 * CV_PI / 180.));
  m[1] = (float) (factor * sin (-angle * 2 * CV_PI / 180.));
  m[3] = -m[1];
  m[4] = m[0];
  // 将旋转中心移至图像中间
  m[2] = w * 0.5f;
  m[5] = h * 0.5f;
  //  dst(x,y) = A * src(x,y) + b
  cvZero (dst);                                  //将dst区域全部赋给空值
  cvGetQuadrangleSubPix (src, dst, &M);
  cvNamedWindow ("dst", 1);
  cvShowImage ("dst", dst);
  
  if (cvWaitKey (1) == 27) //ESC
   break;
  angle = (int) (angle + delta) % 360;
 }   
}</span>

另一个连接也很详细:http://www.cnblogs.com/HappyXie/archive/2011/03/02/1969434.html

你可能感兴趣的:(Opencv 任意比例旋转图像)