C++ 如何在 QtCreator 中链接 opencv 并使用 Qt 库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15881913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:50:42  来源:igfitidea点击:

How to link opencv in QtCreator and use Qt library

c++qtopencvqt-creator

提问by Daniel Katz

This question must be duplicate many times, but it just doesn't work and sometimes it still remains unanswered. Sources of information are mainly these
http://www.laganiere.name/opencvCookbook/chap1s1_2.shtml
http://www.youtube.com/watch?v=dgcXYQijV6c

这个问题必须重复多次,但它不起作用,有时仍然没有答案。信息来源主要是这些
http://www.laganiere.name/opencvCookbook/chap1s1_2.shtml
http://www.youtube.com/watch?v=dgcXYQijV6c

This is the summation of what I think one should/can do. (And now it works for me.) Hopefully I mentioned everything from the very beginning, the aim is to write a very clear tutorial.

这是我认为应该/可以做的事情的总结。(现在它对我有用。)希望我从一开始就提到了所有内容,目的是写一个非常清晰的教程。

Installation of OpenCV for QtCreator

为 QtCreator 安装 OpenCV

  1. I have already MS Visual Studio 2010 Professional installed. (I have a free licence as a student) - I think this is not necessary, just a mention
  2. Download:Qt 5.0.1 for Windows 32-bit (MinGW 4.7, 823 MB)
    2.1 Install:Warning, everything that Qt uses (e.g. OpenCV) must be in directories that don't contain white-spaces in their names. - i.e. "Program Files" is wrong. (But I don't want different program files to accumulate directly on C, so I've only made a folder "Programs" in which everything important is installed)
  3. Download:cmake-2.8.10.2-win32-x86.exe - Install for all users (this can be in Program Files)
  4. Download:OpenCV-2.4.0.exe, extract to: C:\Programs\opencv24- it'll create a dir "opencv"; add another folder "opencv_bin". Now it looks like this:
    C:\Programs\opencv24\opencv*
    C:\Programs\opencv24\opencv_bin
  5. Set PATH environment variable, so that there be a link to MinGW compiler. e.g. C:\Programs\Qt\Qt5.0.1\Tools\MinGW\bin;
  6. Start cmake-gui.exe
    6.1 source code:set the default dir for OpenCV; C:\Programs\opencv24\opencv
    6.2 binaries:set the opencv_bin dir; C:\Programs\copencv24\opencv_bin
    6.3 click configure:
    • Choose MinGW Makefilesand Specify native compilers, click next
    • Field C is for gcc.exe; C:/Programs/Qt/Qt5.0.1/Tools/MinGW/bin/gcc.exe
    • Field C++ is for g++.exe; C:/Programs/Qt/Qt5.0.1/Tools/MinGW/bin/g++.exe
    • Field fortran can be empty, click finish
    6.4 Many red lines will appearTo the search field enter one by one: WITH_QT, WITH_TBB, WITH_IPP, WITH_CUDA, CMAKE_BUILD_TYPE
    • WITH_QT - must be selected.
    • WITH_TBB, WITH_IPP, WITH_CUDA - must be unselected
    • CMAKE_BUILD_TYPE - click and enter a text "Debug" (without quotes).
    • Clear the text from the Search field.
    6.5 click configureand keep clicking configure until all red lines are gone, then click generateand closecmake-gui.exe
  7. Go to the terminal (~command prompt), cd to the directory where are the builds (opencv_bin)and type mingw32-make
  8. When the process ends after a long time, type mingw32-make install
  9. Add into Path variable the path to the QtCreator/binC:\Programs\Qt\Qt5.0.1\Tools\QtCreator\bin
  1. 我已经安装了 MS Visual Studio 2010 Professional。(我作为学生有免费许可证) - 我认为这没有必要,只是提一下
  2. 下载:适用于 Windows 32 位的 Qt 5.0.1 (MinGW 4.7, 823 MB)
    2.1安装:警告,Qt 使用的所有内容(例如 OpenCV)必须位于名称中不包含空格的目录中。- 即“程序文件”是错误的。(但我不希望不同的程序文件直接堆积在 C 上,所以我只创建了一个文件夹“Programs”,其中安装了所有重要的东西)
  3. 下载:cmake-2.8.10.2-win32-x86.exe - 为所有用户安装(这可以在程序文件中)
  4. 下载:OpenCV-2.4.0.exe,解压到:C:\Programs\opencv24- 它会创建一个目录“opencv”;添加另一个文件夹“opencv_bin”。现在看起来像这样:
    C:\Programs\opencv24\opencv*
    C:\Programs\opencv24\opencv_bin
  5. 设置 PATH 环境变量,以便有一个到 MinGW 编译器的链接。例如C:\Programs\Qt\Qt5.0.1\Tools\MinGW\bin;
  6. 启动cmake-gui.exe
    6.1源码:设置OpenCV的默认目录;C:\Programs\opencv24\opencv
    6.2二进制文件:设置 opencv_bin 目录;C:\Programs\copencv24\opencv_bin
    6.3点击配置:
    • 选择MinGW Makefiles指定本机编译器,单击下一步
    • 字段 C 用于 gcc.exe;C:/Programs/Qt/Qt5.0.1/Tools/MinGW/bin/gcc.exe
    • 字段 C++ 用于 g++.exe;C:/Programs/Qt/Qt5.0.1/Tools/MinGW/bin/g++.exe
    • 字段fortran可以为空,点击完成
    6.4会出现很多红线到搜索栏一一输入:WITH_QT、WITH_TBB、WITH_IPP、WITH_CUDA、CMAKE_BUILD_TYPE
    • WITH_QT - 必须被选中。
    • WITH_TBB、WITH_IPP、WITH_CUDA - 必须取消选择
    • CMAKE_BUILD_TYPE - 单击并输入文本“调试”(不带引号)。
    • 清除搜索字段中的文本。
    6.5点击configure,一直点击configure,直到所有红线消失,然后点击generate关闭cmake-gui.exe
  7. 转到终端(~命令提示符),cd 到构建目录(opencv_bin)并输入mingw32-make
  8. 当进程在很长时间后结束时,输入mingw32-make install
  9. 将 QtCreator/bin 的路径添加到 Path 变量中C:\Programs\Qt\Qt5.0.1\Tools\QtCreator\bin

Now I have created a new console app in QtCreator.

现在我在 QtCreator 中创建了一个新的控制台应用程序。

//cvHello.pro
QT       += core
QT       -= gui

TARGET = cvHello
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app
INCLUDEPATH += C:/Programs/opencv24/opencv_bin2/install/include
LIBS += "C:/Programs/opencv24/opencv_bin2/bin/*.dll"

SOURCES += main.cpp
OTHER_FILES += \
    img.JPG

And the main file:

和主文件:

//main.cpp
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"

using namespace std;

int main()
{
    cout << "Hello World!" << endl;

    cv::Mat mat;
    mat = cv::imread("img.JPG");
    cvNamedWindow("hello");
    cv::imshow("hello",mat);

    cvWaitKey(0);

    return 0;
}

采纳答案by Daniel Katz

Finally I am starting to be happy. When adjusting this question I had to try all ways, how to define LIBS. Listing them manually helped, at first I wrote them somehow wrongly.

终于,我开始快乐了。在调整这个问题时,我不得不尝试各种方法,如何定义LIBS。手动列出它们有帮助,起初我以某种方式错误地编写了它们。

This is how it works finally:

这是它最终的工作方式:

LIBS += -LC:\Programs\opencv24\opencv_bin2\bin \
    libopencv_core240d \
    libopencv_highgui240d \
    libopencv_imgproc240d \
    libopencv_features2d240d \
    libopencv_calib3d240d \

Btw if I've made any grammar mistakes, I am sorry for my english. :)

顺便说一句,如果我犯了任何语法错误,我很抱歉我的英语。:)

回答by IntenseCorona

The originally accepted answer did not work for me, I am running MSVC2013 Professional and QT5.9. I found SIMPLE and SUREFIRE CROSS-PLATFORM solution that should help anyone who is trying to link an external library (like openCV) with QT.

最初接受的答案对我不起作用,我正在运行 MSVC2013 Professional 和 QT5.9。我发现 SIMPLE and SUREFIRE CROSS-PLATFORM 解决方案应该可以帮助任何试图将外部库(如 openCV)与 QT 链接的人。

The steps listed below are found in the Qt5 documentation: http://doc.qt.io/qtcreator/creator-project-qmake-libraries.htmlunder the "To Add Library" section.

下面列出的步骤可以在 Qt5 文档中找到:http://doc.qt.io/qtcreator/creator-project-qmake-libraries.html在“添加库”部分下。

  1. Right click on the project file located in the 'project pane' on the left side of the creator... and select "Add Library..."
  2. Follow the instructions of the wizard
  1. 右键单击位于创建者左侧“项目窗格”中的项目文件...并选择“添加库...”
  2. 按照向导的说明进行操作

Let me add some specificity from here:

让我从这里添加一些特殊性:

  1. Select "External Library"
  2. For the "Library File" navigate to your opencv_worldXXX.lib file (or opencv_worldXXXd.lib file, you will notice that by specifying only one or the other the wizard has a checkbox which includes the other automatically) [ex. ...\opencv\build\x64\vc12\lib\opncv_world310.lib]
  3. For the "Include Folder" navigate to the "include" folder within the build. [ex. ...\opencv\build\include]
  4. Select your operating system, dynamic/static library (whichever is appropriate)
  5. Hit NEXT, CLEAN UP, and RUN!
  1. 选择“外部库”
  2. 对于“库文件”,导航到您的 opencv_worldXXX.lib 文件(或 opencv_worldXXXd.lib 文件,您会注意到,通过仅指定一个或另一个,向导具有一个自动包含另一个的复选框)[例如。...\opencv\build\x64\vc12\lib\opncv_world310.lib]
  3. 对于“包含文件夹”,导航到构建中的“包含”文件夹。[前任。...\opencv\build\include]
  4. 选择您的操作系统、动态/静态库(以合适的为准)
  5. 点击下一步,清理,然后运行!