• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
james1207

博客园    首页    新随笔    联系   管理    订阅  订阅

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


 

posted @ 2013-08-23 18:07  Class Xman  阅读(347)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3