Using OpenCV within QTcreator and the Mingw compiler in Windows

OpenCV loves QTCreator

In the following steps, I’m going to show you how to compile OpenCV in Windows using CMake and MinGW and then setting up a Project in QTcreator to use OpenCV.

  1. Navigate to the OpenCV website and download the latest binaries/source for Windows, and extract them somewhere for ex. C:yourOpencvDirectory
  2. NOTE:  In your extracted directory, you should have two sub-directories called “build” and “sources”:Opencv extracted directory
  3. Navigate to the CMake website and download the latest CMake Windows Installer and install CMake (when asked, I chose to add CMake to the path for all users).
  4. Launch CMake GUI, here’s what the GUI should look like:CMake GUI
  5. In the GUI, pick the source directory and the directory where your compiled OpenCV build will be installed, and the hit the “Configure” button:CMake GUI picking directories
  6. After clicking the “Configure” button, you’re presented with the following window, where you have to pick which Makefiles to generate.  In my case I picked the MinGW Makefiles:CMake pick the compiler
  7. Then, in the next window you have to find the compilers on your computer, for example on my computer I specified where the MinGW compilers for both C and C++ are:CMake find the compilers
  8. After you’ve picked your compilers, you might get errors, that’s ok, just hit the “Configure” button one more time, and CMake will start configuring the build:CMake configure process
  9. Once the configure process is done, you’ll be presented with choices for your build, I left everything as is, and then when you’re ready to build, you click the “Generate” button:CMake ready to hit Generate
  10. We’re almost there.  CMake should have generated the Makefiles.  Now open up a command prompt and go to your opencv build directory, and simply type the command “mingw32-make” (without the quotes) and hit enter:CMake about to build by typing make
  11. Now, sit back and relax, because this is going to take a while (look mine is at only 2%, man this is not going to be quick):CMake just started the build process
  12. It’s DONE!!!

After it’s done, we’re now ready to use the libraries in our projects, let’s see what we need to do to use it in QTcreator.  In your QTcreator project, in the “.pro” file, we’re going to add the path to the include directory, as well as link to the opencv libraries.  The process is the same for whichever version of Opencv you have just built.  The following snippet shows the lines to add I added to my project’s “.pro” file, you can see I decided to add every library from opencv:


INCLUDEPATH += C:/opencv/build/include

LIBS += C:/opencv248/build/x86/mingw/lib/libopencv_calib3d248.dll.a
C:/opencv248/build/x86/mingw/lib/libopencv_contrib248.dll.a
C:/opencv248/build/x86/mingw/lib/libopencv_core248.dll.a
C:/opencv248/build/x86/mingw/lib/libopencv_features2d248.dll.a
C:/opencv248/build/x86/mingw/lib/libopencv_flann248.dll.a
C:/opencv248/build/x86/mingw/lib/libopencv_gpu248.dll.a
C:/opencv248/build/x86/mingw/lib/libopencv_highgui248.dll.a
C:/opencv248/build/x86/mingw/lib/libopencv_imgproc248.dll.a
C:/opencv248/build/x86/mingw/lib/libopencv_legacy248.dll.a
C:/opencv248/build/x86/mingw/lib/libopencv_ml248.dll.a
C:/opencv248/build/x86/mingw/lib/libopencv_nonfree248.dll.a
C:/opencv248/build/x86/mingw/lib/libopencv_objdetect248.dll.a
C:/opencv248/build/x86/mingw/lib/libopencv_ocl248.dll.a
C:/opencv248/build/x86/mingw/lib/libopencv_photo248.dll.a
C:/opencv248/build/x86/mingw/lib/libopencv_stitching248.dll.a
C:/opencv248/build/x86/mingw/lib/libopencv_superres248.dll.a
C:/opencv248/build/x86/mingw/lib/libopencv_ts248.a
C:/opencv248/build/x86/mingw/lib/libopencv_video248.dll.a
C:/opencv248/build/x86/mingw/lib/libopencv_videostab248.dll.a

Don’t forget to check out my blImageAPI library that makes working with opencv much more intuitive.  (By the way, I’m about to release a new version of the blImageAPI library any day now, so check back, I’ll put it up on github)

GOOD LUCK and have fun programming!!!

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"

12 Comments

  1. Pingback: Using OpenCV 2.3 with QTCreator in Windows | BarbatoLabs

  2. Pingback: Using OpenCV 2.2 within QTCreator in Windows | BarbatoLabs

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

  4. Hi! Thank you for such a full explanation. It’s already the third day when I’m trying to start to use OpenCV. I’ll really appreciate if you help me to find the problem.
    Using your prescriptions everything was ok until step 10 “mingw32-make”. I’m not sure at what percentage it stopped but it took not more than 5-7 minutes. Now it shows lots of text and last lines are “makefile:159: recipe for target “all” failed
    mingw32-make: ***[all] Error 2″
    How can it be solved?
    I’m installing OpenCV 3.1.0. After all these manipulations I don’t have folder “build” in C:/opencv248/ so if I compile it QT says for every Libs “no such file or derictory”.
    So far in my project I included:
    “#include
    #include ”
    and use:
    “cv::Mat inputImage = cv::imread(“queen.png”);
    cv::imshow(“Display image”,inputImage);”
    When I remove the “include” lines it says “imread is not a cv member” as well for “imshow”.
    Thank you in advance!

  5. Renata, no worries.

    But the problem will still come up when you try compiling opencv 3.0 and up with mingw on windows.

    With Opencv 3.0 and up I started getting the same errors. In my case, during the step where you can choose the packages to compile, I had to turn off (uncheck) the WITH_IPP option and then it all worked fine

  6. I still had problems with 3.0 so installed 3.1.0. Now I can see libs there but still can’t run it. It always shows “undefined reference to ‘cv::imread(cv::String const&, int)'”
    In .pro I have:
    “INCLUDEPATH += C:/opencv/build/include

    LIBS += C:/opencv310/lib/libopencv_calib3d310.dll.a \
    C:/opencv310/lib/libopencv_core310.dll.a \
    C:/opencv310/lib/libopencv_features2d310.dll.a \
    C:/opencv310/lib/libopencv_flann310.dll.a \
    C:/opencv310/lib/libopencv_highgui310.dll.a \
    C:/opencv310/lib/libopencv_imgproc310.dll.a \
    C:/opencv310/lib/libopencv_ml310.dll.a \
    C:/opencv310/lib/libopencv_objdetect310.dll.a \
    C:/opencv310/lib/libopencv_photo310.dll.a \
    C:/opencv310/lib/libopencv_stitching310.dll.a \
    C:/opencv310/lib/libopencv_superres310.dll.a \
    C:/opencv310/lib/libopencv_ts310.a \
    C:/opencv310/lib/libopencv_video310.dll.a \
    C:/opencv310/lib/libopencv_videostab310.dll.a”

    And code I want to execute:

    “#include “mainwindow.h”
    #include “ui_mainwindow.h”
    #include
    #include
    #include

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    cv::Mat inputImage = cv::imread(“C:\\queen.png”);
    if(!inputImage.empty()) cv::imshow(“Display Image”, inputImage);
    }

    Looking forward for your reply! 🙁

  7. After mixing all ways of defining libs I’ve finally found the working one 😀 So if smb stuck with the same problems: I use QT 5.0.0 and MinGW as a compiler. OpenCV of version 3.1.0.
    To definde the path in .pro: “INCLUDEPATH += C:/opencv/build/include”
    To define libs in .pro: “LIBS += -LC:/opencv310/lib \
    -llibopencv_calib3d310 \
    -llibopencv_core310 \
    -llibopencv_features2d310 \
    -llibopencv_flann310 \
    -llibopencv_highgui310 \
    -llibopencv_imgproc310 \
    -llibopencv_ml310 \
    -llibopencv_objdetect310 \
    -llibopencv_photo310 \
    -llibopencv_stitching310 \
    -llibopencv_superres310 \
    -llibopencv_video310 \
    -llibopencv_videoio310 \
    -llibopencv_videostab310 \
    -llibopencv_shape310 \
    -llibopencv_imgcodecs310”
    After that I can the image 🙂

  8. Hello Enzo
    Thank you very much for a detailed explanation of installation.
    I have a simple question, if Opencv comes with MSVC builds and if we gonna use the QT Creator with VC14 or VC15 compiler, do we still need to make CMAKE step in order to access OpenCV libraries within QT Creator ? More to explain; we don’t want to use MinGW compilers within QT but only VC15 then how we will integrate OpenCV libraries to QT Creator?

    Thank you

Leave a Reply

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