opencv 用户文档 错误更正 仿射变换

来源:互联网 发布:淘宝现金红包哪里抢 编辑:程序博客网 时间:2024/05/25 13:33

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

官方文档连接: http://opencv.willowgarage.com/documentation/cpp/imgproc_geometric_image_transformations.html#warpAffine


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

cv::getRotationMatrix2D¶

Comments from the Wiki

Mat getRotationMatrix2D(Point2f 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:

\begin{bmatrix} \alpha &  \beta & (1- \alpha )  \cdot \texttt{center.x} -  \beta \cdot \texttt{center.y} \\ - \beta &  \alpha &  \beta \cdot \texttt{center.x} - (1- \alpha )  \cdot \texttt{center.y} \end{bmatrix}

where

\begin{array}{l} \alpha =  \texttt{scale} \cdot \cos \texttt{angle} , \\ \beta =  \texttt{scale} \cdot \sin \texttt{angle} \end{array}

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

See also: getAffineTransform() , warpAffine() , transform()


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

\texttt{dst} (x,y) =  \texttt{src} ( \texttt{M} _{11} x +  \texttt{M} _{12} y +  \texttt{M} _{13},  \texttt{M} _{21} x +  \texttt{M} _{22} y +  \texttt{M} _{23})


但是,很明显的,根据上面的计算,得到的 新的  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] 就是上面仿射矩阵中的第二行第三列的元素,可以看到,正确的算法应该是 “+” 号。


原创粉丝点击