一、建立Qt工程
1、选择类别
2、建立项目名称
3、选择Qt基类
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、界面及属性设置
先完成载入图像功能:
在头文件添加如下代码
2、完成载入图像功能
#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、界面设计“均值滤波”单选按钮
4、完成滤波功能
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);
}
}
完成保存功能:
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);
}
}
运行结果: