这个过程遇坑无数,耽误了不少时间,记录一下。
第一个坑
如果Windows系统下QT使用opencv,选择 “Sources”,不要选“Windows”下载,“opencv-4.5.5-vc14_vc15.exe”这个安装包编译始终要出错,未找到原因。
环境变量,网上的教程都说要配置,没测试不配置得不得行?主观认为不配置应该也可以。
QT自带CMake,不用专门下载安装。双击“cmake-gui.exe”
"Advanced"要勾选,点击“Configure” 。
选择“MinGW...”,如图。
如图配置gcc和g++。
点击“Configure”后出现一堆红色字。
第二个坑
说明ffmpeg 没下载下来,这时候去编译肯定失败。
临时解决GitHub的raw.githubusercontent.com无法连接问题
通过IPAddress.com首页,输入raw.githubusercontent.com查询到真实IP地址修改hosts
C:\Windows\System32\drivers\etc\hosts 文件增加
多试几次,一定要把ffmpeg下载下来。实在不行就百度其他办法csdn挺多,只不过这个最简单。
点击“Configure”成功后如图。
第三个坑
只增加勾选WITH_QT,不要勾选 WITH_OPENGL,网上有些教程让两个都选,现在已不行了,编译不过。
点击“Configure”后如图
红色不用管,再次点击“configure”。
一切正常, 点击“Generate”
mingw32-make -j 8
mingw32-make install
漫长等待后成功
测试程序如下:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
#include
using namespace cv;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
Mat image=imread("c:/c++app/c.jpg");
namedWindow("Display window",WINDOW_AUTOSIZE );
imshow( "Display window",image );
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
如果用qmake,pro文件配置如下:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
#opencv4 库
INCLUDEPATH += c:\OpenCV4\opencv-build\install\include
LIBS += C:\opencv4\opencv-build\install\x64\mingw\lib\libopencv_*.a
#
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
最后一个坑
现在QT6都提倡用cmake编译,搞懂CMakeLists.txt走了不少弯路。
如果用cmake,配置CMakeLists.txt 如下:
#最低CMake版本
cmake_minimum_required(VERSION 3.5)
#项目名
project(untitled3 VERSION 0.1 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
#C++17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
#设置opencv 路径,搜索opencv依赖项。
#“Found OpenCV Windows Pack but it has no binaries compatible with your configuration.
You should manually point CMake variable OpenCV_DIR to your build of OpenCV library.”原文引用,所以有以下设置。
set(OpenCV_DIR C:/opencv4/opencv-build/install/)
find_package( OpenCV 4.5.5 REQUIRED )
#
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
#向环境中添加指定库的头文件和路径。
#Opencv cmake的模块将这些数据保存在${OpenCV_INCLUDE_DIRS}、${OpenCV_LIBS}。这些数据在编译后的\opencv-build\install 文件夹中“OpenCVConfig.cmake”文件中有定义。
include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${OpenCV_LIBS})
#
set(PROJECT_SOURCES
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(untitled3
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
# Define target properties for Android with Qt 6 as:
# set_property(TARGET untitled3 APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
# ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
if(ANDROID)
add_library(untitled3 SHARED
${PROJECT_SOURCES}
)
# Define properties for Android with Qt 5 after find_package() calls as:
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
else()
add_executable(untitled3
${PROJECT_SOURCES}
)
endif()
endif()
#将可执行文件与opencv库链接
target_link_libraries(untitled3 PRIVATE Qt${QT_VERSION_MAJOR}::Widgets ${OpenCV_LIBS})
#
set_target_properties(untitled3 PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(untitled3)
endif()
搞定,请开始你的学习OpenCV之旅。