50

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.

Installation of OpenCV for QtCreator

  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 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
    6.4 Many red lines will appear To 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 configure and keep clicking configure until all red lines are gone, then click generate and close cmake-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/bin C:\Programs\Qt\Qt5.0.1\Tools\QtCreator\bin

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

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;
}
Daniel Katz
  • 2,271
  • 3
  • 25
  • 27
  • I define LIBS here so that I use a directory opencv_bin2, because I rebuilt opencv several times. – Daniel Katz Apr 08 '13 at 22:43
  • 4
    My project [cvImage](https://github.com/karlphillip/GraphicsProgramming/tree/master/cvVideo) is available on GitHub and demonstrates how to build a cross-platform application with Qt/OpenCV. Check the **.pro** file. – karlphillip Apr 09 '13 at 02:28
  • I had to use "CMAKE_BUILD_TYPE = Release" rather than Debug as given in [here](http://www.laganiere.name/opencvCookbook/chap1s1_2.shtml) - your first source. – inblueswithu Nov 17 '14 at 18:44
  • Anyways... I could not get this to working!! Not sure.. still pursuing it – inblueswithu Nov 19 '14 at 23:38
  • In CMake I keep getting this error: "Found unsuitable Qt version "" from NOTFOUND, this code requires Qt 4.x" – birgersp Jan 30 '15 at 10:39
  • @BirgerSkogengPedersen, this can be solved by looking here: http://stackoverflow.com/a/23998308/873072 – Maschina Feb 03 '15 at 16:11

2 Answers2

13

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

This is how it works finally:

LIBS += -LC:\\Programs\\opencv24\\opencv_bin2\\bin \
    libopencv_core240d \
    libopencv_highgui240d \
    libopencv_imgproc240d \
    libopencv_features2d240d \
    libopencv_calib3d240d \
Daniel Katz
  • 2,271
  • 3
  • 25
  • 27
  • I still don't know what was wrong with the original LIBS += "C:/Programs/opencv24/opencv_bin2/bin/*.dll" – Daniel Katz Apr 08 '13 at 23:01
  • you need to add .libs because it contain information about functions inside DLL, and this information is needed when building the project, so your exe will run then check those information and then load needed DLL. – Wajdy Essam Apr 09 '13 at 08:20
  • Wajdy: Do you mean not an extention .lib or .libs, but rather any files within a directory lib? Probably those in opencv_bin/lib and not opencv_bin/install/lib. But when I simply change -LC:\\Programs\\opencv24\\opencv_bin2\\bin \ ... to -LC:\\Programs\\opencv24\\opencv_bin2\\lib \ ... then it doesn't work. Are you saying that the solution with \\bin \ ... is wrong? – Daniel Katz Apr 09 '13 at 18:10
  • Wajdy: Even if I use the libs in install\lib it cannot find the .dll.a files though I have a correct path set. – Daniel Katz Apr 09 '13 at 18:25
  • @Wajdy: I've found out that if I use the other bin dir in install\bin, it works too. But in both ways it doesn't work when I start the program by double click on the .exe. - cannot find those dlls. – Daniel Katz Apr 12 '13 at 18:54
  • Following this tutorial with OpenCV 2.46 and Qt Creator 2.7.2 was the only way I was able to make this work. I don't have .NET installed. My .pro looks like the one in this answer. – DanyAlejandro Aug 03 '13 at 21:22
  • @daniel-katz I'm interested in this topic since I'm building an app with Qt and OpenCV, and dynamic linking is necessary if one can't pay Qt commercial fees. Can we get in contact? – DanyAlejandro Aug 03 '13 at 21:38
  • @DanyAlejandro I'm sorry but I myself am not able to link anything dynamically, I needed at least to make it work to do a homework. Though I'd be still very glad if I knew how to do it. – Daniel Katz Aug 13 '13 at 12:38
  • @DanielKatz, thank you so much for being helpful and informative. It worked with me as a charm. – CroCo Mar 30 '14 at 02:52
  • @DanielKatz, check out this link. It's the answer for your question. http://stackoverflow.com/questions/12123479/setting-up-opencv-2-4-2-with-qt-creator – CroCo Mar 30 '14 at 02:56
  • 1
    this worked for me although i was specifiying seperate *.dll files which was not working with QT5.13 , but was working in QT5.12 – user889030 Sep 18 '19 at 17:10
2

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.

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

  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

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!
Sailanarmo
  • 1,139
  • 15
  • 41