Qt – setting up OpenCV 2.4.2 with Qt Creator

opencvqtqt-creatorvisual studio 2010

I tried two methods to use opencv with qt creator
first one using Mingw where the dlls and .dll.a files are already downloaded with the opencv library and I just add reference to the .dll.a files in the .pro file as follow

INCLUDEPATH += D:\\OpenCV\\opencv\\build\\include
LIBS += D:\\OpenCV\\opencv\\build\\x64\\mingw\\lib\\libopencv_calib3d242.dll.a
LIBS += D:\\OpenCV\\opencv\\build\\x64\\mingw\\lib\\libopencv_contrib242.dll.a
LIBS += D:\\OpenCV\\opencv\\build\\x64\\mingw\\lib\\libopencv_core242.dll.a
LIBS += D:\\OpenCV\\opencv\\build\\x64\\mingw\\lib\\libopencv_features2d242.dll.a

I have a simple code to test opencv:

#include <QtCore/QCoreApplication>
#include <opencv/cv.h>

using namespace cv;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Mat image;

    return a.exec();
}

but I got a build issues as follow

C:\Users\Kato\Documents\QT projects\QtOpenCVYaRab\debug\main.o:-1: In function ~Mat':
d:\OpenCV\opencv\build\include\opencv2\core\mat.hpp:278: error: undefined reference to
cv::fastFree(void*)'
d:\OpenCV\opencv\build\include\opencv2\core\mat.hpp:367: error: undefined reference to `cv::Mat::deallocate()'
:-1: error: collect2: ld returned 1 exit status

Here is some of the compile output:

Running build steps for project QtOpenCVYaRab...
Configuration unchanged, skipping qmake step.
Starting: "C:\QtSDK\mingw\bin\mingw32-make.exe" 
C:/QtSDK/mingw/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory `C:/Users/Kato/Documents/QT projects/QtOpenCVYaRab'
g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL

 d:/OpenCV/opencv/build/include/opencv2/core/mat.hpp:278: undefined reference to `cv::fastFree(void*)'
debug/main.o:d:/OpenCV/opencv/build/include/opencv2/core/mat.hpp:367: undefined reference to `cv::Mat::deallocate()'
collect2: ld returned 1 exit status
mingw32-make[1]: *** [debug\QtOpenCVYaRab.exe] Error 1
mingw32-make: *** [debug] Error 2
The process "C:\QtSDK\mingw\bin\mingw32-make.exe" exited with code 2.
Error while building project QtOpenCVYaRab (target: Desktop)
When executing build step 'Make'

the second method is using cmake to compile the opencv library the using visual studio 2010 to build it and add references to the files in the bin folder but I got almost the same building issues.

Best Answer

#include "iostream"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

int main()
{
    IplImage *image = cvLoadImage("C:\\lena.jpg");
    Mat im(image);
    imshow("TEST",im);
    waitKey();
    return 0;
}

this is ur main.cpp...the above programme displays the picture of lena...use double backslashes for indicating change of directory on windows platform...some how the imread does work for me so i have loaded the image as IplImage and casted it to Mat...u cn do the following also..

 IplImage *image = cvLoadImage("C:\\lena.jpg",1);
 cvShowImage("TEST",image);
 cvWaitKey();

your .pro file should have the following lines as mentioned earlier...

INCLUDEPATH += D:\OpenCV\opencv\build\include

LIBS +=-LD:\OpenCV\opencv\build\x64\mingw\lib\
-lopencv_core242\
-lopencv_highgui242\
-lopencv_imgproc242\
-lopencv_video242\

and your system variable named path should have

  1. D:\Opencv2.4.2\opencv\build\x86\vc9\bin (if you have Qt 4.8.1 for desktop MSVC2008 (QtSDK) Debug as your target)
  2. D:\Opencv2.4.2\opencv\build\x86\mingw\bin (if your target is based on Qt MinGW x86 )
  3. D:\OpenCV2.4.2\opencv\build\common\tbb\ia32\vc9(mingw) (i have added this coz it was showing some weird errors...u can try it)

after editing the path variable close the Qt ide/application and restart it for the system variable change to get reflected..

Related Topic