无奈电脑系统重装,又到了要重新配置环境的时候了,正好就顺便写一篇博客记录下整个配置过程吧~
本文是基于当前时间的最新发布的工具环境的搭建教程: OpenCV4.1.1 QT5.13 QtCreater4.9.1 MinGW730_64
目录
1,安装前准备
2,安装QT
3,安装Cmake
4,(最关键的来了!)安装,编译Opencv4.1.1
5,编写QT测试程序,调用已经编译好的opencv库。
首先,你需要准备的安装文件有opencv,cmake,qt三个:
下载地址: https://opencv.org/releases/
文件名:opencv-4.1.1-vc14_vc15.exe
下载地址:http://download.qt.io/archive/qt/5.13/5.13.0/
文件名:qt-opensource-windows-x86-5.13.0.exe
下载地址: https://cmake.org/download/
文件名:cmake-3.15.2-win64-x64.msi
准备好以上三个文件后,下面就是正式开始搭建开发环境了!!
例如我的路径是:
C:D:/WorkApp/QT/Tools/mingw730_64/bin/gcc.exe
C++:D:/WorkApp/QT/Tools/mingw730_64/bin/g++.exe
在第三行的Search中,搜索WITH_QT,打勾。
搜索WITH_OPENGL,打勾。
搜索ENABLE_PRECOMPILED_HEADERS,取消打勾。
搜索QT5_DIR,将“你的QT路径\5.13.0\mingw73_64\lib\cmake\Qt5”填进去。 例如我的是:D:\WorkApp\QT\5.13.0\mingw73_64\lib\cmake\Qt5,完成以上操作后点击Configure进行第二次Configure。
编译命令:
mingw32-make.exe -j (线程数量)
(线程数量根据自己电脑cpu性能酌情填写。4核CPU就填4,8核心就填8,以此类推,例如我是4核心的就填“mingw32-make.exe -j 4”即可)
mingw32-make.exe install
示例代码:
#-------------------------------------------------
#
# Project created by QtCreator 2019-09-01T22:28:36
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = hello_opencv411
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
INCLUDEPATH += $$(OPENCV_SDK_DIR)/include
LIBS += -L$$(OPENCV_SDK_DIR)/x64/mingw/lib \
-lopencv_core411 \
-lopencv_highgui411 \
-lopencv_imgcodecs411 \
-lopencv_imgproc411 \
-lopencv_features2d411 \
-lopencv_calib3d411
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
示例代码:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
using namespace cv;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//加载一张图片
Mat image = imread("D://qtProject/1.jpg");
//新建一个窗口,显示该图片
imshow("My Image", image);
}
MainWindow::~MainWindow()
{
delete ui;
}