环境配置学习视频来源:https://haokan.baidu.com/author/1706884117046568
代码编写复制来源:QT+OpenCV从零打造美颜软件 | 图像处理入门教程_哔哩哔哩_bilibili
电脑环境:Windows11家庭中文版+Qt5.12.12 +VS2015+可以正常开发Qt Widgets Application项目
OpenCv下载地址:Releases - OpenCV
下载完成是这样的:
双击进行安装:选好路径,点Extract开始安装
安装完的样子:
添加环境变量:
在Qt能正常使用的情况下,在pro文件中添加外部库,库文件选:
C:\opencv\build\x64\vc15\lib\opencv_world460d.lib
添加完应该是这样的:
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
win32:CONFIG(release, debug|release): LIBS += -LC:/opencv/build/x64/vc15/lib/ -lopencv_world460
else:win32:CONFIG(debug, debug|release): LIBS += -LC:/opencv/build/x64/vc15/lib/ -lopencv_world460d
else:unix: LIBS += -LC:/opencv/build/x64/vc15/lib/ -lopencv_world460
在pro文件中手动写点内容:
INCLUDEPATH += C:/opencv/build/include
DEPENDPATH += C:/opencv/build/include
到此环境配置结束;文章来源:https://www.toymoban.com/news/detail-457578.html
以下是测试demo的代码,供后续参考复习文章来源地址https://www.toymoban.com/news/detail-457578.html
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFileDialog>
#include <QImage>
#include <QPixmap>
#include <opencv2/opencv.hpp>
using namespace cv;
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
QImage MatToImage(Mat&src);
void whiteFace(Mat&m);
private slots:
void on_pushButton_clicked();
void on_contrastSlider_valueChanged(int value);
void on_brightSlider_valueChanged(int value);
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
private:
Ui::MainWindow *ui;
QString m_FileName;
Mat m_Mat;
float m_Contrast;
int m_Brightness;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_Contrast = 1;
m_Brightness = 0;
}
MainWindow::~MainWindow()
{
delete ui;
}
QImage MainWindow::MatToImage(Mat &src)
{
if(src.type() == CV_8UC3){
QImage image((uchar*)src.data,src.cols,src.rows,static_cast<int>(src.step),QImage::Format_RGB888);
return image.rgbSwapped();
}
else{
QImage image;
return image;
}
}
void MainWindow::whiteFace(Mat &m)
{
for (int i = 0;i<m.rows ;i++ ) {
for (int j =0;j<m.cols ;j++ ) {
for (int k=0;k<3 ;k++ ) {
m.at<Vec3b>(i,j)[k]=saturate_cast<uchar>(m_Contrast*m_Mat.at<Vec3b>(i,j)[k]+m_Brightness);
}
}
}
}
void MainWindow::on_pushButton_clicked()
{
m_FileName = QFileDialog::getOpenFileName(this,"选择图片","","");
if(m_FileName.isEmpty()){return;}
m_Mat = imread(m_FileName.toStdString());
QImage img = MatToImage(m_Mat);
img = img.scaled(ui->label->geometry().width(),ui->label->geometry().height()
,Qt::KeepAspectRatio,Qt::SmoothTransformation);
QPixmap tempPixmap= QPixmap::fromImage(img);
ui->label->setPixmap(tempPixmap);
}
void MainWindow::on_contrastSlider_valueChanged(int value)
{
m_Contrast = 1 + (float)value/10;
Mat result(m_Mat.rows,m_Mat.cols,m_Mat.type());
whiteFace(result);
QImage img = MatToImage(result);
img = img.scaled(ui->label->geometry().width(),ui->label->geometry().height()
,Qt::KeepAspectRatio,Qt::SmoothTransformation);
QPixmap tempPixmap= QPixmap::fromImage(img);
ui->label_4->setPixmap(tempPixmap);
}
void MainWindow::on_brightSlider_valueChanged(int value)
{
m_Brightness = value;
Mat result(m_Mat.rows,m_Mat.cols,m_Mat.type());
whiteFace(result);
QImage img = MatToImage(result);
img = img.scaled(ui->label->geometry().width(),ui->label->geometry().height()
,Qt::KeepAspectRatio,Qt::SmoothTransformation);
QPixmap tempPixmap= QPixmap::fromImage(img);
ui->label_4->setPixmap(tempPixmap);
}
void MainWindow::on_pushButton_2_clicked()
{
int val = 40;
Mat bfMat;
Mat result(m_Mat.rows,m_Mat.cols,m_Mat.type());
bilateralFilter(m_Mat,bfMat,val,val*2,val/2);
GaussianBlur(bfMat,result,Size(3,3),1,1);
addWeighted(bfMat,1.5,result,-0.5,0,result);
QImage img = MatToImage(result);
img = img.scaled(ui->label->geometry().width(),ui->label->geometry().height()
,Qt::KeepAspectRatio,Qt::SmoothTransformation);
QPixmap tempPixmap= QPixmap::fromImage(img);
ui->label_4->setPixmap(tempPixmap);
}
void MainWindow::on_pushButton_3_clicked()
{
Mat hsvMat,destFrame,result;
cvtColor(m_Mat,hsvMat,COLOR_BGR2HSV);
for (int i= 0;i<hsvMat.cols ;i++ ) {
qDebug()<< hsvMat.at<Vec3b>(500,i)[0]<<hsvMat.at<Vec3b>(500,i)[1]<<hsvMat.at<Vec3b>(500,i)[2];
}
inRange(hsvMat,Scalar(10,10,50),Scalar(60,120,190),destFrame);
cvtColor(destFrame,destFrame,COLOR_GRAY2BGR);
bitwise_and(m_Mat,destFrame,result);
QImage img = MatToImage(result);
img = img.scaled(ui->label->geometry().width(),ui->label->geometry().height()
,Qt::KeepAspectRatio,Qt::SmoothTransformation);
QPixmap tempPixmap= QPixmap::fromImage(img);
ui->label_4->setPixmap(tempPixmap);
}
到了这里,关于Qt使用OpenCv的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!