zoukankan      html  css  js  c++  java
  • opencv学习--透视变化

    透视变换和仿射变换具有很大的相同特性,前面提到了放射变化,这里再次把它拿出和透视变换进行比较

     1 #include"cv.h"
     2 #include"highgui.h"
     3 using namespace cv;
     4 void WarpPerspective(IplImage *img);
     5 void WarpFangshe(IplImage *img);
     6 int main()
     7 {
     8     IplImage *getimg = cvLoadImage("e:/picture/Wife2.jpg");
     9     IplImage *img = cvCreateImage(cvSize(350,450),getimg->depth,getimg->nChannels);
    10     cvResize(getimg,img);
    11     WarpPerspective(img);
    12     //WarpFangshe(img);
    13     cvWaitKey();
    14     cvDestroyAllWindows();
    15     return 0;
    16 }
    17 //透视变换
    18 //任意四边形的变换
    19 //透视变换需要的设置四个点
    20 void WarpPerspective(IplImage *img)
    21 {
    22     IplImage *dst = cvCreateImage(cvGetSize(img),img->depth,img->nChannels);
    23     CvMat *mat = cvCreateMat(3,3,CV_32FC1);
    24     CvPoint2D32f ori_point[4], dst_point[4];
    25     ori_point[0].x = 0; ori_point[0].y = 0;
    26     ori_point[1].x = img->width - 1; ori_point[1].y = 0;
    27     ori_point[2].x = 0; ori_point[2].y = img->height-1;
    28     ori_point[3].x = img->width - 1; ori_point[3].y = img->height - 1;
    29     dst_point[0].x = img->width / 2; dst_point[0].y = img->height*0.05;
    30     dst_point[1].x = img->width*0.3;; dst_point[1].y = img->height / 2;
    31     dst_point[2].x = img->width*0.7; dst_point[2].y = img->height / 2;
    32     dst_point[3].x = img->width / 2; dst_point[3].y = img->height*0.9;
    33     //获取映射矩阵
    34     cvGetPerspectiveTransform(ori_point,dst_point,mat);
    35     cvWarpPerspective(img,dst,mat);
    36     //cvFlip(dst,dst,1);
    37     cvNamedWindow("origin");
    38     cvNamedWindow("Warp");
    39     cvShowImage("origin",img);
    40     cvShowImage("Warp",dst);
    41     cvReleaseImage(&img);
    42     cvReleaseImage(&dst);
    43     cvReleaseMat(&mat);
    44 }
    45 void WarpFangshe(IplImage *img)//仿射变换
    46 {
    47     //定义两个CvPoint2D32F的数组
    48     //第一个数组用来标定将要变换的原始图像中的区域
    49     //第二个数组用来标定变换后的图像在窗口中的位置
    50     CvPoint2D32f SrcTri[3], DstTri[3];
    51     //定义仿射映射矩阵,然后计算(2*3的矩阵)
    52     CvMat *warp_mat = cvCreateMat(2, 3, CV_32FC1);
    53     CvMat *rot_mat = cvCreateMat(2, 3, CV_32FC1);
    54     IplImage *src, *dst;
    55     src = img;
    56     dst = cvCloneImage(src);
    57     SrcTri[0].x = 0; SrcTri[0].y = 0;
    58     SrcTri[1].x = src->width - 1; SrcTri[1].y = 0;
    59     SrcTri[2].x = 0; SrcTri[2].y = src->height - 1;
    60     DstTri[0].x = 0; DstTri[0].y = src->height*0.33;
    61     DstTri[1].x = src->width*0.85; DstTri[1].y = src->height*0.25;
    62     DstTri[2].x = src->width*0.15; DstTri[2].y = src->height*0.7;
    63     //获取映射矩阵
    64     cvGetAffineTransform(SrcTri, DstTri, warp_mat);
    65     //映射变换
    66     cvWarpAffine(src, dst, warp_mat);
    67     cvCopy(dst, img);
    68     cvNamedWindow("Warp");
    69     //cvShowImage("Warp",dst);
    70     //下面是对变换后的图像进一步旋转
    71     CvPoint2D32f center = cvPoint2D32f(src->width / 2, src->height / 2);
    72     double angle = 50.0;
    73     double scale = 0.8;
    74     //旋转图像
    75     //上面center是旋转中心
    76     //下面函数的第二个和第三个参数给出了旋转的角度和缩放的尺度
    77     //最后一个参数是输出的映射矩阵
    78     /*cv2DRotationMatrix(
    79     CvPoint2D32F center
    80     double angle
    81     double scale
    82     CvMat *map_matrix
    83     )*/
    84     cv2DRotationMatrix(center, angle, scale, rot_mat);
    85     cvWarpAffine(src, dst, rot_mat);
    86     cvShowImage("Warp", dst);
    87     cvReleaseImage(&img);
    88     cvReleaseImage(&dst);
    89 }
    What I don't dare to say is I can't!
  • 相关阅读:
    SPOJ 4487. Can you answer these queries VI splay
    Oracle Enterprise Linux 64-bit 下Oracle11g的监听配置改动及測试步骤
    欧拉函数
    安装Windows7步骤
    在Eclipse中执行、配置Hadoop
    java设计模式演示样例
    VC中获取窗体句柄的各种方法
    HTML5 Canvas中实现绘制一个像素宽的细线
    Java实现 蓝桥杯VIP 基础练习 Sine之舞
    Java实现 蓝桥杯VIP 基础练习 Sine之舞
  • 原文地址:https://www.cnblogs.com/sytu/p/3984431.html
Copyright © 2011-2022 走看看