Qt5.14 与 OpenCV4.5 教程四:图像滤波

一、建立Qt工程

1、选择类别

Qt5.14 与 OpenCV4.5 教程四:图像滤波_第1张图片

2、建立项目名称

Qt5.14 与 OpenCV4.5 教程四:图像滤波_第2张图片

3、选择Qt基类

Qt5.14 与 OpenCV4.5 教程四:图像滤波_第3张图片

4、选择编译器

Qt5.14 与 OpenCV4.5 教程四:图像滤波_第4张图片

5、

在项目的.pro文件中添加语句

INCLUDEPATH += F:\OpenCV4.5.0\opencv\buildmingw\install\include
CONFIG(debug, debug|release): {
LIBS += F:\OpenCV4.5.0\opencv\buildmingw\install\x64\mingw\bin\libopencv_*d.dll
} else:CONFIG(release, debug|release): {
LIBS += -LF:\OpenCV4.5.0\opencv\buildmingw\install\x64\mingw\bin\
    -llibopencv_core450 \
    -llibopencv_highgui450 \
    -llibopencv_imgcodecs450 \
    -llibopencv_imgproc450 \
    -llibopencv_features2d450 \
-llibopencv_calib3d450
}

完成项目配置

二、界面设计

1、界面及属性设置

Qt5.14 与 OpenCV4.5 教程四:图像滤波_第5张图片

先完成载入图像功能:

在头文件添加如下代码

Qt5.14 与 OpenCV4.5 教程四:图像滤波_第6张图片

2、完成载入图像功能

Qt5.14 与 OpenCV4.5 教程四:图像滤波_第7张图片

#include 
#include 
#include 

connect(ui->loadBtn,SIGNAL(clicked()),this, SLOT(loadPicture()));

void Dialog::loadPicture()
{
    QString Fileadd = QFileDialog::getOpenFileName(this,"打开图片");
    if(Fileadd.isEmpty())
    {
        QMessageBox::information(this,"警告","没有选择文件");
        return ;
    }

    srcImage = imread(Fileadd.toLatin1().data());  //读取图片
    cvtColor(srcImage,srcImage1,COLOR_BGR2RGB);    // 图像格式转换
    QImage disImage = QImage((const unsigned char*)(srcImage1.data),srcImage1.cols,srcImage1.rows,QImage::Format_RGB888);

    ui->viewLabel->setPixmap(QPixmap::fromImage(disImage.scaled(ui->viewLabel->size(), Qt::KeepAspectRatio)));
    ui->viewLabel->setScaledContents(true);
}

 

3、界面设计“均值滤波”单选按钮

Qt5.14 与 OpenCV4.5 教程四:图像滤波_第8张图片

4、完成滤波功能

Qt5.14 与 OpenCV4.5 教程四:图像滤波_第9张图片

Qt5.14 与 OpenCV4.5 教程四:图像滤波_第10张图片

void Dialog::meanFilter()
{
    if ( sender() == ui->blurRadioButton )
    {
        if ( !srcImage1.data )
        {
            return ;
        }

        cv::blur(srcImage1,srcImage1,Size(3,3));
        QImage disImage = QImage((const unsigned char*)(srcImage1.data),srcImage1.cols,srcImage1.rows,QImage::Format_RGB888);

        ui->viewLabel->setPixmap(QPixmap::fromImage(disImage.scaled(ui->viewLabel->size(), Qt::KeepAspectRatio)));
        ui->viewLabel->setScaledContents(true);
    }
}

完成保存功能:

Qt5.14 与 OpenCV4.5 教程四:图像滤波_第11张图片

Qt5.14 与 OpenCV4.5 教程四:图像滤波_第12张图片

Qt5.14 与 OpenCV4.5 教程四:图像滤波_第13张图片

void Dialog::savePicture()
{
    QString filename1 = QFileDialog::getSaveFileName(this,tr("Save Image"),"",tr("Images (*.png *.bmp *.jpg)")); //选择路径
    QScreen *screen = QGuiApplication::primaryScreen();
    screen->grabWindow(ui->viewLabel->winId()).save(filename1);
}

5、依次完成高斯、中值,双边滤波

void Dialog::gassianFilter()
{
    if ( sender() == ui->gassianRadioButton )
    {
        if ( !srcImage1.data )
        {
            return ;
        }

        cv::GaussianBlur(srcImage1,srcImage1,Size(3,3),0,0);
        QImage disImage = QImage((const unsigned char*)(srcImage1.data),srcImage1.cols,srcImage1.rows,QImage::Format_RGB888);

        ui->viewLabel->setPixmap(QPixmap::fromImage(disImage.scaled(ui->viewLabel->size(), Qt::KeepAspectRatio)));
        ui->viewLabel->setScaledContents(true);
    }
}

void Dialog::medianFilter()
{
    if ( sender() == ui->medianRadioButton )
    {
        if ( !srcImage1.data )
        {
            return ;
        }

        cv::medianBlur(srcImage1,srcImage1,3);
        QImage disImage = QImage((const unsigned char*)(srcImage1.data),srcImage1.cols,srcImage1.rows,QImage::Format_RGB888);

        ui->viewLabel->setPixmap(QPixmap::fromImage(disImage.scaled(ui->viewLabel->size(), Qt::KeepAspectRatio)));
        ui->viewLabel->setScaledContents(true);
    }
}

void Dialog::bilateralFilter()
{
    if ( sender() == ui->bilateralRadioButton )
    {
        if ( !srcImage1.data )
        {
            return ;
        }

        cv::bilateralFilter(srcImage1,srcImage,25,50,12.5);
        QImage disImage = QImage((const unsigned char*)(srcImage.data),srcImage.cols,srcImage.rows,QImage::Format_RGB888);

        ui->viewLabel->setPixmap(QPixmap::fromImage(disImage.scaled(ui->viewLabel->size(), Qt::KeepAspectRatio)));
        ui->viewLabel->setScaledContents(true);
    }
}

运行结果:

 

你可能感兴趣的:(Qt5,+,OpenCV,Qt5,OpenCV4.5)