OpenCV2.4.4 图像仿射变换

OpenCV2.4.4 图像仿射变换_第1张图片

OpenCV2.4.4 图像仿射变换_第2张图片


#include<iostream>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>

using namespace std;
using namespace cv;

int main()
{
    Mat src = imread("pic3.png");
    Mat dst_warp,dst_warpRotateScale;
    Point2f srcPoints[3];//原图中的三点
    Point2f dstPoints[3];//目标图中的三点

    //三个点对的值
    srcPoints[0] = Point2f(0,0);
    srcPoints[1] = Point2f(0,src.rows-1);
    srcPoints[2] = Point2f(src.cols-1,0);
    dstPoints[0] = Point2f(0,src.rows*0.3);
    dstPoints[1] = Point2f(src.cols*0.25,src.rows*0.75);
    dstPoints[2] = Point2f(src.cols*0.75,src.rows*0.25);

    Mat M1 = getAffineTransform(srcPoints,dstPoints);//由三个点对计算变换矩阵
    warpAffine(src,dst_warp,M1,src.size());//仿射变换

    //旋转加缩放
    Point2f center(src.cols/2,src.rows/2);//旋转中心
    double angle = 45;//逆时针旋转45度
    double scale = 0.5;//缩放比例

    Mat M2 = getRotationMatrix2D(center,angle,scale);//计算旋转加缩放的变换矩阵
    warpAffine(dst_warp,dst_warpRotateScale,M2,src.size());//仿射变换

    imshow("src",src);
    imshow("dst_warp",dst_warp);
    imshow("dst_warpRotateScale",dst_warpRotateScale);
    waitKey(0);

    return 0;
}

效果图:

OpenCV2.4.4 图像仿射变换_第3张图片

原图


OpenCV2.4.4 图像仿射变换_第4张图片

仿射变换


OpenCV2.4.4 图像仿射变换_第5张图片

旋转加缩放

你可能感兴趣的:(OpenCV2.4.4 图像仿射变换)