• OpenCV 仿射变换


     1 #include "opencv2/highgui/highgui.hpp"
     2 #include "opencv2/imgproc/imgproc.hpp"
     3 #include <iostream>
     4 #include <stdio.h>
     5 
     6 using namespace cv;
     7 using namespace std;
     8 
     9 /// 全局变量
    10 char* source_window = "Source image";
    11 char* warp_window = "Warp";
    12 char* warp_rotate_window = "Warp + Rotate";
    13 
    14 /** @function main */
    15  int main( int argc, char** argv )
    16  {
    17    Point2f srcTri[3];
    18    Point2f dstTri[3];
    19 
    20    Mat rot_mat( 2, 3, CV_32FC1 );
    21    Mat warp_mat( 2, 3, CV_32FC1 );
    22    Mat src, warp_dst, warp_rotate_dst;
    23 
    24    /// 加载源图像
    25    src = imread( argv[1], 1 );
    26 
    27    /// 设置目标图像的大小和类型与源图像一致
    28    warp_dst = Mat::zeros( src.rows, src.cols, src.type() );
    29 
    30    /// 设置源图像和目标图像上的三组点以计算仿射变换
    31    srcTri[0] = Point2f( 0,0 );
    32    srcTri[1] = Point2f( src.cols - 1, 0 );
    33    srcTri[2] = Point2f( 0, src.rows - 1 );
    34 
    35    dstTri[0] = Point2f( src.cols*0.0, src.rows*0.33 );
    36    dstTri[1] = Point2f( src.cols*0.85, src.rows*0.25 );
    37    dstTri[2] = Point2f( src.cols*0.15, src.rows*0.7 );
    38 
    39    /// 求得仿射变换
    40    warp_mat = getAffineTransform( srcTri, dstTri );
    41 
    42    /// 对源图像应用上面求得的仿射变换
    43    warpAffine( src, warp_dst, warp_mat, warp_dst.size() );
    44 
    45    /** 对图像扭曲后再旋转 */
    46 
    47    /// 计算绕图像中点顺时针旋转50度缩放因子为0.6的旋转矩阵
    48    Point center = Point( warp_dst.cols/2, warp_dst.rows/2 );
    49    double angle = -50.0;
    50    double scale = 0.6;
    51 
    52    /// 通过上面的旋转细节信息求得旋转矩阵
    53    rot_mat = getRotationMatrix2D( center, angle, scale );
    54 
    55    /// 旋转已扭曲图像
    56    warpAffine( warp_dst, warp_rotate_dst, rot_mat, warp_dst.size() );
    57 
    58    /// 显示结果
    59    namedWindow( source_window, CV_WINDOW_AUTOSIZE );
    60    imshow( source_window, src );
    61 
    62    namedWindow( warp_window, CV_WINDOW_AUTOSIZE );
    63    imshow( warp_window, warp_dst );
    64 
    65    namedWindow( warp_rotate_window, CV_WINDOW_AUTOSIZE );
    66    imshow( warp_rotate_window, warp_rotate_dst );
    67 
    68    /// 等待用户按任意按键退出程序
    69    waitKey(0);
    70 
    71    return 0;
    72   }
  • 相关阅读:
    windows下pip安装python module失败
    设置jenkins的邮件通知功能
    jenkins 中 violation使用pylint
    pylint & jenkins
    dracut 基本介绍
    etcdctl 命令介绍
    python
    django --------------------- [必要操作]
    vim
    ssl选购
  • 原文地址:https://www.cnblogs.com/ybqjymy/p/12170937.html
Copyright © 2020-2023  润新知