Series of articles:
QT + Opencv development (1)-environment configuration
Article Directory
Features
Realize the functions of opening files, reading image data, sliding bar control binarization, canny, Gaussian blur parameters and displaying processed pictures.
Function realization
Build interface
First build the interface
Code
The most critical code is in mainwindow.cpp, as follows:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "opencv2/opencv.hpp"
#include "QFileDialog"
#include "QtDebug"
cv::Mat _src;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 选取文件
connect(ui->btn_openFile,&QPushButton::clicked,[=](){
//点击选取文件按钮,弹出文件对话框
QString path = QFileDialog::getOpenFileName(this,"打开文件","E:/Pictures/");
//将路径放入到lineEdit中
ui->show_filePath->setText(path);
_src = cv::imread(path.toStdString());
if(_src.empty())
{
qDebug() << "could not load the image...";
return;
}
cv::Mat src = _src.clone();
cv::cvtColor(src, src, cv::COLOR_BGR2RGB);
QImage img = QImage(src.data, src.cols, src.rows, src.step, QImage::Format_RGB888);
// int w = img.width();
// int h = img.height();
// if(w > 800 || h > 800)
// {
// double rate = 888.0 / std::max(w, h);
// int nw = static_cast<int>(rate * w);
// int nh = static_cast<int>(rate * h);
// img = img.scaled(QSize(nw, nh), Qt::KeepAspectRatio);
// }
QPixmap mp;
mp = mp.fromImage(img);
ui->label_src->setPixmap(mp);
ui->label_src->setAlignment(Qt::AlignCenter);
src.release();
});
// canny提取边缘
connect(ui->btn_canny,&QPushButton::clicked,[=](){
cv::Mat src = _src.clone();
if(src.empty())
{
qDebug() << "could not load the image...";
return;;
}
cv::cvtColor(src, src, cv::COLOR_BGR2GRAY);
cv::Mat edge;
ui->text_cannyLowValue->setText(QString::number(ui->slider_cannyLowValue->value()));
ui->text_cannyHighValue->setText(QString::number(ui->slider_cannyHighValue->value()));
cv::Canny(src, edge, ui->slider_cannyLowValue->value(), ui->slider_cannyHighValue->value());
QImage img = QImage(edge.data, edge.cols, edge.rows, edge.step, QImage::Format_Grayscale8);
QPixmap mp;
mp = mp.fromImage(img);
ui->label_canny->setPixmap(mp);
ui->label_canny->setAlignment(Qt::AlignCenter);
src.release();
});
// 二值化
connect(ui->btn_binary,&QPushButton::clicked,[=](){
cv::Mat src = _src.clone();
if(src.empty())
{
qDebug() << "could not load the image...";
return;
}
cv::cvtColor(src, src, cv::COLOR_BGR2GRAY);
cv::Mat bin;
ui->text_binaryValue->setText(QString::number(ui->slider_binaryValue->value()));
cv::threshold(src, bin, ui->slider_binaryValue->value(), 255, cv::THRESH_BINARY);
QImage img = QImage(bin.data, bin.cols, bin.rows, bin.step, QImage::Format_Grayscale8);
QPixmap mp;
mp = mp.fromImage(img);
ui->label_binary->setPixmap(mp);
ui->label_binary->setAlignment(Qt::AlignCenter);
src.release();
});
//高斯模糊
connect(ui->btn_gauss, &QPushButton::clicked,[=](){
cv::Mat pic = _src.clone();
if(pic.empty())
{
qDebug()<<"could not load image ...";
return;
}
cv::Mat gauss_img;
int Ksize =2 * ui->slider_gaussKernalSize->value() + 1;
ui->text_gaussKernalSize->setText(QString::number(Ksize));
cv::GaussianBlur(pic, gauss_img, cv::Size(Ksize, Ksize), 1, 1);
QImage img = QImage(gauss_img.data, gauss_img.cols, gauss_img.rows, gauss_img.step, QImage::Format_BGR888);
QPixmap mp;
mp = mp.fromImage(img);
ui->label_draw->setPixmap(mp);
ui->label_draw->setAlignment(Qt::AlignCenter);
pic.release();
});
}
MainWindow::~MainWindow()
{
delete ui;
_src.release();
}
function display
Bale
windeployqt
Windeployqt is a packaging tool that comes with QT5. It can find all library files used by the program (exe) and copy them to the current file of the exe program.
- Create a new folder, name it my_release, and copy the exe file generated after the above run into it.
2. Open the command prompt as an administrator and enter the current folder
cd /d E:\workSpace\qt_workspace\build-opencv_startDemo-Desktop_Qt_5_15_2_MinGW_64_bit-Release\my_release
Pay attention to the path to write your own path, I am just showing it here.
3. Use windeployqt for packaging
enter:
windeployqt opencv_startDemo.exe
The format is: windeployqt The name of the program you want to package. It
should be noted that you must ensure that windeployqt.exe is already in the environment variable, otherwise there will be an error in this step. If you make a mistake, you can find the location of windeployqt.exe, as shown in the figure below. Just add it to the system environment variable.
Packaged folder:
Pack the folder as a whole to others, and they can run the program.
HM NIS Edit + NSIS
On the basis of the previous step, you can also use the third-party packaging tool HM NIS Edit to further package and generate the wizard-driven installation program Setup.exe
HM NIS Edit download: http://hmne.sourceforge.net/
NSIS: https://nsis.sourceforge.io/Download
After installing these two, you need to open HM NIS Edit to configure the NSIS compiler, otherwise there is no language option when creating a wizard script.
Packaging tutorial:
Reference: https://www.cnblogs.com/yply/p/12001813.html
According to the previous tutorial, there is only one step:
After the packaging is complete, you will get the wizard with the installation program Setup.exe
After sending the installation program to others, others can use custom installation paths and other operations (just like the installation package we usually download).
Other packaging tools:
Engima Virtual Box Download link: http://enigmaprotector.com/en/downloads.html
Use tutorial: https://blog.csdn.net/kangshuaibing/article/details/84951619
You can try it.
Common mistakes in packaging
After Qt software was packaged, it reported an error "Unable to locate program input point_ZN10QArrayData10deallocateEPs_jj on the dynamic link library....exe.
This is caused by the difference between the compilation tool used when building the project and the compilation tool used when packaging the software.
Solution: select the corresponding compilation tool when packaging.
Reference: https://blog.csdn.net/weijifen000/article/details/102537640
related information
- int to Qstring:
Qstring::number(int value)