• 仿射变换与透视变换


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

    将透视变换写成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));

    --------------------------------- 业精于勤而荒于嬉 行成于思而毁于随 ---------------------------------
  • 相关阅读:
    local 不能解析为127.0.0.1
    完全使用接口方式调用WCF 服务
    【人生】自己对于求职应聘的一些感受
    OO的经典例子
    剪刀、石头、布机器人比赛
    TextTree 文本资料收集轻量级工具
    两个代替重复输入的小工具
    桌面助手 Desktop Helper 自动帮你关闭指定的窗口
    磁盘可用空间平衡
    用C#制造可以继承的“枚举”
  • 原文地址:https://www.cnblogs.com/riddick/p/7398311.html
Copyright © 2020-2023  润新知