4
votes

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 tocv::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.

5
Please have a look at (OpenCV with other GUI)[stackoverflow.com/questions/2210937/…masad

5 Answers

5
votes
#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..

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

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


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

using namespace cv;

int main()
{
}

if u are running it as an console application then no need to include QtCore/QtApplication

0
votes

You have to specify your library path with -L and then add the library file with

-l<libname_without_extension>

For unix (installed in default place):

unix: LIBS += -lopencv_core
unix: LIBS += -lopencv_highgui

For windows (your problem):

win32: LIBS += -L C:\OpenCV2.3\opencv\build\gpu\x64\lib\ -lopencv_core231
0
votes

From the error mentioned, it seems that Opencv library is missing some files/components.Please start with the simple program for Opencv. This program just display the camera image.

Please follow the given link.

http://linux.softpedia.com/get/Multimedia/Graphics/qwebcam-38246.shtml

Download the source code for qwebcam and follow the instruction to setup Opencv. This is a very simple source code and works fine (tested on Linux-os).

I recently tried with Opencv & this link was quiet useful for me to begin with.

Hope you will be able to resolve your error through this code.

-1
votes

I ran into the same problem, but altering the .pro manually didn't work for me. Eventually I found a simple solution to connect the openCV to Qt. I posted about it a few other threads, https://stackoverflow.com/a/51914928/10245006 and have included the information below.

The steps listed below are found in the Qt5 documentation: [http://doc.qt.io/qtcreator/creator-project-qmake-libraries.html][1] 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!