OpenCV图像处理基础(C++)

目录

平移变换

旋转&缩放

翻转

仿射变换


平移变换

#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <string.h>
using namespace cv;
using namespace std;
int main()
{
    std::string path = "E:/opencv452/project/task_1/图片/鸣人.jpg";
    cv::Mat img = cv::imread(path);
    if(img.empty()){
        cout << "无法读取图片" << std::endl;
        return 0;
    }
    else{
        cout << "图片读取成功" << std::endl;
    }
    //展示原图
    cv::imshow("原图",img);

    //平移
    Mat translation_img;
    //仿射矩阵,img.clos/2为上下平移距离,img.rows/2为左右平移距离
    Mat warp_matrix = (cv::Mat_<float>(2, 3) << cos(0), -sin(0), img.cols/2 ,
                                                sin(0), cos(0),  img.rows/2  ); 
    cv::warpAffine(img, translation_img, warp_matrix, cv::Size(img.cols, img.rows), cv::INTER_LINEAR);
    cv::imshow("右下方平移", translation_img);

    cv::waitKey();
    return 0;
}

旋转&缩放

#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <string.h>
using namespace cv;
using namespace std;
int main()
{
    std::string path = "E:/opencv452/project/task_1/图片/鸣人.jpg";
    cv::Mat img=cv::imread(path);
    if(img.empty()){
        cout << "无法读取图片" << std::endl;
        return 0;
    }
    else{
        cout << "图片读取成功" << std::endl;
    }
    //展示原图
    cv::imshow("原图",img);

    //旋转 & 缩放
    Mat rotation_img;
    Point center = Point2f((float)(img.cols/2.0),(float)(img.rows/2.0)); //旋转中心
    double angle = -45;  //旋转角度  正数逆时针,负数顺时针
    double scale = 1/sqrt(2); //缩放比例
    Mat warp_matrix = getRotationMatrix2D(center, angle, scale);  //获得仿射矩阵
    warpAffine(img, rotation_img, warp_matrix, cv::Size(img.cols, img.rows), cv::INTER_LINEAR);//仿射变换
    imshow("45度旋转,1/sqrt(2)倍缩放", rotation_img);

    //缩放
    Mat zoom_img;
    resize(img, zoom_img, Size(img.cols / 2, img.rows / 2), 0, 0, INTER_LINEAR);		//图像缩小2倍
    imshow("缩小1/2倍", zoom_img);

    cv::waitKey();
    return 0;
}

翻转

#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <string.h>
using namespace cv;
using namespace std;
int main()
{
    std::string path = "E:/opencv452/project/task_1/图片/鸣人.jpg";
    cv::Mat img=cv::imread(path);
    if(img.empty()){
        cout << "无法读取图片" << std::endl;
        return 0;
    }
    else{
        cout << "图片读取成功" << std::endl;
    }
    //展示原图
    cv::imshow("原图",img);

    //翻转
    Mat flip_img;
    flip(img, flip_img,0); //0:上下翻转;1:左右翻转;-1:上下翻转且左右翻转
    imshow("上下翻转", flip_img);
    flip(img, flip_img,1); //0:上下翻转;1:左右翻转;-1:上下翻转且左右翻转
    imshow("左右翻转", flip_img);
    flip(img, flip_img,-1); //0:上下翻转;1:左右翻转;-1:上下翻转且左右翻转
    imshow("上下且左右翻转", flip_img);

    cv::waitKey();
    return 0;
}

 

仿射变换

#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <string.h>
using namespace cv;
using namespace std;
int main()
{
    std::string path = "E:/opencv452/project/task_1/图片/鸣人.jpg";
    cv::Mat img=cv::imread(path);
    if(img.empty()){
        cout << "无法读取图片" << std::endl;
        return 0;
    }
    else{
        cout << "图片读取成功" << std::endl;
    }
    //展示原图
    cv::imshow("原图",img);

    //仿射变换
    Mat transform_img;
    Point2f srcPoints1[4], dstPoints1[4]; //投影(透视视角)变换
    srcPoints1[0] = Point2f(0, 0);//左上/原点
    srcPoints1[1] = Point2f(0, img.rows);//左下角
    srcPoints1[2] = Point2f(img.cols, 0);//右上角
    srcPoints1[3] = Point2f(img.cols, img.rows);//右下角
    //前4点到后4点变换
    dstPoints1[0] = Point2f(0 + 150, 0 + 150);
    dstPoints1[1] = Point2f(0 + 50, img.rows);
    dstPoints1[2] = Point2f(img.cols - 50, 0 + 50 );
    dstPoints1[3] = Point2f(img.cols, img.rows);
    //获取设计好的仿射矩阵
    Mat M2 = getAffineTransform(srcPoints1, dstPoints1);
    warpAffine(img, transform_img, M2, img.size(), 1, 0);
    imshow("仿射变换", transform_img);

    cv::waitKey();
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值