How to setup Qt and openCV on Windows

How to setup Qt and openCV on Windows

[[Category:HowTo]]

== Introduction ==

This article shows how to install Qt, build OpenCV, and run a basic OpenCV example. This article assumes Windows 10 has just been installed.

This procedure requires close to 10GB of disk space:

Qt: 5.06GB

opencv: 522MB

opencv-Build: 3.95GB

downloads:152MB 

This article uses information from the following pages:

http://docs.opencv.org/2.4/doc/tutorials/introduction/windows_install/windows_install.html?highlight=installation

http://www.laganiere.name/opencvCookbook/chap1s1_2.shtml

== Windows 10, Qt 5.9,  OpenCV 3.2.0 ==

=== Qt ===

==== Installation ====

Download the Qt installer from [https://www.qt.io/download-open-source/ www.qt.io], then choose "Download now". This will then download qt-unified-windows-x86-2.0.5-online.exe. Execute the program, then choose the following settings:

  Welcome to the Qt online installer: next

  Qt Account - your unified login to everything Qt: skip

  Setup-Qt: next

  installation folder: D:\Qt

  select components: Qt-Qt5.9-MingGW 5.3.0 32 bit

  select components: Qt-Tools-MinGW 5.3.0

  License Agreement: agree and next

  start menu shortcuts: next

  ready to install: install

==== Testing ====

Run D:\Qt\Tools\QtCreator\bin\qtcreator.exe

  File-New file or project-Qt Widgets Application-choose

  enter a name and a location: next

  select all kits: next

  Class information: MainWindow (defaults): Next

  Project management: Finish

Now a new project is made. Start debugging by choosing

  Debug-Start Debugging-Start debugging (F5)

Now the Qt tab in the Windows taskbar should turn into a progress bar. After some time a new empty window should pop up. Stop debugging either by pressing the red cross in the top right of this new window, or choose

  Debug-Stop debugging 

==== Adjust Qt ====

When you need to add, remove or update a component of Qt, this can be done by running D:\Qt\MaintenanceTool.exe:

  maintain Qt: Qt Account: Skip

  Setup Qt: Add or remove components: Next

  Select components:

  next :update

=== Tortoisegit ===

Todo

=== Cmake ===

Download cmake from [https://cmake.org/download/ cmake.org]. In this guide, [https://cmake.org/files/v3.7/cmake-3.7.2-win64-x64.msi 3.7.2] is used. Start cmake-3.7.2-win64-x64.msi, then choose the following settinge:

  Welcome to the CMake etup Wizzard: next

  End-User License Agreement: [X] Accept and next

  Install options: [X] Add CMake to the system PATH for all users, next

  Destination folder: C:\Program Files\CMake (default), next

  Ready to install CMake, Install

=== OpenCV ===

==== Getting OpenCV ====

Download openCV from [https://sourceforge.net/projects/opencvlibrary/ sourceforge]. In this guide, version [https://sourceforge.net/projects/opencvlibrary/files/opencv-win/3.2.0/ 3.2.0] is used. Start opencv-3.2.0-vc14.exe and let it extract to d:\ . Now the folder d:\opencv is created.

==== Add minGW to the windows PATH variable====

Open the control panel,

System and Security,

System,

Advanced system settings,

Environment Variables,

System Variables,

Variable Name: Path

Variable value: ;D:\Qt\Tools\mingw530_32\bin

==== Compiling OpenCV ====

Start C:\Program Files\CMake\bin\cmake-gui.exe then choose the following settings:

  Where is the source code: D:\opencv\sources

  where to build the binaries: D:\opencv-build

Then click Configure, let cmake create the build directory, and choose the following settings:

  Specify the generator for this project: MinGW Makefiles

  Specify native compilers, next

  Compilers C: D:\Qt\Tools\mingw530_32\bin\gcc.exe

  Compilers C++: D:\Qt\Tools\mingw530_32\bin\g++.exe

  Finish

  Check the box [X]WITH_QT

  Check the box [X]WITH_OPENGL

Then click configure again

  Set QT_MAKE_EXECUTABLE to D:\Qt\5.9\mingw53_32\bin\qmake.exe

  Set Qt5Concurrent_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5Concurrent

  Set Qt5Core_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5Core

  Set Qt5Gui_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5Gui

  Set Qt5Test_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5Test

  Set Qt5Widgets_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5Widgets

  Set Qt5OpenGL_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5OpenGL

  Set CMAKE_BUILD_TYPE to Release or RelWithDebInfo

Then click configure again

Then click generate 

Next open cmd, and type the following commands. To speed up the compile, the -j flag can be used to run multiple compile jobs simultaneously. On an 8 core CPU, you can set it to 8 or higher, so all cores are used. On a core i7-3770@3.4GHz with 8GB ram, the compile takes about 6 minutes.

  d:

  cd d:\

  cd opencv-build 

  mingw32-make -j 8

  mingw32-make install



If you have the following error : ... windres.exe: unknown option -- W ...

try this : in cmake uncheck ENABLE_PRECOMPILED_HEADERS

If, in the file  opencv/sources/modules/videoio/src/cap_dshow.cpp, you have the following error : 'sprintf_instead_use_StringCbPrintfA_or_StringCchPrintfA' was not declared in this scope ...

try this :put the following line: #define NO_DSHOW_STRSAFE, before the line : #include "DShow.h"

If you have the error: ‘nullptr’ was not declared in this scope..

try this: in cmake check the box ENABLE_CXX11

If, in the file modules\videoio\src\cap_msmf.cpp you have the error: using invalid field '{anonymous}::ComPtr<T>::p'..

try this: in cmake unchecking WITH_MSMF

==== Add OpenCV compiled libraries to the windows PATH variable====

Open the control panel,

System and Security,

System,

Advanced system settings,

Environment Variables,

System Variables,

Variable Name: Path

Variable value: ;D:\opencv-build\install\x86\mingw\bin


=== Compile and run the example ===

Run D:\Qt\Tools\QtCreator\bin\qtcreator.exe

  File-New file or project-Qt Widgets Application-choose

  enter a name and a location: next

  select all kits: next

  Class information: MainWindow (defaults): Next

  Project management: Finish

Now a new project is made.

modify the .pro file like this:

<syntaxhighlight lang="cpp">

#-------------------------------------------------

#

# Project created by QtCreator 2017-03-05T12:30:06

#

#-------------------------------------------------

QT      += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = opencvtest

TEMPLATE = app

# The following define makes your compiler emit warnings if you use

# any feature of Qt which as 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 += D:\opencv\build\include

LIBS += D:\opencv-build\bin\libopencv_core320.dll

LIBS += D:\opencv-build\bin\libopencv_highgui320.dll

LIBS += D:\opencv-build\bin\libopencv_imgcodecs320.dll

LIBS += D:\opencv-build\bin\libopencv_imgproc320.dll

LIBS += D:\opencv-build\bin\libopencv_features2d320.dll

LIBS += D:\opencv-build\bin\libopencv_calib3d320.dll

# more correct variant, how set includepath and libs for mingw

# add system variable: OPENCV_SDK_DIR=D:/opencv/build

# read http://doc.qt.io/qt-5/qmake-variable-reference.html#libs

#INCLUDEPATH += $$(OPENCV_SDK_DIR)/include

#LIBS += -L$$(OPENCV_SDK_DIR)/x86/mingw/lib \

#        -lopencv_core320        \

#        -lopencv_highgui320    \

#        -lopencv_imgcodecs320  \

#        -lopencv_imgproc320    \

#        -lopencv_features2d320  \

#        -lopencv_calib3d320

</syntaxhighlight>

and modify mainwindow.cpp like this:

<syntaxhighlight lang="cpp">

#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("f://1.jpg", 1);

    // create image window named "My Image"

    cv::namedWindow("My Image");

    // show the image on window

    cv::imshow("My Image", image);

}

MainWindow::~MainWindow()

{

    delete ui;

}

</syntaxhighlight>

Place an image with the name "1.img" in the root of F:\, then run the example. Now 2 windows should pop up. One with the image, and one with an empty window.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,302评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,232评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,337评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,977评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,920评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,194评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,638评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,319评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,455评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,379评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,426评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,106评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,696评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,786评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,996评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,467评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,043评论 2 341

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,269评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,298评论 0 23
  • 2018年6月14日 星期四 今天的任务就是就是召开各组的贫情小组分析会。 今天还是下雨。而且村上今天停水停电,其...
    册名花阅读 104评论 0 0
  • 学习为所失去的感恩,也接纳失去的事实,不管人生的得与失,总是要让自已的生命充满了亮丽与光彩,不再为过去掉泪,努力的...
    姜雷_24b1阅读 184评论 0 0
  • 憨媳妇 憨媳妇的葬礼结束了,李长德老汉坐在冰凉的炕上,一只接一只的抽着烟。 憨媳妇是李长德三十五岁那年刘婶给介绍的...
    鲁冰魂阅读 680评论 0 0