QT + Opencv development (1)-environment configuration

QT + OpenCV applet development


Series of articles:
Qt + OpenCV development (two)-use label to display pictures and package

platform

Windows10 + Opencv4.5.0 + Qt4.15.2 + cmake3.19

Environment configuration

download

Qt download: https://www.qt.io/download After
Insert picture description here
clicking it, you will register an account, and you can download it after registration.
When installing, pay attention to download the corresponding version number of Qt corresponding to the compiler version.
Opencv
Insert picture description here
download: https://opencv.org/releases/ cmake download: https://cmake.org/download/
Insert picture description here
download and install it.

Configuration

Environment configuration reference video: MinGW+OpenCV4.2+QT source code compiling to program demonstration

It should be noted that there are two ways to compile in Qt: one is MinGW and the other is MSVC, which are two different compilers.

1. MSVC refers to Microsoft's VC compiler

2. MingGW stands for Minimalist GNU on Windows. It is a collection of freely available and freely distributed Windows specific header files and import libraries using the GNU toolset, allowing you to generate native Windows programs on GNU/Linux and Windows platforms without the need for third-party runtime libraries.

Note the Qt version: the difference between msvc and MinGW version. msvc uses the vs compiler, this version is mainly used for PC development; the MinGW version uses the MinGW compiler, which is mainly used for cross-platform development. The two versions of Qt configuration are completely different. Therefore, when searching for configuration methods on the Internet, you must add keywords such as msvc or MinGW to search. Most of the content on the Internet is about the configuration method of the MinGW version. Msvc version configuration is relatively simple; MinGW version needs to download cmake to compile opencv by itself.

If you use Msvc to compile in Qt, you don't need to recompile OpenCV. The compiled bin and lib are available in opencv-4.5.0\build\x64 downloaded from the official website.
If you use MinGW to compile, you need to recompile Opencv with CMake according to the video.

General process:

  • After installing Qt, add environment variables to bin
    For example: D:\Qt_5.15.2\Tools\mingw810_64\bin
  • Use MinGw to compile OpenCV and
    install CMake first. Note that the bin folder under the Cmake installation file should also be added to the environment variables.
    When configuring, pay attention to selecting the specified generator and specifying the gcc and g++ compilers under Qt_5.15.2\Tools\mingw810_64\bin.
    Insert picture description here
    After configuring, on its default configuration, check and then check:

WITH_OPENGL
WITH_OPENMP
ENABLE_CXX11
BUILD_opencv_world

If BUILD_opencv_world is not checked, the generated files will be messy.

Also remove the check below

WITH_OPENCL_D3D11_NV
WITH_IPP
ENABLE_PRECOMPLIED_H

WITH_OPENCL_D3D11_NV is not used for compilation under MinGW, and errors will occur if it is not removed.
In fact, when compiling Opencv4.5.0, I didn't find the option ENABLE_PRECOMPLIED_H, so I didn't change it. This option should be in version 4.2.

After checking, wait for the red warning to disappear, you can Generate.
Insert picture description here
Then run the command prompt as an administrator, enter the compilation output path newbuild, and enter:

mingw32-make -j8

Press Enter and start the build.
Insert picture description here
During the build process, I encountered an error: gcc: error: long: No such file or directory
Reason : the windres.exe mingw uses cannot handle/escape “long long” given as a cmdline arg

Solution : Find OPENCV_ENABLE_ALLOCATOR_STATS in cmake or search in search, then remove the following check, then Configure and Generate, and then mingw32-make in cmd window to continue compiling.

After the build is complete, enter:

mingw32-make install

The things under the install file are what we need.
Insert picture description here
At this point, the compilation of OpenCV is over.
Don’t forget to add the OpenCV bin path to the environment variable (for example, mine is D:\opencv-4.5.0\newbuild\install\x64 \mingw\bin).

Code demo

Build OpenCV environment in Qt

Just press to create a new project in the video. Note that MinGW 64-bit is selected in Kits (if you use the Opencv version compiled by MinGW)

Insert picture description here
Then right-click the project name, click Add Library>External Library, add the header files and library files in the just compiled install, and
Insert picture description here
then you can type the code!
For more tutorials on Qt, you can refer to the official tutorial , which is quite detailed, and there are many demos, which are quite good for learning.

The main codes:

main.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "opencv2/opencv.hpp"
#include "QHBoxLayout"
#include "QLabel"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    
    
    ui->setupUi(this);
    QHBoxLayout *layout = new QHBoxLayout(ui->centralwidget);
    QLabel *label = new QLabel();
    layout->addWidget(label);
    cv::Mat src = cv::imread("E:/workSpace/qt_workspace/qt_opencvDemo/pictures/test1.jpg");
    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);
    label->setPixmap(mp);
    label->setAlignment(Qt::AlignCenter);
    src.release();
}

MainWindow::~MainWindow()
{
    
    
    delete ui;
}


Code effect:

Insert picture description here

Reference:
https://blog.csdn.net/nohopenolove/article/details/106411477
https://www.bilibili.com/video/BV1Za4y1v7ra?from=search&seid=16325599423545792934

Guess you like

Origin blog.csdn.net/weixin_44456692/article/details/110092678