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
- 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 - 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.
- Go to the terminal (~command prompt), cd to the directory where are the builds (opencv_bin) and type mingw32-make
- When the process ends after a long time, type mingw32-make install
- 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;
}