如何链接opencv在QtCreator和使用Qt库 [英] How to link opencv in QtCreator and use Qt library
问题描述
这个问题必须重复多次,但它只是不工作,有时它仍然没有回答。信息来源主要是以下
http://www.laganiere.name/opencvCookbook/chap1s1_2.shtml
http://www.youtube.com/watch?v=dgcXYQijV6c
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
这是我认为应该/可以做的总结。希望我从一开始就提到一切,目的是写一个非常清晰的教程。
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.
安装的OpenCV for QtCreator
- 我已经安装了MS Visual Studio 2010 Professional。 (我有一个学生的免费许可证) - 我认为这是没有必要的,只是提一下
- 下载: Qt 5.0.1 for Windows 32位(MinGW 4.7,823 MB)
2.1 安装:警告,Qt使用的一切(例如OpenCV)必须在名称中不包含空格的目录中。 - 即程序文件是错误的。 (但我不想让不同的程序文件直接在C上累积,所以我只创建了一个文件夹程序,其中一切重要的安装) - 下载: cmake-2.8.10.2-win32-x86.exe - 为所有用户安装(可以在程序文件中)
- 2.4.0.exe,解压到: C:\Programs\opencv24 - 它会创建一个diropencv;添加另一个文件夹opencv_bin。现在看起来像这样:
C:\Programs \opencv24\opencv *
C:\Programs\opencv24 \opencv_bin - 设置PATH环境变量,以便有一个指向MinGW编译器的链接。例如 C:\Programs\Qt\Qt5.0.1\Tools\MinGW\bin;
- 启动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 ++。 C:/Programs/Qt/Qt5.0.1/Tools/MinGW/bin/g ++。exe
字段fortran可以为空,单击完成
- 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
- 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) - Download: cmake-2.8.10.2-win32-x86.exe - Install for all users (this can be in Program Files)
- 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 - Set PATH environment variable, so that there be a link to MinGW compiler. e.g. C:\Programs\Qt\Qt5.0.1\Tools\MinGW\bin;
- 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 Makefiles and 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
- 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.
现在我在QtCreator中创建了一个新的控制台应用程序。
Now I have created a new console app in 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
主文件:
//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;
}
推荐答案
快乐。当调整这个问题,我不得不尝试所有的方式,如何定义LIBS。手动帮助列出他们,一开始我写错了他们。
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 += -LC:\\Programs\\opencv24\\opencv_bin2\\bin \
libopencv_core240d \
libopencv_highgui240d \
libopencv_imgproc240d \
libopencv_features2d240d \
libopencv_calib3d240d \
Btw如果我犯了任何语法错误,我很抱歉我的英语。 :)
Btw if I've made any grammar mistakes, I am sorry for my english. :)
这篇关于如何链接opencv在QtCreator和使用Qt库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!