Skip to content

Instantly share code, notes, and snippets.

@BartG95
Last active January 30, 2023 21:30
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save BartG95/1ce8ba1e9c25ec3698d1 to your computer and use it in GitHub Desktop.
Save BartG95/1ce8ba1e9c25ec3698d1 to your computer and use it in GitHub Desktop.
How to: build OpenCV x64 with Qt on Visual Studio 2015 Community Preview and Windows 10 Preview (build 10162)

Note: since I switched to GNU+Linux (a libre operating system, which I recommend), I don't maintain this tutorial anymore. Also, be aware that this tutorial is about preview software that is superseded by multiple stable releases. Consider all information below as outdated and possibly wrong.

How to: build OpenCV x64 with Qt on Visual Studio 2015 Community Preview and Windows 10 Preview (build 10162)

Introduction

As I'm an update addict, I like to try preview software. Or I'm an update addict because I like preview software. I don't know. Anyway, before my holiday started, I used OpenCV for a project in college. I like the library, because it's possible to achieve funny things with it and it makes computer vision easy. So when my holiday began, I started using OpenCV for some fun projects. And then I upgraded Windows 10. It wasn't stable back then, but I didn't need my pc for a while, so I could try it. Also I installed Visual Studio 2015 and I started to make an OpenCV project.

Unfortunately, the existing binaries didn't support Visual Studio 2015 (or, more precisely, msvc 14). The only option was to build OpenCV myself. So here is my tutorial of building OpenCV. As stated in the title, this is a x64 build with Qt. I found out that Qt is pretty amazing, but I'm sure you can use this tutorial to build OpenCV without Qt. Just use your brains. Same with the x64 stuff; if you want x86, you should be able to do that. Furthermore, I use some version numbers. Don't take that too seriously; if there's a newer version available, you probably want the newer version.

Sorry for spelling and grammar errors; English is not my mother tongue.

Prerequisites

Human

  • Basic knowledge of C++. At least you should be able to code and compile hello world.
  • Basic knowledge of Visual Studio 2012 or later.
  • Basic knowledge of Git. git clone and git checkout are enough. Learn git here.
  • Ability to:
    • change system variables.
    • use an installer.
    • ask your fellow programmers or search on the internet. Tip: DuckDuckGo !Bang Tech.
    • to work with preview software. Update: most of the software used in this tutorial is now superseded by stable editions.
    • read instructions carefully.

Machine

Qt has the following prerequisites:

I recommend you install the latest version of each. Regardless of the version, make sure to install the x64 edition!

Since we are going to use git to download (and update!) the source code, we need... git. I use the GitHub windows client (command line), but any other will do, too.

Finally, you need CMake 3.3.0-rc3 for building OpenCV.

Download

I made the following directory structure:

  • D:\
    • opencv
      • source
      • build
      • dep

Later on, we will add a directory for Qt. For now, clone the OpenCV repo to opencv\source. It's about 430 MB. Now checkout the version you want. I recommend to use the latest stable, with was 3.0.0 at the time of writing, so I use git checkout 3.0.0.

Now, let's download some dependencies.

Put the downloads in D:\opencv\dep and extract them there. Put D:\opencv\dep\tbb43_20150611oss_win\tbb43_20150611oss\bin\intel64\vc12 or similar to your PATH.

Now move on to download QT. Go to https://www.qt.io/download-open-source/#section-2 and download the SOURCE. DO NOT DOWNLOAD INSTALLER! I downloaded a zip with this name: qt-everywhere-opensource-src-5.5.0.zip. Inside that zip, there is a directory with the same name. Copy that to D:. For some reason, both actions took some time at my pc. Finished? Now rename that directory to qt.

Build

Qt

Go to your system variables and add a variable named INCLUDE. Set it's content to your python directory, in my case C:\Python34. Search in your start menu for VS2015 x64 Native Tools Command Prompt. DO NOT USE ANY OTHER COMMAND PROMPT! Navigate to D:\qt and run the following command: configure.bat -debug-and-release -opensource -confirm-license -c++11 -mp. Regarding the options, use -help to see what's possible. You are free to use other options, it doesn't really matter for this tutorial, I think. This could take a minute. As the output states, if you did something wrong, run nmake confclean and run configure again.

Do not close that command line!

So you think you build Qt now? Nope, you only made some preparations. Now you have to run nmake (no arguments necessary). This will build Qt. THIS COULD TAKE A LONG TIME! In my case about two hours. Popcorn time!

OK, done? Now excute the following: setx -m QTDIR D:\qt (you could get an error, because you need administrator privileges). Don't worry, it's a matter of milliseconds. Now add the following to your PATH: %QTDIR%\qtbase\bin.

Restart your computer.

OpenCV

Now, start CMake (cmake-qui). The upper fields ask for source code and where to build binaries. That's respectively D:/opencv/source and D:/opencv/build. I believe you should use forward slashes. Press configure. It ask you for the generator. Use Visual Studio 14 2015 Win64. There's some radio buttons below the drop-down. Leave it to Use default native compilers. Click finish. Every entry in the list box in the middle will be red. Don't worry.

You probably want to check the Grouped-checkbox. Now you have various groups. WITH and BUILD are the most important ones for now. Check the following checkboxes:

  • WITH_OPENMP
  • WITH_QT
  • WITH_TBB

Disable the following:

  • BUILD_DOCS
  • BUILD_PERF_TESTS
  • BUILD_TESTS
  • WITH_CUDA (see below for the why)

Click configure again (it won't ask for a generator this time). Still some entries will be red. Still don't worry. One of that entries will be TBB_INCLUDE_DIRS. Type your TBB include directory there. In my case, that's D:\opencv\dep\tbb43_20150611oss_win\tbb43_20150611oss\include. Set TBB_LIB_DIR to your TBB lib directory, in my case D:\opencv\dep\tbb43_20150611oss_win\tbb43_20150611oss\lib\intel64\vc12_ui.

Click configure again. Still some entries will be red. Still don't worry. You don't have to change something now (although you may).

Click configure again. Still some red entries? Now you have to worry, because there shouldn't be any. Check the red entries and use your brain.

Notice that when clicking configure, the output below will spit out some warnings. You can safely ignore them.

When you're done configuring, click Generate. Again you could see some warnings. Now close CMake and head over to D:\opencv\source. You should see a file named OpenCV.sln. Open it. Since there are a lot of projects in there, give Visual Studio some time.

Make sure that your current configuration is Debug. Now, right click on the project ALL_BUILD and choose Build. On my pc, it took about 3 minutes to complete. Now build again with configuration Release (again about 3 minutes). Do not close Visual Studio.

Install

Building is done, but we need some extra steps to use it. In the open solution, there should be a folder called CMakeTargets. In there, a project called INSTALL. Build it. It will make the include directory. Open a command prompt and execute setx -m OPENCV_DIR D:\opencv\build\install\x64\vc14. Add %OPENCV_DIR%\bin to your PATH.

Restart your computer. You're done.

Test

Let's make hello world. If you're lazy, I've made a template project, which include all the following settings and code: link.

  • Open Visual Studio
  • Click new project
  • Choose Win32 Console Application
  • Choose a project name and a solution name
  • Click OK
  • Click Next
  • Check Empty project
  • Disable Security Development Lifecycle (SDL) checks (I've no idea what it does)
  • Click Finish
  • Open the Configuration Manager
  • Under Active solution platform, click New
  • Select x64 and copy settings from x86
  • Make sure Create new project platforms is checked
  • Click OK
  • Under Active solution platform, click Edit
  • Select x86 and click Remove
  • Click Close and click Close again
  • Go to Property Manager and right click and remove both Debug | Win32 and Release | Win32
  • Right click on Debug | x64 and choose to add a new property sheet
  • Name it whatever you want. You are going to use this as settings file for all of your OpenCV projects
  • I choose opencv_debug as a name. Right click it and click Properties
  • In C/C++ > General > Additional Include Directories type this: $(OPENCV_DIR)\..\..\include
  • In Linker > General > Additional Library Directories type this: $(OPENCV_DIR)\lib
  • In Linker > Input > Additional Dependencies click Edit
  • Add the following:
    • opencv_calib3d300d.lib
    • opencv_core300d.lib
    • opencv_features2d300d.lib
    • opencv_flann300d.lib
    • opencv_highgui300d.lib
    • opencv_imgcodecs300d.lib
    • opencv_imgproc300d.lib
    • opencv_ml300d.lib
    • opencv_objdetect300d.lib
    • opencv_photo300d.lib
    • opencv_shape300d.lib
    • opencv_stitching300d.lib
    • opencv_superres300d.lib
    • opencv_ts300d.lib
    • opencv_video300d.lib
    • opencv_videoio300d.lib
    • opencv_videostab300d.lib
  • Click OK and OK
  • Make another property sheet for Release
  • Enter NEARLY the same values: in Additional dependencies you have to use the libs WITHOUT that trailing d! So opencv_calib3d300.lib instead of opencv_calib3d300d.lib
  • Edit the property sheet named Microsoft.Cpp.x64.User. Use the one listed in Release | x64!
  • In C/C++ > General set Warning Level to Level3. The compiler is your friend, it will warn you before your fellow programmers see you've written bad code. (Recommended)
  • In Multi-processor Compilation set to Yes (/MP) (Recommended)
  • In C/C++ > Optimization, set Enable Intrinsic Functions to Yes (Recommended)
  • In Favor Size Or Speed, choose Favor fast code (/Ot) (Recommended)
  • In C/C++ > Code Generation, set Enable Parallel Code Generation to Yes (/Qpar) (Recommended)
  • In C/C++ > Language, set Open MP Support to Yes (/openmp) (Recommended. Learn more.)
  • Save and close the property page shizzle
  • Make a new source file. I named mine main.cpp
  • Add the following code:
#include <opencv2\opencv.hpp>

using namespace cv;

int main(int argc, char** argv)
{
    VideoCapture cap(0);
    if(!cap.isOpened())
    {
        std::cout << "Sorry, can't use webcam" << std::endl;
    }
    while(true)
    {
        Mat frame;
        cap >> frame;
        imshow("Frame", frame);
        if(waitKey(30) == 'q')
        {
            break;
        }
    }
}
  • It should compile and run. A nice window with a live-stream of your webcam should appear.
  • Try to zoom in. If you zoom in close enough (about x30), you will see individual pixel values. Perfect for debugging!

Pitfalls

  • Install or build a x86 version of one of the dependencies and build a x64 version of OpenCV.
  • Something is not added to the PATH or another system variable.
  • You added something to the PATH, but you used the user PATH instead of the system PATH.
  • You forgot to restart after changing the PATH.

Conclusion

OK, you're ready to build your own applications. There are a few notes I'd like to share with you:

  • I tried to build with CUDA, since GPU acceleration can be very useful when processing images. But I couldn't find a solution to use CUDA with msvc 14. Please let me know if you found a solution.
  • There's a nice Qt add-in for Visual Studio with will aid you when making Qt projects. It isn't doesn't work with Visual Studio 2015, even when compiling the add-in from source (compiling fails). Please let me know if you found a solution.
  • I used x64 because I think that's the future. It has better performance too: our programs only need one step to handle 64-bit numbers instead of two. I don't understand why so much programs default to x86. x64 just works, it isn't preview software. Let's change the world and use x64 all over the place!
  • Tip: in our test project we've made a property sheet. Notice you can reuse them in other OpenCV projects. Actually, that's why we made them!
@mister-good-deal
Copy link

CUDA works fine in my mvsc 2015 using Nvidia cuda_8.0.44_win10.exe in their official website that includes NVIDIA_Nsight_Visual_Studio_Edition_Win64_5.2.0.16218.msi

Qt-vs-addin-msvc2015-2.0.0-beta.vsix still not work, it freezes visual studio when I try to use the UI designer feature. Others components seems to work.

I'm currently at the building OpenCV indide visual studio (debug) step. It takes a long time, the process is still running with several warnings.

@mister-good-deal
Copy link

It seems it's the cuda opencv feature that I checked in the CMake options which takes a long time to compile.

@mister-good-deal
Copy link

Compilation done.
It took about 30 min.

===== Build: 65 succeeded, 0 failed, 0 up-to-date, 0 skipped =====

@Eng-Fouad
Copy link

I had to add /APPCONTAINER:NO to the following properties during the build process, otherwise it will not work:

CMAKE_EXE_LINKER_FLAGS
CMAKE_MODULE_LINKER_FLAGS
CMAKE_SHARED_LINKER_FLAGS

@Bahramudin
Copy link

If I build OpenCV without Qt, can I use the OpenCV in Qt? If yes, then what is the advantage of the building OpenCV with Qt? The second question is why we need to don't select CUDA, I need CUDA, for GPU Mat, if we do not have the CUDA functionality in Qt, then there is no need to use Qt build Computer Vision application. Thanks!! waiting to you reply...

@BartG95
Copy link
Author

BartG95 commented Oct 4, 2017

If I build OpenCV without Qt, can I use the OpenCV in Qt?

I think so.

If yes, then what is the advantage of the building OpenCV with Qt?

OpenCV provides some UI elements for testing. These elements will be based on Qt if you build with Qt, which will make the elements look nicer. But if it wasn't clear: this was merely a fun project to learn something about compiling stuff.

The second question is why we need to don't select CUDA

Well, because enabling it didn't work for me. As per previous comments, other people have no problem with enabling it.

I need CUDA, for GPU Mat, if we do not have the CUDA functionality in Qt, then there is no need to use Qt build Computer Vision application.

Probably. Again, this was for fun.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment