图像旋转后的还原图像坐标

来源:互联网 发布:南京行知小学宿舍 编辑:程序博客网 时间:2024/06/05 20:51

需求:对每个新图像中的像素进行遍历。计算像素点在原图像中对应的位置。


由于在求边界时,假定图像进行顺时针旋转,因此此处进行反推新像素位置在原图像中的对应位置时,需要用逆时针计算。

顺时针计算方法是:

[plain] view plain copy print?
  1. X = xcos(theta) + y sin(theta)  
  2. Y = ycos(theta) – x sin(theta)  
逆时针计算方法是:
[plain] view plain copy print?
  1. X= x cos(theta) – ysin(theta)  
  2. Y = xsin(theta) + ycos(theta)  
而图像的坐标轴与平常所用的坐标轴不同。


也就是说,图像的旋转顺时针和逆时针的坐标变换公式与常见坐标的变换公式相反:

逆时针计算方法是:

[plain] view plain copy print?
  1. X = xcos(theta) + y sin(theta)  
  2. Y = y cos(theta) – x sin(theta)  
顺时针计算方法是:
[plain] view plain copy print?
  1. X= x cos(theta) – ysin(theta)  
  2. Y = xsin(theta) + ycos(theta)  
边界xmin,xmax,ymin,ymax的计算方法:
[cpp] view plain copy print?
  1. void bound(int x, int y, float ca, float sa, int *xmin, int *xmax, int *ymin, int *ymax)  
  2. /* int x,y; 
  3.  float ca,sa; 
  4.  int *xmin,*xmax,*ymin,*ymax;*/  
  5. {    
  6.     int rx,ry;  
  7.      // 以左上角为中心逆时针旋转  
  8.     rx = (int)floor(ca*(float)x+sa*(float)y);  
  9.     ry = (int)floor(-sa*(float)x+ca*(float)y);  
  10.   
  11.     if (rx<*xmin) *xmin=rx; if (rx>*xmax) *xmax=rx;  
  12.     if (ry<*ymin) *ymin=ry; if (ry>*ymax) *ymax=ry;  
  13. }  
计算出边界来之后,就可以用下式子计算出新图像的高度和宽度。
[cpp] view plain copy print?
  1. xmin = xmax = ymin = ymax = 0;  
  2.      bound(nx-1,0,ca,sa,&xmin,&xmax,&ymin,&ymax);  
  3.      bound(0,ny-1,ca,sa,&xmin,&xmax,&ymin,&ymax);  
  4.      bound(nx-1,ny-1,ca,sa,&xmin,&xmax,&ymin,&ymax);  
  5.    
  6.      sx = xmax-xmin+1;  
  7.      sy = ymax-ymin+1;  
然后就可以利用cvWarpAffine( src, rotImage, &M,CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS,cvScalarAll(0) )进行旋转。其实,方法cvWarpAffine看起来的效果,貌似是按照图像左上角旋转,然后,平移矩阵a13,a23得到的。即:
[plain] view plain copy print?
  1. xp = ca * x + sa * y - xtrans;  
  2. yp = ca * y – sa * x - ytrans;  
M矩阵为:
[plain] view plain copy print?
  1. m[0] = ca;  
  2. m[1] = sa;  
  3. m[2] =-(float)xmin;  
  4. m[3] =-m[1];  
  5. m[4] = m[0];  
  6. m[5] =-(float)ymin;  
上文中提到的都是从原图计算旋转后新图的问题。怎样进行复原。首先,需要找出原图像的原点。此处就是原图像左上角的点,在新图像中的位置。旋转超过90°和不超过需要分开处理。
[plain] view plain copy print?
  1. void compensate_affine_coor1(int *x0, int *y0, int w1, int h1, float t1, float t2, float Rtheta)  
  2. {  
  3.      // 逆时针旋转时的复原  
  4.      float x_ori, y_ori;     
  5.      float x_tmp, y_tmp;  
  6.    
  7.      float x1 = *x0;  
  8.      float y1 = *y0;  
  9.   
  10.      Rtheta = Rtheta*CV_PI/180;  
  11.      if ( Rtheta <= CV_PI/2 ) {  
  12.          x_ori = 0;  
  13.          y_ori = w1 * sin(Rtheta) / t1;  
  14.      }  
  15.      else {  
  16.          x_ori = -w1 * cos(Rtheta) / t2;  
  17.          y_ori = ( w1 * sin(Rtheta) + h1 * sin(Rtheta-CV_PI/2) ) / t1;  
  18.      }  
  19.   
  20.      float sin_Rtheta = sin(Rtheta);  
  21.      float cos_Rtheta = cos(Rtheta);   
  22.   
  23.      /* project the coordinates of im1 to original image before tilt-rotation transform */  
  24.      /* Get the coordinates with respect to the 'origin' of the original image before transform */  
  25.      x1 = x1 - x_ori;  
  26.      y1 = y1 - y_ori;  
  27.   
  28.      /* Invert tilt */  
  29.      x1 = x1 * t2;  
  30.      y1 = y1 * t1;  
  31.   
  32.      /* Invert rotation (Note that the y direction (vertical) is inverse to the usual concention. Hence Rtheta instead of -Rtheta to inverse the rotation.) */  
  33.      x_tmp = cos_Rtheta*x1 - sin_Rtheta*y1;  
  34.      y_tmp = sin_Rtheta*x1 + cos_Rtheta*y1;   
  35.   
  36.      x1 = x_tmp+1;  
  37.      y1 = y_tmp+1;       
  38.    
  39.      *x0 = x1;  
  40.      *y0 = y1;  
  41. }  
旋转不超过90度时,


因此,O`的x_ori = 0; y_ori = w1 * sin(Rtheta) / t1;

如果超过90度时,


[plain] view plain copy print?
  1. x_ori = -w1 * cos(Rtheta) / t2;  
  2. y_ori = ( w1 * sin(Rtheta) + h1 * sin(Rtheta-CV_PI/2) ) / t1;   
首先将所需要还原的坐标点平移,使之变为相对于O`点的坐标。然后,以O`点,即原来的O点按相反的方向旋转theta角度。注意,w1和h1是旋转前新图像的高宽,而不是旋转后新图像的高和宽。
原创粉丝点击