当前位置:网站首页>QT configures opencv-4.5.1 and runs the program
QT configures opencv-4.5.1 and runs the program
2022-06-10 05:34:00 【Osmanthus rice cake】
There is a little friend in the lab who is engaged in face recognition , So I plan to combine QT Do a face recognition project , So I configured QT Medium OpenCV Environmental Science , Previously configured visual stdio In the environment opencv, however QT It's different .
This article mainly refers to these two places :B Stop video :OpenCV introduction 1:Qt5.142 build OpenCV4.51 learning environment and CSDN Blog :Qt To configure OpenCV course , Personal test has been tried ( A detailed version )
List of articles
1、 To configure OpenCV4.5.1
1.1 download OpenCV4.5.1
The first step is to download from the official website OpenCV-4.5.1, Because the official website could not be downloaded before , So here I give a Baidu network disk link , Extraction code 5205.
1.2 install OpenCV4.5.1
After downloading, you will get such an icon , Double-click to run it :
I choose the current folder for the installation directory , See online say best and qt It's a drive letter , Just like mine qt Is installed in d Discoid .
After installation, you will get a folder , It contains the built build and source, among build It refers to the environment in which the Microsoft platform editor can run directly ,source We need to make use of cmake To convert the file , because qt It uses MinGW compiler ( The bottom is gcc and g++), So we need to compile it ourselves .

1.3 CMAKE compile OpenCV
(CMAKE I ignored the installation steps of , Because of this, I have installed it before , If you want to know more about this step, you can check this article CSDN Blog :Qt To configure OpenCV course , Personal test has been tried ( A detailed version ))
open CMAKE, open OpenCV Of source File path , Select the generation path of the target file , What I set up is D:/Opencv/opencv/Qt_OpenCV_Build, As shown in the figure below :
Then click... In the lower left corner Configure:
Then a specified local compiler will pop up , This is the choice MinGW, As shown in the figure below :
The next step will require us to choose to compile c Document and c++ File compiler ( The third one Fortran Never mind ), Here we use QT Medium gcc and g++;QT The path of these two tools is shown in the figure below :

After choosing CMAKE So I started to work , The red font in the following figure indicates some errors , Or the compilation fails , This requires us to fill in some information according to his tips .
After compiling , Some mistakes we ignore first , Pull down first , find with_qt and with_open_gl, Tick these two , Then continue to click configure. among with_open_gl It is also often used , Some of the libraries may be used later , and opencv There are intersections .opencv Is to get data from an image ,opengl Is to generate pictures from data , Rendering .

After compiling again , The following errors will occur , At this time we need Modify the path one by one : The path before and after modification is shown in the following figure ( Be careful : OPENGL Don't worry about the mistakes in , Revised QT Mistakes in , Other mistakes will be fine ): In fact, this is also mentioned in detail in this article , From here on , The following steps and this article Qt To configure OpenCV course , Personal test has been tried ( A detailed version ) Big difference is not bad , But I think we should finish it , After all, everyone's environment may be different .

After that , You will find It's all right .
Last , Click on Generate, Generate makefile file , When it's done , close Cmake that will do , Now enter the folder we created before 
1.4 download OpenCV4.5.1
- Enter the folder we created before
D:\Opencv\opencv\Qt_OpenCV_Build - Place the mouse in the folder interface , Hold down shift key , Right click at the same time , Click here to open Powershell window (s), Get into Windows Powershell, Input
mingw32-make -j 8- Press enter to execute the command , there -j 8 It's to make CPU Multitasking , You can speed up compilation . This process may last more than an hour .

- 100% When it's done , Input
mingw32-make install


- Exit when finished , In addition, add the following path environment variable that will do
D:\Opencv\opencv\Qt_OpenCV_Build\install\x64\mingw\bin


2、 test QT Program
Here you can test two programs :
1: come from B standing :QT+OpenCV Create beauty software from scratch | Introduction to image processing This is more detailed , But the words written in the blog are rather wordy , You can see it later .
2: be based on Qt Of OpenCV Face recognition ( One ) The second one is used here .
2.1 Create project
- First , establish QT Applications , choice minGW As a compiler
- stay test1.pro Add the following code at the position shown in the figure

INCLUDEPATH += D:/Opencv/opencv/Qt_OpenCV_Build/install/include/opencv2 \ # These two must be added
D:/Opencv/opencv/Qt_OpenCV_Build/install/include
LIBS += D:/Opencv/opencv/Qt_OpenCV_Build/lib/libopencv_*.a
among libopencv_*.a Said to libopencv start , With a All static library files at the end , because .a It's an extension name , With the dll Back . Be careful not to get the backslash wrong , \ There's more to come
Can press ctrl+s Or click qmake Build
The first set ui Interface , Two are used label And three pushbutton,label Set the style sheet , So there is a background color , The following code is added to the stylesheet
QLabel{background-color:rgb(200,101,102);}ui The interface is as follows :


mainwindows.h The code for is as follows :
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <QTimer>
#include <QImage>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/types_c.h> // Prevent error reporting 'CV_BGR2RGB' was not declared
using namespace cv;
using namespace std;
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
QImage Mat2QImage(Mat cvImg);
private slots:
void openCamara(); // Turn on the camera
void readFarme(); // Read the current frame information
void closeCamara(); // Turn off camera .
void takingPictures(); // Taking pictures
private:
Ui::MainWindow *ui;
QTimer *timer;
QImage imag;
Mat cap,cap_gray,cap_tmp; // Define a Mat Variable , Used to store images for each frame
VideoCapture capture; // Declare the video read in class
};
#endif // MAINWINDOW_H
- mainwindows.cpp The code for is as follows :
#include "mainwindow.h"
#include "ui_mainwindow.h"
using namespace cv;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(readFarme())); // Time out , Read the current camera information
connect(ui->open, SIGNAL(clicked()), this, SLOT(openCamara()));
connect(ui->take, SIGNAL(clicked()), this, SLOT(takingPictures()));
connect(ui->close, SIGNAL(clicked()), this, SLOT(closeCamara()));
}
// Turn on the camera
void MainWindow::openCamara()
{
capture.open(0); // Read the video from the camera. If the device has only one camera, the parameters will be transferred 0
qDebug("open");
if (!capture.isOpened()) // First determine whether the camera is turned on
{
qDebug("err");
}
timer->start(20); // Start timing ,20ms Get a frame
}
// Read the camera information
void MainWindow::readFarme()
{
capture>>cap; // Read the current frame
if (!cap.empty()) // Judge whether the current frame is captured successfully ** This step is very important
{
imag = Mat2QImage(cap);
imag = imag.scaled(ui->camera->width(), ui->camera->height(),
Qt::IgnoreAspectRatio, Qt::SmoothTransformation);// Set the picture size and label The length is the same as the width
//imshow(name, cap); // If the current frame is captured successfully , Show
ui->camera->setPixmap(QPixmap::fromImage(imag)); // Show the picture to label On
}
else
qDebug("can not ");
}
// Taking pictures
void MainWindow::takingPictures()
{
capture>>cap; // Read the current frame
if (!cap.empty()) // Judge whether the current frame is captured successfully ** This step is very important
{
imag = Mat2QImage(cap);
imag = imag.scaled(ui->photo->width(), ui->photo->height(),
Qt::IgnoreAspectRatio, Qt::SmoothTransformation);// Set the picture size and label The length is the same as the width
//imshow(name, cap); // If the current frame is captured successfully , Show
ui->photo->setPixmap(QPixmap::fromImage(imag)); // Show the picture to label On
}
else
qDebug("can not ");
}
// Turn off camera , Release resources , Must release ***
void MainWindow::closeCamara()
{
timer->stop(); // Stop reading data .
}
// Picture conversion ( I copied it from the Internet )
QImage MainWindow::Mat2QImage(Mat cvImg)
{
QImage qImg;
if(cvImg.channels()==3) //3 channels color image
{
cv::cvtColor(cvImg,cvImg,CV_BGR2RGB);
qImg =QImage((const unsigned char*)(cvImg.data),
cvImg.cols, cvImg.rows,
cvImg.cols*cvImg.channels(),
QImage::Format_RGB888);
}
else if(cvImg.channels()==1) //grayscale image
{
qImg =QImage((const unsigned char*)(cvImg.data),
cvImg.cols,cvImg.rows,
cvImg.cols*cvImg.channels(),
QImage::Format_Indexed8);
}
else
{
qImg =QImage((const unsigned char*)(cvImg.data),
cvImg.cols,cvImg.rows,
cvImg.cols*cvImg.channels(),
QImage::Format_RGB888);
}
return qImg;
}
MainWindow::~MainWindow()
{
delete ui;
}
2.2 effect
The end result is as follows 


Add a little trick :shift+r You can preview ui Interface
Last
If it helps you , I hope I can Like collection A wave of , Your encouragement is my greatest support , thank you !
边栏推荐
- Flutter file operation
- [stacking | fast scheduling] Top-k problem
- Odometer. JS digital scrolling plug-in
- Be diligent in some habits of foreign lead
- IDC发布《中国云原生市场分析》,蚂蚁集团已成覆盖最全面厂商之一
- [data analysis] RFM analysis method
- Interview question 05.06 Integer conversion
- [live dialogue] graph computing is the next frontier of science and technology
- He said it was difficult to make lead abroad
- Three principles of layout design
猜你喜欢

IDC released China Cloud native market analysis. Ant group has become one of the most comprehensive manufacturers

Five best! Ant group passed the highest level evaluation of the "stability Assurance Plan" of the ICT Institute
![[stacking | fast scheduling] Top-k problem](/img/8e/43739ffd6a24c50d99d21ec0db4d4c.jpg)
[stacking | fast scheduling] Top-k problem

Understand ant bizstack cloud native development and governance platform

自定义Tooltips提示气泡Js插件

Cubase pro 12

Talking about thread pool with pictures and texts

Exception assertion of assertj

全球首个金融图数据库测试基准立项,蚂蚁集团开放专利共建

《模型轻量化-剪枝蒸馏量化系列》YOLOv5无损剪枝(附源码)
随机推荐
MTK platform sensor arch introduction -kernel
使用GAT解析Minidump(图形界面)
.NET C#基础(7):接口 - 人如何和猫互动
[simplify] [exclude]
He said it was difficult to make lead abroad
Hevc HM learning 02
R language uses t.test function to perform Welch double sample t-test analysis and double independent sample t-test on the mean value of target variables corresponding to different groups
Odometer. JS digital scrolling plug-in
mdadm: cannot open /dev/sda1: Device or resource busy
【对话直播】图计算是下一个科技前沿
Power mathematics of leetcode326-3
Talking about thread pool with pictures and texts
Curator - implement service registration and discovery
全球首个金融图数据库测试基准立项,蚂蚁集团开放专利共建
Safari's favorites item does not appear on the home page
The first BMW I brand exclusive experience store was opened to fully demonstrate the charm of BMW electric products
Ant group joined the commitment of low-carbon patents, opened patents to the world free of charge, and promoted energy conservation and emission reduction
Interview question 05.08 draw a straight line
蚂蚁集团三项技术方案入选“2021年信息技术应用创新典型解决方案”
最高奖项!2022数博会领先科技成果“新技术”授予OceanBase数据库