ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

OpenCV图像旋转(cv::rotate)与镜像(cv::flip)

2022-05-10 23:03:46  阅读:384  来源: 互联网

标签:chooseImageBtn rotate clicked setText flip connect rotate1 cv QRadioButton


一、概述

  案例:使用OpenCV实现图像的旋转和镜像操作

  所用函数:这里主要使用到了两个函数

    1.旋转:cv::rotate

    2.镜像:cv::flip  

rotate(InputArray src, OutputArray dst, int rotateCode);
src:输入图像
dst:输出图像
rotateCode:
  ROTATE_180,顺时针180°
  ROTATE_90_CLOCKWISE,顺时针90°
  ROTATE_90_COUNTERCLOCKWISE,逆时针90°
flip(InputArray src, OutputArray dst, int flipCode);
src:输入图像
dst:输出图像
flipCode:
  >0表示y轴翻转
  =0表示x轴翻转
  <0表示xy轴同时翻转

 

二、代码示例

Video_Player_Roate_Flip::Video_Player_Roate_Flip(QWidget *parent)
    : QWidget{parent}
{
    this->setWindowTitle("图片旋转与镜像");
    this->setFixedSize(320,480);
    //选择图片
    QPushButton *chooseImageBtn = new QPushButton(this);
    chooseImageBtn->setText("选择图片");
    connect(chooseImageBtn,&QPushButton::clicked,[=](){//选择图片
        chooseImage();
    });
    //图像旋转
    QRadioButton *rotate1 = new QRadioButton(this);
    rotate1->move(0,chooseImageBtn->y()+chooseImageBtn->height()+5);
    rotate1->setText("顺时针180°");
    QRadioButton *rotate2 = new QRadioButton(this);
    rotate2->move(rotate1->x()+rotate1->width()+5,chooseImageBtn->y()+chooseImageBtn->height()+5);
    rotate2->setText("顺时针90°");
    QRadioButton *rotate3 = new QRadioButton(this);
    rotate3->move(rotate2->x()+rotate2->width()+5,chooseImageBtn->y()+chooseImageBtn->height()+5);
    rotate3->setText("逆时针90°");
    connect(rotate1,&QRadioButton::clicked,[=](){
        showImageRoate(0);
    });
    connect(rotate2,&QRadioButton::clicked,[=](){
        showImageRoate(1);
    });
    connect(rotate3,&QRadioButton::clicked,[=](){
        showImageRoate(2);
    });

    //图像镜像
    QRadioButton *rotate4 = new QRadioButton(this);
    rotate4->move(0,rotate1->y()+rotate1->height()+5);
    rotate4->setText("沿y轴翻转");
    QRadioButton *rotate5 = new QRadioButton(this);
    rotate5->move(rotate4->x()+rotate4->width(),rotate1->y()+rotate1->height()+5);
    rotate5->setText("沿x轴翻转");
    QRadioButton *rotate6 = new QRadioButton(this);
    rotate6->move(rotate5->x()+rotate5->width(),rotate1->y()+rotate1->height()+5);
    rotate6->setText("沿xy轴翻转");
    connect(rotate4,&QRadioButton::clicked,[=](){
        showImageFlip(0);
    });
    connect(rotate5,&QRadioButton::clicked,[=](){
        showImageFlip(1);
    });
    connect(rotate6,&QRadioButton::clicked,[=](){
        showImageFlip(2);
    });
}

void Video_Player_Roate_Flip::chooseImage(){
    path = QFileDialog::getOpenFileName(this,"选择图像","/Users/yangwei/Downloads/","Image File(*.jpg *.jpeg *.png *.bmp)");
    qDebug()<<path;
}

void Video_Player_Roate_Flip::showImageRoate(int type){
    Mat src = imread(path.toStdString().c_str());
    if(src.empty()){
        qDebug()<<"不能为空";
        return;
    }
    imshow("src",src);
    Mat dst;
    switch(type){
    case 0://
        cv::rotate(src,dst,ROTATE_180);//顺时针180°
        break;
    case 1:
        cv::rotate(src,dst,ROTATE_90_CLOCKWISE);//顺时针90°
        break;
    case 2:
        cv::rotate(src,dst,ROTATE_90_COUNTERCLOCKWISE);//逆时针90°
        break;
    }
    imshow("dst",dst);

}

void Video_Player_Roate_Flip::showImageFlip(int type){
    Mat src = imread(path.toStdString().c_str());
    if(src.empty()){
        qDebug()<<"不能为空";
        return;
    }
    imshow("src",src);
    Mat dst;
    switch(type){
    case 0:
        cv::flip(src,dst,1);//y轴翻转
        break;
    case 1:
        cv::flip(src,dst,0);//x轴翻转
        break;
    case 2:
        cv::flip(src,dst,-1);//xy轴翻转
        break;
    }
    imshow("dst",dst);
}

 

三、演示图像

 

 

 

标签:chooseImageBtn,rotate,clicked,setText,flip,connect,rotate1,cv,QRadioButton
来源: https://www.cnblogs.com/tony-yang-flutter/p/16255824.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有