Using OpenCV 2.2 within QTCreator in Windows

OpenCV loves QTCreator

UPDATE:  If you’re trying to compile opencv look at this article.

This is an update to my “Using OpenCV within QTCreator in Windows” post, that will show you how to quickly setup OpenCV 2.2 for QTCreator in Windows. (This of course will work for any IDE, not just QTCreator).

So, let’s get down to the details quickly:

  1. Download both OpenCV and QTCreator, which can be found by clicking on the links.
  2. Install both OpenCV and QTCreator, specifying installation directories and so forth. (I suggest to just let the automatic installation do its thing, and jog down the directories where OpenCV was installed).
    1. Make sure to let OpenCV create a path for the shared libraries, otherwise we’ll have to do some extra work.
    2. Also, make sure that if you choose a different path, that there are no spaces in the path name (Trust me on this one)
  3. Restart the computer. (If you let OpenCV create the path).
  4. Now let’s have a look at the directory where you installed OpenCV
    1. There are several sub-directories.  Let’s have a look at the include directory:
      1. There are two sub-directories:
        1. opencv
        2. opencv2
      2. Inside opencv2 sub-directory, there is a file called “opencv.hpp”
        1. Cut that file and copy it into the parent “include” directory
          1. In my case I cut it from “C://OpenCV2.2/include/opencv2” and pasted it into “C://OpenCV2.2/include”
  5. Run QTCreator
  6. Start a New Project (File->New File or Project… Then pick a QT c++ project, could be QT application or Console application, then follow the instructions to create the new project)
  7. Now that you have a new project, click on the “Edit” button on the left pane of QTCreator as shown below:
  8. Once in the edit pane, you will see a list of files.
    1. Double click on the project file to open it (the file with the “.pro” file extension).
  9. In the project file we just need to add the include paths to the Opencv2.2 include files, and the lib paths.
    1. All we have left to do, is to add the INCLUDEPATH and LIBS lines to your pro file, like the ones shown below, but remember to use the actual directory where you installed OpenCV.  (Backward slashes are used to add multiple lines)
    2. INCLUDEPATH += C:\OpenCV2.2\include
      
      CONFIG(release,debug|release){
          LIBS += C:\OpenCV2.2\lib\opencv_calib3d220.lib \
                  C:\OpenCV2.2\lib\opencv_contrib220.lib \
                  C:\OpenCV2.2\lib\opencv_core220.lib \
                  C:\OpenCV2.2\lib\opencv_features2d220.lib \
                  C:\OpenCV2.2\lib\opencv_ffmpeg220.lib \
                  C:\OpenCV2.2\lib\opencv_flann220.lib \
                  C:\OpenCV2.2\lib\opencv_gpu220.lib \
                  C:\OpenCV2.2\lib\opencv_highgui220.lib \
                  C:\OpenCV2.2\lib\opencv_imgproc220.lib \
                  C:\OpenCV2.2\lib\opencv_legacy220.lib \
                  C:\OpenCV2.2\lib\opencv_ml220.lib \
                  C:\OpenCV2.2\lib\opencv_objdetect220.lib \
                  C:\OpenCV2.2\lib\opencv_ts220.lib \
                  C:\OpenCV2.2\lib\opencv_video220.lib \
      }
      
      CONFIG(debug,debug|release){
          LIBS += C:\OpenCV2.2\lib\opencv_calib3d220d.lib \
                  C:\OpenCV2.2\lib\opencv_contrib220d.lib \
                  C:\OpenCV2.2\lib\opencv_core220d.lib \
                  C:\OpenCV2.2\lib\opencv_features2d220d.lib \
                  C:\OpenCV2.2\lib\opencv_ffmpeg220d.lib \
                  C:\OpenCV2.2\lib\opencv_flann220d.lib \
                  C:\OpenCV2.2\lib\opencv_gpu220d.lib \
                  C:\OpenCV2.2\lib\opencv_highgui220d.lib \
                  C:\OpenCV2.2\lib\opencv_imgproc220d.lib \
                  C:\OpenCV2.2\lib\opencv_legacy220d.lib \
                  C:\OpenCV2.2\lib\opencv_ml220d.lib \
                  C:\OpenCV2.2\lib\opencv_objdetect220d.lib \
                  C:\OpenCV2.2\lib\opencv_ts220.lib \
                  C:\OpenCV2.2\lib\opencv_video220d.lib \
      }
      
  10. Now the last thing is, in your file that will use the opencv functions include the opencv by putting the following statement in your code:
    #include <opencv .hpp>
    

Note: If you have a chance, check out the BarbatoLabs Image API, blImageAPI. It’s a very simple but powerful API, which is based on opencv’s IplImage and boost::shared_ptr, and gives you automatic garbage collection and memory allocation/deallocation and other good stuff, while being 100% compatible with opencv’s existing algorithms, it can be used instead of IplImage or interchangeably with it.

Troubleshooting List, to be built as I get feedback:

  1. Error —  “exited with code – 1073741515” (or something along those lines)
    1. You probably missed Step 3. — Restart the computer after installation of OpenCV, (a quick restart should resolve the problem).
    2. If you still get the error after restarting, then copy the OpenCV dlls from the OpenCV2.2/bin directory to the Windows system32 directory if in 32bit version of windows or to the Windows SYSWOW64 directory if in 64bit version of windows
    3. If you still get the error after the above steps, then you’re missing some Microsoft dlls.  It turns out that the “debug” version of opencv has some dependencies on Microsoft dlls.  The “release” version is good to go out of the box.
      1. [SOLVED] — It turns out that the Microsoft dlls are downloaded as part of the visual c++ express package.  So as a solution, download and install the bare-bones visual c++ express and the “debug” version of opencv will work.  (You don’t have to use visual c++ express, you can still use QTcreator and the Mingw compiler, it will work, trust me).
  2. If you get errors such as “cannot find cv.h file” and something like that, look at the steps above, where I mentioned to cut and past the “opencv.hpp” file in the correct directory.
  3. Error — “underfined reference cv::VideoCapture” or any other opencv c++ function like cv::mat and others
    1. After some googling, the consensus is that to use the c++ features of opencv with Mingw, opencv has to have been compiled using g++ instead of gcc. Follow the instructions at this link to compile opencv c++ for QTcreator in windows.
    2. Note:  If you want c++ like ease and usability with the opencv c functions, while maintaining full compatibility with the c functions, then use the Barbato Labs Image library.

About Vincenzo Barbato

Known to his friends as Enzo, he's an outside-the-box engineer/researcher whose interests and expertise span many fields, including controls systems, multi-physics simulations, mechatronics, oil technologies, data analysis and machine vision just to name a few.

Refusing to grow up, he's on a continuous journey to develop simple and creative solutions that have the power of disrupting industries by optimizing systems and processes.

Married to a beautiful wife, with two beautiful daughters and two identical twin boys, his home is a never ending chaotic fountain of inspiration.

His outlook on life:

"Never blindly accept what you're told, listen, but then question, with curiosity, creativity and collaboration we can change the world"

About Enzo

Known to his friends as Enzo, he's an outside-the-box engineer/researcher whose interests and expertise span many fields, including controls systems, multi-physics simulations, mechatronics, oil technologies, data analysis and machine vision just to name a few. Refusing to grow up, he's on a continuous journey to develop simple and creative solutions that have the power of disrupting industries by optimizing systems and processes. Married to a beautiful wife, with two beautiful daughters and two identical twin boys, his home is a never ending chaotic fountain of inspiration. His outlook on life: "Never blindly accept what you're told, listen, but then question, with curiosity, creativity and collaboration we can change the world"

25 Comments

  1. Just to point out, the installer aviable in opencv site is built with/for vs2010, so there might be undefined references problems using them on qt (which uses MinGW).

    • Valrack, I have not run into any problems using the mingw compiler.

      • Well, Im currently using win7 x64. Using the opencv installable gave me undefined reference error in some functions, so I decided to download cmake and mingw to manually compile opencv. That allowed me to compile opencv apps correctly, but everytime it ran, it inmediately exited with code -1073741511, so there was still an error with the dll’s.
        At last, I uninstalled the mingw version I downloaded, added qt’s mingw (C:\Qt\2010.05\mingw\bin) as PATH in the environment variables and recompiled opencv source, and added the bin folder as environemt variable aswell. Doing that the apps ran flawless.
        tl;dr If youre getting errors and you have no idea why, recompile opencv with the same compiler that your ide/framework uses, because ive noticed that even if the libraries were compiled with the same compiler but a different version, it might not work at all.

  2. Hey i have followed all your instructions mentioned over here. Still I am getting these \”undefined reference to cv::VideoCapture functionname()\” error. I am not getting whats the problem? can you elaborate something on this?

  3. I am using openCV2.2 for camera capturing and display functionality.
    I have installed openCV2.2 in C:\openCV2.2 and in .pro file I have mentioned
    INCLUDEPATH += “C:\\OpenCV2.1\\bin”
    INCLUDEPATH += “C:\\OpenCV2.1\\include”

    CONFIG(release,debug|release){
    LIBS += C:\\OpenCV2.2\\lib\\opencv_calib3d220.lib \

    C:\\OpenCV2.2\\lib\\opencv_contrib220.lib \

    C:\\OpenCV2.2\\lib\\opencv_core220.lib \

    C:\\OpenCV2.2\\lib\\opencv_features2d220.lib \

    C:\\OpenCV2.2\\lib\\opencv_ffmpeg220.lib \

    C:\\OpenCV2.2\\lib\\opencv_flann220.lib \

    C:\\OpenCV2.2\\lib\\opencv_gpu220.lib \

    C:\\OpenCV2.2\\lib\\opencv_highgui220.lib \

    C:\\OpenCV2.2\\lib\\opencv_imgproc220.lib \

    C:\\OpenCV2.2\\lib\\opencv_legacy220.lib \

    C:\\OpenCV2.2\\lib\\opencv_ml220.lib \

    C:\\OpenCV2.2\\lib\\opencv_objdetect220.lib \

    C:\\OpenCV2.2\\lib\\opencv_ts220.lib \

    C:\\OpenCV2.2\\lib\\opencv_video220.lib \

    }

    CONFIG(debug,debug|release){

    LIBS += C:\\OpenCV2.2\\lib\\opencv_calib3d220d.lib \

    C:\\OpenCV2.2\\lib\\opencv_contrib220d.lib \

    C:\\OpenCV2.2\\lib\\opencv_core220d.lib \

    C:\\OpenCV2.2\\lib\\opencv_features2d220d.lib \

    C:\\OpenCV2.2\\lib\\opencv_ffmpeg220d.lib \

    C:\\OpenCV2.2\\lib\\opencv_flann220d.lib \

    C:\\OpenCV2.2\\lib\\opencv_gpu220d.lib \

    C:\\OpenCV2.2\\lib\\opencv_highgui220d.lib \

    C:\\OpenCV2.2\\lib\\opencv_imgproc220d.lib \

    C:\\OpenCV2.2\\lib\\opencv_legacy220d.lib \

    C:\\OpenCV2.2\\lib\\opencv_ml220d.lib \

    C:\\OpenCV2.2\\lib\\opencv_objdetect220d.lib \

    C:\\OpenCV2.2\\lib\\opencv_ts220.lib \

    C:\\OpenCV2.2\\lib\\opencv_video220d.lib \

    and in my header file:
    #include <opencv.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/ml/ml.hpp>

     in my cpp file:
    VideoCapture videoCapture;
    if(!videoCapture.open(0))
    {
    qDebug()<<“camera not initialized”;
    capture = 0;
    return;
    }

    When I compile the program It gives me error that undefined reference to
    cv::VideoCapture open(int)

    I am not getting whats the problem.
    Please help

    • Sanket, your problem has a very easy fix.

      VideoCapture is defined in the cv namespace.

      From your code, I see that you’re missing the cv namespace. You say VideoCapture instead of cv::VideoCapture, or otherwise, you have to include the “using namespace cv;” line somewhere before your main function.

      You also only need to include the opencv.hpp file and nothing else.

      Make sure you copy and past the opencv.hpp file where I show to do it in the instructions.

      Hope that helps. Have fun programming.

    • Sanket, I just realized that all opencv c++ functions return an “unreferenced function” error. We’re obviously not including the source files or lib files needed for it. Let me figure it out and I’ll post an update

    • UPDATE: Just follow the instructions on this article and you’ll have C and C++ features of opencv with Mingw
      After some googling, the consensus is that to use the c++ features of opencv with Mingw, opencv has to have been compiled using g++ instead of gcc. Follow the instructions at this link to compile opencv c++ for opencv

  4. Hey Enzo, Thanks for your help. I have compiled Opencv with MinGW compiler and these errors disappeared. But now the problem is it does not show any video on my GUI which was visible previously. Do you think is there any problem with these functions in 2.2?

    • Sanket, no problem, i’m glad I could help.

      To tell you the truth, I don’t like the way the opencv team wrapped their library in c++. To me, it seems like an after thought, and the functions and structures are not compatible with their “c” counterparts. For this same reason I started the blImageAPI library. It is c++ and fully interchangeble with IplImage and all the “c” algorithms and structures from opencv. I will be releasing a lot more functionality in the next few days which i’m currently still testing.

  5. Hey man,

    I will be curiously waiting for it.

  6. Pingback: Using OpenCV within QTCreator in Windows | BarbatoLabs

  7. Thanks a lot for the article Mr.Enzo.. I’m working on my final project using Qt Creator and OpenCV2.2..My final project is to create a software for Video capturing via webcam, compress it, then send it through WLAN.. maybe you can give me some advice ? thank you

    • Dimas, i’m working on something similar. I’ll post an article as soon as I have something working. But the idea is simple, just connect to the webcam, encode the image into jpeg or png, and then send it using simple get/put requests.
      For the connect to the webcam part, that’s very simple, just follow one of my articles.
      For the encoding part, you can either use libjpeg, or use the functions cvEncodeImage and cvDecodeImage from OpenCV. I’m currently making my own functions for this.
      Finally, you can send images over the internet using http, for which there are literally thousands of libraries. I’m currently using the sfml library for that.

      • How is your work Mr.Enzo ? i’m curiously waiting for it..

        • Dimas, I have to pick and choose what to release as open source out of my work. Most of the times, I have to simplify things before I post them. So to make it easier, tell me what you’re interested in seeing.

          • A software that can do video capturing via webcam, has compressing function, and can send the captured frame through WLAN to an another part of the network.. . Or i want to know about webcam remote viewing between 2 computer, which the software is installed in one computer and the webcam is installed in one another.. is it possible ? thank you

            • Dimas, what you’re asking for is pretty easy. I have just added a couple of functions to my blImageAPI library to encode/decode compress/uncompress images in memory. Check them out. I’m currently adding a couple of classes for sending/receiving images over networks and internet.

  8. hello
    I wanted to compile an application developed with Qt and OpenCV and I can not. andsearching on google I found your forum and I hope you can help me because it is very urgent
    This is the beginning of fichier.pro
    p, li { white-space: pre-wrap; }
     
    TEMPLATE = app
    TARGET =
    DEPENDPATH += . debug include\opencv
    INCLUDEPATH += .
    # Input
    HEADERS += angledetectionpass.h \
    batchpasschain.h \
    binaryconversionpass.h \

    error:
    C:\exemple\src/angledetectionpass.cpp:62: erreur : undefined reference to `cvCreateImage’
    C:\exemple\src/angledetectionpass.cpp:63: erreur : undefined reference to `cvCopy’
    C:\exemple\src/angledetectionpass.cpp:92: erreur : undefined reference to `cvReleaseImage’
    C:\exemple\src/binaryconversionpass.cpp:62: erreur : undefined reference to `cvThreshold’
    C:\exemple\src/blurpass.cpp:77: erreur : undefined reference to `cvSmooth’

  9. I added it to my fichier.pro so now I have no compilation errors
    TEMPLATE = app
    TARGET =
    IINCLUDEPATH += C:\OpenCV2.2\include
    INCLUDEPATH += “C:\\OpenCV2.1\\bin”
    CONFIG(release,debug|release){
    LIBS += C:\OpenCV2.2\lib\opencv_calib3d220.lib \
    C:\OpenCV2.2\lib\opencv_contrib220.lib \
    C:\OpenCV2.2\lib\opencv_core220.lib \
    C:\OpenCV2.2\lib\opencv_features2d220.lib \
    C:\OpenCV2.2\lib\opencv_ffmpeg220.lib \
    C:\OpenCV2.2\lib\opencv_flann220.lib \
    C:\OpenCV2.2\lib\opencv_gpu220.lib \
    C:\OpenCV2.2\lib\opencv_highgui220.lib \
    C:\OpenCV2.2\lib\opencv_imgproc220.lib \
    C:\OpenCV2.2\lib\opencv_legacy220.lib \
    C:\OpenCV2.2\lib\opencv_ml220.lib \
    C:\OpenCV2.2\lib\opencv_objdetect220.lib \
    C:\OpenCV2.2\lib\opencv_ts220.lib \
    C:\OpenCV2.2\lib\opencv_video220.lib \
    C:\OpenCV2.2\lib\opencv_core220d.lib
    }
    CONFIG(debug,debug|release){
    LIBS += C:\OpenCV2.2\lib\opencv_calib3d220d.lib \
    C:\OpenCV2.2\lib\opencv_contrib220d.lib \
    C:\OpenCV2.2\lib\opencv_core220d.lib \
    C:\OpenCV2.2\lib\opencv_features2d220d.lib \
    C:\OpenCV2.2\lib\opencv_ffmpeg220d.lib \
    C:\OpenCV2.2\lib\opencv_flann220d.lib \
    C:\OpenCV2.2\lib\opencv_gpu220d.lib \
    C:\OpenCV2.2\lib\opencv_highgui220d.lib \
    C:\OpenCV2.2\lib\opencv_imgproc220d.lib \
    C:\OpenCV2.2\lib\opencv_legacy220d.lib \
    C:\OpenCV2.2\lib\opencv_ml220d.lib \
    C:\OpenCV2.2\lib\opencv_objdetect220d.lib \
    C:\OpenCV2.2\lib\opencv_ts220.lib \
    C:\OpenCV2.2\lib\opencv_video220d.lib \
    }
    # Input

    but when I run it gives me this message
    Démarrage de C:\exemple\src\debug\src.exe…

    C:\exemple\src\debug\src.exe s’est terminé avec le code -1073741515
    and when I execute the exe file it gives to this message

    Unable to start the program because it lacks opencv-adrenal core220d.dll yourcomputer

    • swito this simply means you’re missing a dll. make sure you restart after installing opencv or copy the dlls in the right place like I show in my post

  10. thank you for the response Mr.Enzo.. i have checked your bllmageAPI.. but then i have some problem.. i want to combine your lib with my project in Qt.. currently, my project is webcam viewer.. how can i implement your lib in my project ? i don’t mind to share my source code..thanks

    • i have tried to put blImageAPI library to my project just now.. when i debug it, a lots of error occured.. need your help, please.. thanks

Leave a Reply

Your email address will not be published. Required fields are marked *