大家好,又见面了,我是你们的朋友全栈君。
终于实现了在windows下配置Qt和opencv,从刚学opencv尝试,到现在终于配置成功,断断续续经历了一年左右,真实操碎了心。。。走了太多弯路了
系统:windows10
Qt:Qt5.11.1,mingw5.3版本
opencv:3.2.0版本
一直有用最新软件的强迫症,这次屈服了,没有使用opencv3.4.2版本,本人尝试了2.7.13版本到3.4.2版本的所有opencv,花了一天时间,加上今天在官方教程最终完成配置,经过个人血的教训,说明以下几点:
(1)3.3版本之后编译要C++11支持,否则会报错error: 'nullptr' was not declared in this scope
,解决办法是cmake设置flags -std=c++11
(2)3.3版本之后编译sources/modules/videoio/src/cap_dshow.cpp
报错error : 'sprintf_instead_use_StringCbPrintfA_or_StringCchPrintfA' was not declared in this scope ...
,解决办法是在sources/modules/videoio/src/cap_dshow.cpp添加宏定义#define STRSAFE_NO_DEPRECATE
(官网教程是添加#define NO_DSHOW_STRSAFE
,笔者没有测试了,是在网上其他地方找到的答案)
(3)在opencv相关问答网站http://answers.opencv.org/question/192758/compile-error-when-compiling-for-windows-341-dev/是看到这样一段话
if you are using a 64 bit os, you should use mingw64, not the 32bit one (530 might also be too old !)
you have to disable WITH_MFMS (media foundation needs special win sdk, only available for VS)
ENABLE_PRECOMPILED_HEADERS=OFF
WITH_IPP=OFF WITH_TBB=OFF (again libs available are for VS only)
,就是在cmake设置的时候要关闭一些没用的选项
(4)遇过编译完成了,在install的时候报错,提示类似无法编译opencv_python的错误,具体错误没有保存,如果有读者遇到这种错误可以把cmake的选项build_opencv_python
去掉
(5)opencv2.7版本不支持Qt5
(6)编译opencv3.1版本很顺利,但还是无法在Qt上使用,可能是添加的库的问题,没有再试了。
今天在博客https://blog.csdn.net/qq_40680049/article/details/81436663里看到了官网关于配置opencv的介绍,用的是Qt5.9和opencv3.2,因此规规矩矩地重新按照教程走了一遍,重要可以在Qt上使用opencv了,不过也出现了一些小问题。
严重推荐!!!
官方教程:https://wiki.qt.io/How_to_setup_Qt_and_openCV_on_Windows
笔者前面踩过的坑教程里面就有提醒,教程中需要注意的几个地方就是cmake选项要正确,安装完成以后把bin目录添加到环境变量。
在修改pro文件那一步有一个注意的地方,就是添加的LIBS是bin目录下的,我之前都是用的lib目录就一直出错
pro文件:
#-------------------------------------------------
#
# Project created by QtCreator 2018-08-29T14:01:18
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = mingw_opencv
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
SOURCES += \ main.cpp \ mainwindow.cpp
HEADERS += \ mainwindow.h
FORMS += \ mainwindow.ui
INCLUDEPATH += F:\qtbuild3.2\install\include
# 注意这里添加的bin目录下的
LIBS += F:\qtbuild3.2\install\x86\mingw\bin\libopencv_core320.dll
LIBS += F:\qtbuild3.2\install\x86\mingw\bin\libopencv_highgui320.dll
LIBS += F:\qtbuild3.2\install\x86\mingw\bin\libopencv_imgcodecs320.dll
LIBS += F:\qtbuild3.2\install\x86\mingw\bin\libopencv_imgproc320.dll
LIBS += F:\qtbuild3.2\install\x86\mingw\bin\libopencv_features2d320.dll
LIBS += F:\qtbuild3.2\install\x86\mingw\bin\libopencv_calib3d320.dll
测试代码:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// read an image
cv::Mat image = cv::imread("D:\\Files\\qt\\mingw_opencv\\lena.jpg", 1);
if (image.empty())
printf("读取图片错误!");
// create image window named "My Image"
cv::namedWindow("My Image");
// show the image on window
cv::imshow("My Image", image);
}
MainWindow::~MainWindow()
{
delete ui;
}
在测试opencv的时候,使用opencv打开一张图片,图片的路径名一定要正确填写,即使在工程中添加了资源文件,直接使用是会报错的,不知道是opencv的问题还是Qt的问题,windows路径在Qt中处理有点麻烦,比如笔者的一张图片,使用windows的复制路径功能得到D:\Files\qt\mingw_opencv\lena.jpg
,直接使用这个路径运行会报错
需要更改为"D:\\Files\\qt\\mingw_opencv\\lena.jpg"
目前算上基本完成了,强迫症还在,有时间就配置一下最新版opencv
参考链接:
(1)https://wiki.qt.io/How_to_setup_Qt_and_openCV_on_Windows
(2)https://blog.csdn.net/qq_40680049/article/details/81436663
(3)https://blog.csdn.net/scien2011/article/details/52830794
(4)https://blog.csdn.net/lucksis/article/details/60580861
(5)http://answers.opencv.org/question/192758/compile-error-when-compiling-for-windows-341-dev/
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/129313.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...