码迷,mamicode.com
首页 > 其他好文 > 详细

仿射变换与透视变换

时间:2017-08-20 00:45:31      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:不能   mic   png   arp   war   特殊   nbsp   矩阵   旋转   

仿射变换保证物体形状的“平直性”和“平行性”。透视变换不能保证物体形状的“平行性”。仿射变换是透视变换的特殊形式。

将透视变换写成3*3矩阵形式,即为M;

以下面这张图为例,实现仿射变换,包括旋转,平移,缩放,剪切,以图像中心为变换中心;

仿射变换

技术分享

旋转(逆时针旋转30度)

Mat M=Mat::eye(3,3, CV_32FC1);
float alpha=PI/6;
float tx=0;
float ty=0;
float scale=1;

M.at<float>(0, 0) = cos(alpha);
M.at<float>(0, 1) = sin(alpha);
M.at<float>(0, 2) = (1-cos(alpha))*center.x - sin(alpha)* center.y;
M.at<float>(1, 0) = -sin(alpha);
M.at<float>(1, 1) = cos(alpha);
M.at<float>(1, 2) = sin(alpha)* center.x +(1-cos(alpha))*center.y;
M.at<float>(2, 0) = 0;
M.at<float>(2, 1) = 0;
M.at<float>(2, 2) = 1;

 技术分享

平移(水平和垂直方向各向右平移20个像素)

 Mat M=Mat::eye(3,3, CV_32FC1);
    float alpha=0;
    float tx=20;
    float ty=20;
    float scale=0;

    M.at<float>(0, 0) = cos(alpha);
    M.at<float>(0, 1) = sin(alpha);
    M.at<float>(0, 2) = (1-cos(alpha))*center.x - sin(alpha)* center.y + tx;
    M.at<float>(1, 0) = -sin(alpha);
    M.at<float>(1, 1) = cos(alpha);
    M.at<float>(1, 2) = sin(alpha)* center.x +(1-cos(alpha))*center.y + ty;
    M.at<float>(2, 0) = 0;
    M.at<float>(2, 1) = 0;
    M.at<float>(2, 2) = 1;

 技术分享

缩放(图像缩小1/2)

Mat M=Mat::eye(3,3, CV_32FC1);
    float alpha=0;
    float tx=0;
    float ty=0;
    float scale=0.5;

    M.at<float>(0, 0) = cos(alpha) * scale;
    M.at<float>(0, 1) = sin(alpha) * scale;
    M.at<float>(0, 2) = (1-cos(alpha)* scale)*center.x - sin(alpha)* center.y* scale + tx;
    M.at<float>(1, 0) = -sin(alpha) * scale;
    M.at<float>(1, 1) = cos(alpha)* scale;
    M.at<float>(1, 2) = sin(alpha)* center.x* scale +(1-cos(alpha)* scale)*center.y + ty;
    M.at<float>(2, 0) = 0;
    M.at<float>(2, 1) = 0;
    M.at<float>(2, 2) = 1;

技术分享

错切变换(剪切变换):

 

Mat M=Mat::eye(3,3, CV_32FC1);
    float alpha=PI/12;
    float tx=0;
    float ty=0;
    float scale=1;

    M.at<float>(0, 0) = 1;
    M.at<float>(0, 1) = sin(alpha);
    M.at<float>(0, 2) = (1-cos(alpha))*center.x - sin(alpha)* center.y;
    M.at<float>(1, 0) = 0;
    M.at<float>(1, 1) = 1;
    M.at<float>(1, 2) = sin(alpha)* center.x +(1-cos(alpha))*center.y;
    M.at<float>(2, 0) = 0;
    M.at<float>(2, 1) = 0;
    M.at<float>(2, 2) = 1;

 技术分享

 

透视变换(透视变换不保证平行性)

Mat M=Mat::eye(3,3, CV_32FC1);
    float alpha=0;
    float tx=0;
    float ty=0;
    float scale=1;

    M.at<float>(0, 0) = cos(alpha);
    M.at<float>(0, 1) = sin(alpha);
    M.at<float>(0, 2) =(1-cos(alpha))*center.x - sin(alpha)* center.y + tx;
    M.at<float>(1, 0) = -sin(alpha);
    M.at<float>(1, 1) = cos(alpha);
    M.at<float>(1, 2) =sin(alpha)* center.x +(1-cos(alpha))*center.y + ty;
    M.at<float>(2, 0) = 0.001;
    M.at<float>(2, 1) = 0.001;
    M.at<float>(2, 2) = 1;

技术分享

上述操作使用同一个opencv接口函数:warpPerspective(srcImg, dstImg, M, Size(width, height));

 

仿射变换与透视变换

标签:不能   mic   png   arp   war   特殊   nbsp   矩阵   旋转   

原文地址:http://www.cnblogs.com/riddick/p/7398311.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!