当前位置:网站首页>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 )

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 :
 Insert picture description here
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 .
 Insert picture description here
 Insert picture description here

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 :
 Insert picture description here
Then click... In the lower left corner Configure:
 Insert picture description here
Then a specified local compiler will pop up , This is the choice MinGW, As shown in the figure below : Insert picture description here
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 :
 Insert picture description here
 Insert picture description here
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 .
 Insert picture description here
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 .
 Insert picture description here
 Insert picture description here
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 .
 Insert picture description here
 Insert picture description here
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
 Insert picture description here

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 .

 Insert picture description here

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

 Insert picture description here
 Insert picture description here

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

 Insert picture description here
 Insert picture description here

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
     Insert picture description here
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 :
     Insert picture description here
     Insert picture description here

  • 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
 Insert picture description here
 Insert picture description here
 Insert picture description here

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 !

原网站

版权声明
本文为[Osmanthus rice cake]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206100527156496.html