Trang chủ‎ > ‎IT‎ > ‎Programming‎ > ‎GUI Programming‎ > ‎

Install OpenCV 2.4.10 and use it in MSVC 2013 and Qt 5.4.0 of Windows x64

Welcome to a veryyyy long tutorial of mine (again).

Installing OpenCV in a brand new computer has never been straightforward for me in a past decade. I just got this new laptop which happens to be my first 64-bit Windows OS computer. And as expected, setting up OpenCV and Qt on 64-bit Windows OS took me many frustating hours.

Some problems seemed to be very big deals that were about to take me forever to work them out. But somehow I was able to get them right finally. Some solutions are incredibly simple but totally unexpected; something I’ve never imagined that it can cause me these problems.

As everything is working now, I want to again share this step-by-step experience to you and also to my students, who hopelessly gave up half way as they could not make OpenCV in Qt (or even Qt only) work under Windows 64-bit environment.

My laptop:: Windows 8.1 Enterprise N, 64-bit OS, x64-based processor

:::::::::::: STEP 1: Install Microsoft Visual C++ 2013 :::::::::::::

1 ) Install Microsoft Visual Studio Ultimate 2013 to C:\Program Files (x86)\Microsoft Visual Studio 12.0 .

2 ) Make sure that your copy of MSVC 2013 is running ok by creating a new empty Win32 console application and executing some simple C++ codes. Try both debug and release modes, as well as try debugging the codes using MSVC’s debugger.

#include <stdio.h>
int main()
{
	for (int i = 0; i < 100000; i++)
		printf("%d. Hello world\n", i);
	return 0;
}

3 ) Once your MSVC2013 is working fine for none-OpenCV C++ programming, you are ready for the next step.

:::::::::::: STEP 2: Install OpenCV 2.4.10 for Windows ::::::::::::

1 ) Download a free copy of prebuilt OpenCV 2.4.10 for Windows (367.3 MB) from http://opencv.org/downloads.html.

2 ) In my laptop, I extracted the file to C:\opencv2410 as shown below.

dec2014_pic1

3 ) Add OpenCV’s bin directory (in where *.dll files are) to PATH environment variable. This is an unexpectedly tricky step that caused me many troubles in later OpenCV’s as well as Qt’s settings.

Mainly, there are 3 ways that I tried; each gave me different results. (Alternative 3 is the one that finally works for me)

  • Alternative 1: Go to Environment Variables > System variables: Path > click Edit. Then append C:\opencv2410\build\x86\vc12\bin; to the end of the list (don’t forget using ; to separate each entry).
      
    dec2014_pic2

    In my laptop, adding OpenCV’s bin directory to the end of PATH did not work. Somehow, MSVC2013 kept showing me an error saying that opencv_XXXX.dll is missing from your computer, despite of my hopeless attempt to restart the computer.

  • Alternative 2: This is almost the same as Alternative 1. But instead of appending C:\opencv2410\build\x86\vc12\bin; to the end of PATH’s list, I inserted it at the very beginning. This way, OpenCV’s bin directory is the first entry found in system’s PATH variable. I know that this solution sounds weirdly unreasonable. But it has worked like a magic for my past decade of using OpenCV in MSVC environment.
        
    So if your plan is to develop OpenCV in MSVC, this alternative may be the one for you. However, using this alternative led me to another hopeless problem in Qt Creator (more detail in STEP 4.5.1). So I had no choice but to move to Alternative 3.
  • Alternative 3: Instead of adding OpenCV’s bin directory to system’s PATH variable, I added it to user profile’s PATH variable, hoping that this could help avoid conflict with Qt Creator (see detail in STEP 4.5.1). And, HOORAYYYY, it worked, for both OpenCV in MSVC 2013 and OpenCV in Qt 5.4.0!!! Below is a screenshot of how I did this:
      
    dec2014_pic3

    Above picture shows a small and convenient program that helps me to easily set Windows’ environment variables. It is called PathEditor.exe downloaded for free from http://patheditor.codeplex.com/. BTW, you do not need to use this program; editing PATH using a traditional Windows style gives the same result.

  
  
4 ) Restart the computer for the change in 3 to take effect (sometimes you may need to restart your computer more than once).

::::::::::: STEP 3: Use OpenCV 2.4.10 in MSVC 2013 :::::::::::

If you have no plan to develop an OpenCV program under MSVC environment, you may skip this step and go to STEP 4 of setting up Qt right away. But I personally prefer ensuring that every prerequisite tool/library is working just fine before going to the next one.

1 ) Open Microsoft Visual Studio 2013 and create a new VC++ project (an empty Win32 console application).

2 ) Open Property Manager window by choosing VIEW > Other Windows > Property Manager menu.

dec2014_pic4

Below is a screenshot of how the Property Manager look like.

dec2014_pic5

3 ) In Property Manager window, create a property sheet for running OpenCV in Debug mode by right clicking at Debug | Win32 and choose Add New Project Property Sheet. For me, I named the new property sheet OPENCV_DEBUG.props .

4 ) Right click at the newly created OPENCV_DEBUG property sheet and choose Properties menu.

dec2014_pic6

5 ) Add (do not delete existing values) the following values to the OPENCV_DEBUG property sheet:

  1. VC++ Directories
    • Executable Directories: C:\opencv2410\build\x86\vc12\bin
    • Library Directories: C:\opencv2410\build\x86\vc12\lib
  2. C/C++
    • Additional Include Directories: C:\opencv2410\build\include
  3. Linker > General
    • Additional Library Directories: C:\opencv2410\build\x86\vc12\lib
  4. Linker > Input
    • Additional Dependencies:
            opencv_core2410d.lib
            opencv_highgui2410d.lib
            opencv_imgproc2410d.lib
            opencv_video2410d.lib

      You may add more libraries here, depending on OpenCV’s commands being used.

  
6 ) Back to Property Manager window, now create a property sheet for running OpenCV in Release mode by right clicking at Release | Win32 and choose Add New Project Property Sheet; I named this property sheet OPENCV_RELEASE.props .

dec2014_pic7

7 ) Add (do not delete existing values) the following values to the OPENCV_RELEASE property sheet. They are almost the same as done for OPENCV_DEBUG setting, except for library’s names in Additional Dependencies.

  1. VC++ Directories
    • Executable Directories: C:\opencv2410\build\x86\vc12\bin
    • Library Directories: C:\opencv2410\build\x86\vc12\lib
  2. C/C++
    • Additional Include Directories: C:\opencv2410\build\include
  3. Linker > General
    • Additional Library Directories: C:\opencv2410\build\x86\vc12\lib
  4. Linker > Input
    • Additional Dependencies:
            opencv_core2410.lib
            opencv_highgui2410.lib
            opencv_imgproc2410.lib
            opencv_video2410.lib

      You may add more libraries, depending on OpenCV’s commands being used.

  
8 ) Add a new main.cpp file to your project. Then put some OpenCV codes in that file.

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

int main()
{
  // Load an existing image (make sure that the input URL really exists in your computer)
  cv::Mat pic = cv::imread( "C:\\somepic.jpg", CV_LOAD_IMAGE_COLOR );  

  cv::VideoCapture capture(0);       // open a first USB camera found (0-based camera index)
  if (!capture.isOpened())           // if camera not found, display an error message
     printf("Error: cannot open a camera\n");

  cv::Mat cam;
  while (true)
  {
     if ( capture.isOpened() )
 	capture >> cam;                   // read a next frame from camera live stream

     cv::imshow("Still image", pic);      // display the still image on a window

     if ( !cam.empty() )
	cv::imshow("Camera image", cam);  // display live camera stream on another window

     if (cv::waitKey(1) == 27)            // exit this loop when ESC was pressed
	break;
  }

  return 0;
}

9 ) A moment of truth, try running your project in both Debug and Release modes. If success (using my sample codes above), there will be two windows displayed: one showing a still image and the other showing live camera stream.

List of frequently found errors
  1. The program can’t start because opencv_XXXX.dll is missing from your computer
    • Cause: Somehow MSVC cannot find where your OpenCV’s dll files are.
    • Solution: (check it step-by-step from top to bottom)
      1. Make sure that C:\opencv2410\build\x86\vc12\bin; is a valid directory in your computer and that the directory was properly added to Windows’ environment variable (see STEP 2.3 for more tricky detail).
      2. Make sure that the opencv_XXXX.dll file really exists in your computer at the directory mentioned above. If the directory is valid but the dll file is not found inside, you may need to reinstall your OpenCV.
      3. After all, if the problem still remains, my last suggestion is to copy all necessary OpenCV’s dll files to the same directory where your *.exe (built by MSVC) is running in.
  2. fatal error C____: cannot open include file: ‘opencvXXXX.hpp': no such file or directory or error C____ ‘xxxx’ is not a member of ‘cv’ or error C____ ‘xxxx’ undeclared identifier or error C____ ‘xxxx’ identifier not found
    • Cause: Some necessary header files are missing.
    • Solution: (check it step-by-step from top to bottom)
      1. Make sure that you set the correct and valid directory of OpenCV’s include folder during STEP 3.5.2 and STEP 3.7.2.
      2. Check OpenCV’s documentation for a name of header file required by the OpenCV’s command causing this error. Then, ensure that you have already added that header to the top of your code using #include <opencv2/xxxx/xxxx.hpp> .
      3. After all, if the problem still remains, browse your OpenCV’s include folder (C:\opencv2410\build\include for my laptop) to ensure that the header file really exists. Otherwise, you may need to reinstall OpenCV to recover it.
  3. error LNK____: unresolved external symbol
    • Cause: Some library required by your used OpenCV’s command is missing.
    • Solution: (check it step-by-step from top to bottom)
      1. Make sure that you set the correct and valid directory of OpenCV’s library folder during STEP 3.5.1 and STEP 3.7.1.
      2. Check OpenCV’s documentation for a name of library required by the OpenCV’s command causing this error. Then, ensure that you have already added that library to your project (following STEP 3.5.4 and 3.7.4).
      3. After all, if the problem still remains, browse your OpenCV’s library folder (C:\opencv2410\build\x86\vc12\lib for my laptop) to ensure that the library really exists. Otherwise, you may need to reinstall OpenCV to recover it.

  
:::::::::::: STEP 4: Install Qt 5.4.0 for Windows ::::::::::::

If you want something to help create a beautiful GUI in C++, something that is easy to use, something that can be shared among different OSs or platforms, my suggestion is to put QT at the very top of your list. Although I started using QT just a few months ago, I have to say that I totally love it. Of course, it causes me more works and troubles during installation, but what I’ve got is really worth.

No more introduction. Let’s start getting QT to work in your 64-bit Windows OS.

1 ) Download free Qt 5.4.0 for Windows 32-bit (VS 2013, OpenGL, 694 MB) from http://www.qt.io/download-open-source/.

Note: At first, I downloaded Qt 5.4.0 for Windows 64-bit (VS 2013, OpenGL) but it did not work (more detail in STEP 4.5.2).

2 ) Install Qt Creator 5.4.0 at C:\Qt\Qt5.4.0 and launch it as shown below.

dec2014_pic8

3 ) Create a new project of type Qt Console Application. The 32-bit MSVC2013 Debug and Release compilers should be detected automatically in Kit Selection as shown below. Otherwise, you have to manually specify them later.

dec2014_pic9

4 ) In *.cpp file, write simple C++ codes (like those in STEP 1.2) to test whether Qt is working fine for none-OpenCV C++ programming.

5 ) Run the project in both debug and release modes. Also try debugging it too to make sure that the debugger tool is working just fine.

List of frequently found errors
  1. ‘cl’ is not recognized as an internal or external command, operable program or batch file
    • Cause: Somehow the system cannot find C:\Windows\System32 in system’s PATH variable. In consequent, Qt cannot find where cl.exe (MSVC command line prompt which is required by QT) is.
    • Solution: Make sure that C:\Windows\System32 is in the system’s PATH variable list and that it appears as “the first entry of the list.” The latter part of problem corresponds to the tricky part that I mentioned earlier in STEP 2.3.
  2. LNK1112 module machine type ‘x86’ conflicts with target machine type ‘x64’
    • Cause: Mismatch between version of MSVC compiler and Qt Creator.
    • Solution: In my laptop, uninstalling Qt 5.4.0 for Windows 64-bit (VS 2013, OpenGL) and reinstalling Qt 5.4.0 32-bit (VS 2013, OpenGL) solved this problem.
  3. No debugger setup
    • Cause: Debugging Tools for Windows are missing. (These tools are not included in MSVS 2013 installation)
    • Solution: (step-by-step)
      1. Download Standalone Debugging Tools for Windows (WinDbg) installer from http://msdn.microsoft.com/en-us/windows/hardware/hh852365.
      2. Install it (you may choose to install only the debugger and uncheck the others to reduce downloading time).
      3. Relaunch Qt Creator, and choose Tools > Option > Debuggers menu where you will see cdb.exe debugger automatically detected in C:\Program Files (x86)\Windows Kits\8.1\Debuggers directory.

  
:::::::::::: STEP 5: Use OpenCV 2.4.10 in Qt 5.4.0 ::::::::::::

Finally, we are about to go for the last step.

1 ) Create a new project or edit the project created in STEP 4 so that the *.cpp file contains OpenCV test codes (my example codes in STEP 3.8).

2 ) Append the following codes to the bottom of yourProjectName.pro file.

win32 {
    INCLUDEPATH += "C:\\opencv2410\\build\\include" \

    CONFIG(debug,debug|release) {
        LIBS += -L"C:\\opencv2410\\build\\x86\\vc12\\lib" \
            -lopencv_core2410d \
            -lopencv_highgui2410d \
            -lopencv_imgproc2410d \
            -lopencv_features2d2410d \
            -lopencv_calib3d2410d
    }

    CONFIG(release,debug|release) {
        DEFINES += QT_NO_WARNING_OUTPUT QT_NO_DEBUG_OUTPUT
        LIBS += -L"C:\\opencv2410\\build\\x86\\vc12\\lib" \
            -lopencv_core2410 \
            -lopencv_highgui2410 \
            -lopencv_imgproc2410 \
            -lopencv_features2d2410 \
            -lopencv_calib3d2410
    }
}

In future use, do not forget to include additional libraries required by your project here using the above format.

3 ) Choose Build > Run qmake menu. You should do this every time there are changes made to *.pro file.

4 ) Run the project in both debug and release modes. Also try debugging it too to make sure that the debugger tool is working just fine.

List of frequently found errors

Mostly they are the same as those errors explained at the end of STEP 3 and 4.

But in Qt, setting OpenCV’s include directory, OpenCV’s library directory and dependency libraries is easier (less number of clicks) and more flexible than MSVC, just specifying them in *.pro file following the format shown in STEP 5.2.

 

Comments