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

今天在看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] 就是上面仿射矩阵中的第二行第三列的元素,可以看到,正确的算法应该是 “+” 号。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值