OpenCV 几何变换(1)




#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;




Mat src1, src2, gray_src, dst;




int main() {
    
    src1 = imread("/Users/apple/Desktop/test.jpg", IMREAD_COLOR);
    //src2 = imread("/Users/apple/Desktop/test2.jpg", IMREAD_COLOR);
    if (src1.empty()) { // if (!src.data())
        cout << "could not load image..." << endl;
        return -1;
    }
//    imshow("input", src1);
//    waitKey(0);
   
    // 放缩
    // 第一种方法
    Mat s = (Mat_<float>(2, 3) << 0.5, 0, 0, 0, 0.5, 0);
    Mat dst1;
    warpAffine(src1, dst1, s, src1.size());
    // 第二种方法
    Mat dst2;
    resize(src1, dst2, Size(src1.cols / 2, src1.rows / 2), 0.5, 0.5);
    //等效于下两种
    // resize(src1, dst2, Size(), 0.5, 0.5);
    // resize(src1, dst2, src.size(), 0.5, 0.5);
    // show
    imshow("src", src1);
    imshow("warpHffine", dst1);
    imshow("resize", dst2);
    waitKey(0);
    
    // 平移
    s = (Mat_<float>(2, 3) << 1, 0, 100, 0, 1, 100);
    warpAffine(dst1, dst1, s, src1.size());
    imshow("warpHffine", dst1);
    waitKey(0);
    
    
    //旋转
    // 顺时针
    // [ cos(a)  -sin(a)  0]
    // [ sin(a)   cos(a)  0]
    // 逆时针
    // [ cos(a)   sin(a)  0]
    // [-sin(a)   cos(a)  0]
    
    // 此函数仅有90度和180度的旋转
//    rotate(dst1, dst1, ROTATE_90_COUNTERCLOCKWISE);
//    imshow("warpHffine", dst1);
//    waitKey(0);
    
    // 对于任意角度
    // 注意其中角度参数为逆时针旋转角度
    Mat tem = getRotationMatrix2D(Point2f(dst1.cols / 2, dst1.rows / 2), 30, 1);
    warpAffine(dst1, dst1, tem, dst1.size());
    imshow("warpHffine", dst1);
    waitKey(0);
    
    return 0;
}


猜你喜欢

转载自blog.csdn.net/ringggr_h/article/details/80038698