今天在看opencv官方给出的仿射变换计算仿射变换矩阵的文档的时候,发现官方文档中有个很明显的错误,再次给大家提个醒。

 

其中,在说如何计算仿射矩阵的时候, 原文是这样说的:

 

cv::getRotationMatrix2D ​

 

Comments from the Wiki

 

 center, double


 angle, double


 scale

)

Calculates the affine matrix of 2d rotation.

Parameters:


  • center – Center of the rotation in the source image
  • angle – The rotation angle in degrees. Positive values mean counter-clockwise rotation (the coordinate origin is assumed to be the top-left corner)
  • scale – Isotropic scale factor


The function calculates the following matrix:



 

 

where



 

 

The transformation maps the rotation center to itself. If this is not the purpose, the shift should be adjusted.

See also ​

 

然后由仿射矩阵计算每个像素的放射变换后的位置是这样:

 


但是,很明显的,根据上面的计算,得到的 新的  y 的坐标是错误的

上面的仿射变换矩阵中第二行第三列的元素 中间的 减号弄错了,应该是加号

beta * center.x + (1 - alpha)*center.y

这样计算得到的仿射变换后的点的坐标才是正确的。


可以通过查看源码很容易的看到应该是 加号 “+”


 

cv::Mat cv::getRotationMatrix2D( Point2f center, double angle, double scale )
{
angle *= CV_PI/180;
double alpha = cos(angle)*scale;
double beta = sin(angle)*scale;

Mat M(2, 3, CV_64F);
double* m = (double*)M.data;

m[0] = alpha;
m[1] = beta;
m[2] = (1-alpha)*center.x - beta*center.y;
m[3] = -beta;
m[4] = alpha;
m[5] = beta*center.x + (1-alpha)*center.y;

return M;
}


 

 

m[5] 就是上面仿射矩阵中的第二行第三列的元素,可以看到,正确的算法应该是 “+” 号。