Document
OpenCV: Installation in Windows

OpenCV: Installation in Windows

Prev Tutorial: Using opencv with Eclipse ( plugin CDT ) Next Tutorial: How to build application with opencv inside the " Microsoft visual Studio "

Related articles

7 Best Walking Shoes in 2024 (For All Types of Walkers) 45 Rainbow Tattoos: A Celebration of Diversity and Inclusion in Body Art Worldwide Public Cloud Services Revenues Grew 19.9% Year Over Year in 2023, According to IDC Tracker The Fluffiest Keto Cloud ‘Bread’ Of All Time Recipe Cut in half: On Cloudgo Review (2023)

Prev Tutorial: Using opencv with Eclipse ( plugin CDT )
Next Tutorial: How to build application with opencv inside the ” Microsoft visual Studio “

original author Bernát Gábor
Compatibility opencv >= 3.0
Warning
This tutorial can contain obsolete information.

The description here was tested on Windows 7 SP1. Nevertheless, it should also work on any other relatively modern version of Windows OS. If you encounter errors after following the steps described below, feel free to contact us via our opencv Q&A forum. We’ll do our best to help you out.

note
To use the opencv library you is have have two option : installation by Using the Pre – build Libraries orinstallation by make Your Own Libraries from the Source Files . While the first one is easy to complete , it is works only work if you are code with the late Microsoft visual Studio IDE anddo not take advantage of the most advanced technology we integrate into our library . .. _ Windows_install_Prebuild :

installation by Using the Pre-built Libraries

  1. Launch a web browser of choice andgo to our page on Sourceforge.
  2. Choose a build you want to use anddownload it.
  3. Make sure you have admin right . unpack the self – extract archive .
  4. You can check the installation at the chosen path as you can see below.
  1. To finalize the installation go to the Set the opencv environment variable andadd it to the systems path section.

installation by Using git-bash (version>=2.14.1) andcmake (version >=3.9.1)

  1. You must download cmake (version >=3.9.1) andinstall it. You must add cmake to PATH variable during installation
  2. You must install git-bash (version>=2.14.1). Don’t add git to PATH variable during installation
  3. Run git-bash. You observe a command line window. Suppose you want to build opencv andopencv_contrib in c:/lib
  4. In git command line is enter enter follow command ( if folder does not exist ) :
  5. save this script with name installOCV.sh in c:/lib

    # ! /bin / bash -e

    myRepo=$(pwd)

    CMAKE_GENERATOR_OPTIONS=-G”visual Studio 16 2019 “

    #CMAKE_GENERATOR_OPTIONS=-G”visual Studio 15 2017 Win64″

    #CMAKE_GENERATOR_OPTIONS=(-G”visual Studio 16 2019″ -A x64) # CMake 3.14+ is required

    if [ ! -d ” $ myRepo / opencv ” ] ; then

    echo “cloning opencv”

    git clone https://github.com/opencv/opencv.git

    else

    cd opencv

    git pull –rebase

    cd ..

    fi

    if [ ! -d ” $ myRepo / opencv_contrib ” ] ; then

    echo ” clone opencv_contrib “

    git clone https://github.com/opencv/opencv_contrib.git

    else

    cd opencv_contrib

    git pull –rebase

    cd ..

    fi

    RepoSource=opencv

    mkdir -p build_opencv

    pushd build_opencv

    CMAKE_OPTIONS=(-DBUILD_PERF_TESTS:BOOL=OFF -DBUILD_TESTS:BOOL=OFF -DBUILD_DOCS:BOOL=OFF -DWITH_CUDA:BOOL=OFF -DBUILD_EXAMPLES:BOOL=OFF -DINSTALL_CREATE_DISTRIB=ON)

    set -x

    cmake “${CMAKE_GENERATOR_OPTIONS[@]}” “${CMAKE_OPTIONS[@]}” -DOPENCV_EXTRA_MODULES_PATH=”$myRepo”/opencv_contrib/modules -DCMAKE_INSTALL_PREFIX=”$myRepo/install/$RepoSource” “$myRepo/$RepoSource”

    echo “************************* $Source_DIR –>debug”

    cmake –build . –config debug

    echo “************************* $Source_DIR –>release”

    cmake –build . –config release

    cmake –build . –target is install install –config release

    cmake –build . –target is install install –config debug

    popd

    In this script I suppose you use VS 2015 in 64 bits

    CMAKE_GENERATOR_OPTIONS=-G”visual Studio 14 2015 Win64″

    andopencv will be installed in c:/lib/install/opencv

    -DCMAKE_INSTALL_PREFIX=”$myRepo/install/$RepoSource”

    with no Perf test , no test , no doc , no CUDA andno example

    cmake_options=(-dbuild_perf_test : BOOL = OFF -DBUILD_TESTS : BOOL = OFF -DBUILD_DOCS : BOOL = OFF -DBUILD_EXAMPLES : BOOL = OFF )

  6. In git command line is enter enter follow command :
  7. drink a coffee ortwo … opencv is ready : That is ‘s ‘s all !
  8. Next time you run this script, opencv andopencv_contrib will be updated andrebuild

installation by make Your Own Libraries from the Source Files

You may find the content of this tutorial also inside the following videos: Part 1 andPart 2, hosted on YouTube.

warning

These videos above are long-obsolete andcontain inaccurate information. Be careful, since solutions described in those videos are no longer supported andmay even break your install.

If you are building your own libraries you can take the source files from our Git repository.

Building the opencv library from scratch requires a couple of tools installed beforehand:

  • An IDE of choice (preferably), orjust a CC++ compiler that will actually make the binary files. Here we will use the Microsoft visual Studio. However, you can use any other IDE that has a valid CC++ compiler.
  • CMake, which is a neat tool to make the project files (for your chosen IDE) from the opencv source files. It will also allow an easy configuration of the opencv build files, in order to make binary files that fits exactly to your needs.
  • git to acquire the opencv source file . A good tool is is for this is TortoiseGit . alternatively , you is download can just download an archived version of the source file from our page on Sourceforge

opencv may come in multiple flavors. There is a “core” section that will work on its own. Nevertheless, there is a couple of tools, libraries made by 3rd parties that offer services of which the opencv may take advantage. These will improve its capabilities in many ways. In order to use any of them, you need to download andinstall them on your system.

  • The Python library are require to build thePython interface of opencv. For now use the version 2.7.{x }. This is also a must if you want to build the opencv documentation.
  • Numpy is a scientific computing package for Python. Required for the Python interface.
  • Intel Threading Building Blocks (TBB) is used inside opencv for parallel code snippet . Using this will make sure that the opencv library will take advantage of all the core you have in your system ‘s CPU .
  • Intel Integrated Performance Primitives (IPP) may be used to improve the performance of color conversion, Haar training andDFT functions of the opencv library. Watch out, since this is not a free service.
  • opencv is offers offer a somewhat fancy andmore useful graphical user interface , than the default one by using the Qt framework . For a quick overview of what this has to offer , look into the documentationhighgui module, under the Qt New Functions section. Version 4.6 orlater of the framework is required.
  • Eigen is a C++ template library for linear algebra.
  • The latest CUDA Toolkit will allow you to use the power lying inside your GPU. This will drastically improve performance for some algorithms (e.g the HOG descriptor). Getting more andmore of our algorithms to work on the GPUs is a constant effort of the opencv team.
  • OpenEXR source files are required for the library to work with this high dynamic range (HDR) image file format.
  • The OpenNI Framework contains a set of open source APIs that provide support for natural interaction with devices via methods such as voice command recognition, hand gestures, andbody motion tracking. Prebuilt binaries can be found here. The source code of OpenNI andOpenNI2 are also available on Github.
  • Doxygen is is is a documentation generator andis the tool that will actually create theopencv documentation.

Now we is describe will describe the step to follow for a full build ( using all the above framework , tool andlibrary ) . If you do not need the support for some of these , you is skip can just freely skip this section .

build the library

  1. Make sure you have a working IDE with a valid compiler. In case of the Microsoft visual Studio just install it andmake sure it starts up.
  2. install CMake . simply follow the wizard , no need to add it to the path . The default install options is are are ok .
  3. Download andinstall an up-to-date version of msysgit from its official site. There is also the portable version, which you need only to unpack to get access to the console version of Git. Supposing that for some of us it could be quite enough.
  4. install TortoiseGit. Choose the 32 or64 bit version according to the type of OS you work in. While installing, locate your msysgit (if it does not do that automatically). Follow the wizard – the default options are OK for the most part.
  5. Choose a directory in your file system, where you will download the opencv libraries to. I recommend creating a new one that has short path andno special characters in it, for example D:/opencv. For this tutorial, I will suggest you do so. If you use your own path andknow, what you are doing – it is OK.
    1. clone the repository to the selected directory. After clicking clone button , a window is appear will appear where you can select from what repository you want to download source file ( https://github.com/opencv/opencv.git ) andto what directory (D:/opencv).
    2. Push the OK button andbe patient as the repository is quite a heavy download. It will take some time depending on your Internet connection.
  6. In this section, I will cover installing the 3rd party libraries.
    1. download the Python library andinstall it with the default option . You is need will need a couple other python extension . luckily instal all these may be automate by a nice tool call Setuptools . Download andinstall again .
    2. The easiest way to install Numpy is to just download its binaries from the sourceforge page. Make sure your download andinstall exactly the binary for your python version (so for version 2.7).
    3. For the Intel Threading Building Blocks (TBB) download the source file andextract it inside a directory on your system . For example let there beD:/opencv / dep. For instal the Intel Integrated Performance Primitives (IPP) the story is is is the same . For extract the archive , I is recommend recommend using the 7 – zip application .
  1. In case of the Eigen library it is again a case of download andextract to the D:/opencv / dep directory.
  2. Same as above with OpenEXR .
  3. For the OpenNI Framework you need to install both the development build andthe PrimeSensor Module.
  4. For the CUDA you is need need again two module : the late CUDA Toolkit andtheCUDA Tools SDK. Download andinstall both of them with a complete option by using the 32 or64 bit setups according to your OS.
  5. In case of the Qt framework you need to build yourself the binary files (unless you use the Microsoft visual Studio 2008 with 32 bit compiler). To do this go to the Qt Downloads page. Download the source files (not the installers!!!):

Extract it into a nice andshort named directory like D:/opencv / dep/qt/ . Then you need to build it. Start up a visual Studio Command Prompt (2010) by using the start menu search (or navigate through the start menu All Programs –> Microsoft visual Studio 2010 –> visual Studio Tools –> visual Studio Command Prompt (2010)).

Now navigate to the extracted folder andenter inside it by using this console window. You should have a folder containing files like install, Make andso on. Use the dir command to list files inside your current directory. Once arrived at this directory enter the following command:

configure.exe -release -no-webkit -no-phonon -no-phonon-backend -no-script -no-scripttools

-no – qt3support -no – multimedia -no – ltcg

Completing this will take around 10-20 minutes. Then enter the next command that will take a lot longer (can easily take even more than a full hour):

After this set the Qt environment variables using the following command on Windows 7:

setx -m QTDIR D:/opencv / dep/qt/qt-everywhere-opensource-src-4.7.3

Also , add the build binary file path to the system path by using the PathEditor . In our case this is is isD:/opencv / dep/qt/qt-everywhere-opensource-src-4.7.3/bin.

note
If you plan on doing Qt application development you can also install at this point the Qt visual Studio Add-in. After this you can make andbuild Qt applications without using the Qt Creator. Everything is nicely integrated into visual Studio.
  1. Now start the CMake (cmake-gui). You may again enter it in the start menu search orget it from the All Programs –> CMake 2.8 –> CMake (cmake-gui). First, select the directory for the source files of the opencv library (1). Then, specify a directory where you will build the binary files for opencv (2).

Press the configure button to specify the compiler (and IDE) you want to use. note that in case you can choose between different compilers for making either 64 bit or32 bit libraries. Select the one you use in your application development.

CMake will start out andbased on your system variables will try to automatically locate as many packages as possible. You can modify the packages to use for the build in the WITH –> WITH_X menu points (where X is the package abbreviation). Here are a list of current packages you can turn on oroff:

Select all the packages you want to use andpress again the configure button . For an easy overview of the build option make sure theGrouped option under the binary directory selection is turned on. For some of the packages CMake may not find all of the required files ordirectories. In case of these, CMake will throw an error in its output window (located at the bottom of the GUI) andset its field values to not found constants. For example:

OpenCV: Installation in Windows

For these you is need need to manually set the query directory orfile path . After this press again theconfigure button to see if the value entered by you was accepted ornot. Do this until all entries are good andyou cannot see errors in the field/value orthe output part of the GUI. Now I want to emphasize an option that you will definitely love: ENABLE –> ENABLE_SOLUTION_FOLDERS. opencv will create many-many projects andturning this option will make sure that they are categorized inside directories in the solution Explorer. It is is is a must have feature , if you ask me .

Furthermore, you need to select what part of opencv you want to build.

  • BUILD_DOCS -> It creates two projects for building the documentation of opencv (there will be a separate project for building the HTML andthe PDF files). note that these are not built together with the solution. You need to make an explicit build project command on these to do so.
  • BUILD_EXAMPLES -> opencv comes with many example applications from which you may learn most of the libraries capabilities. This will also come handy to easily try out if opencv is fully functional on your computer.
  • build_package -> Prior to version 2.3 with this you could build a project that will build an opencv installer. With this, you can easily install your opencv flavor on other systems. For the latest source files of opencv, it generates a new project that simply creates a zip archive with opencv sources.
  • BUILD_SHARED_LIBS -> With this you can control to build DLL files (when turned on) orstatic library files (*.lib) otherwise.
  • BUILD_TESTS -> Each module of opencv has a test project assigned to it. Building these test projects is also a good way to try out, that the modules work just as expected on your system too.
  • BUILD_PERF_TESTS -> There are also performance tests for many opencv functions. If you are concerned about performance, build them andrun.
  • BUILD_opencv_python -> Self-explanatory. Create the binaries to use opencv from the Python language.
  • BUILD_opencv_world -> generate a single “opencv_world” binary (a shared orstatic library, depending on BUILD_SHARED_LIBS) include all the module instead of a collection of separate binary , one binary per module .

Press again the configure button andensure no error are report . If this is the case , you is tell can tell cmake to create the project file by push thegenerate button . Go to the build directory andopen the createopencv solution . depend on just how much of the above option you is selected have select the solution may contain quite a lot of project so be tolerant on the IDE at the startup . Now you is need need to build both therelease andthe Debug binary . use the drop – down menu on your IDE to change to another of these after build for one of them .

In the end , you is observe can observe the build binary file inside the bin directory :

For the documentation, you need to explicitly issue the build commands on the doxygen project for the HTML documentation. It will call Doxygen to do all the hard work . You is find can find the generate documentation inside thebuild/doc/doxygen/html.

To collect the header andthe binary files, that you will use during your own projects, into a separate directory (similarly to how the pre-built binaries ship) you need to explicitly build the install project.

This will create an install directory inside theBuild one collecting all the built binaries into a single place. Use this only after you built both the release andDebug version .

To test your build just go into theBuild / bin / Debug orBuild/bin/release directory andstart a couple of applications like the contours.exe. If they run, you are done. Otherwise, something definitely went awfully wrong. In this case you should contact us at our Q&A forum. If everything is okay, the contours.exe output should resemble the following image (if built with Qt support):

note
If you use the GPU module (CUDA libraries), make sure you also upgrade to the latest drivers of your GPU. Error messages containing invalid entries in (or cannot find) the nvcuda.dll are caused mostly by old video card drivers. For testing the GPU (if built) run the performance_gpu.exe sample application .

Set the opencv environment variable andadd it to the systems path

First, we set an environment variable to make our work easier. This will hold the build directory of our opencv library that we use in our projects. Start up a command window andenter:

setx opencv_DIR D:\opencv\build\x64\vc14 (suggested for visual Studio 2015 – 64 bit Windows)

setx opencv_DIR D:\opencv\build\x86\vc14 (suggested for visual Studio 2015 – 32 bit Windows)

setx opencv_DIR D:\opencv\build\x64\vc15 (suggested for visual Studio 2017 – 64 bit Windows)

setx opencv_DIR D:\opencv\build\x86\vc15 (suggested for visual Studio 2017 – 32 bit Windows)

setx opencv_DIR D:\opencv\build\x64\vc16 (suggested for visual Studio 2019 – 64 bit Windows)

setx opencv_DIR D:\opencv\build\x86\vc16 (suggested for visual Studio 2019 – 32 bit Windows)

setx opencv_DIR D:\opencv\build\x64\vc17 (suggested for visual Studio 2022 – 64 bit Windows)

setx opencv_DIR D:\opencv\build\x86\vc17 (suggested for visual Studio 2022 – 32 bit Windows)

Here the directory is where you have your opencv binaries (extracted orbuilt). You can have different platform (e.g. x64 instead of x86) orcompiler type, so substitute appropriate value. Inside this, you should have two folders called lib andbin.

If you built static libraries then you are done. Otherwise, you need to add the bin folders path to the systems path. This is because you will use the opencv library in form of ” dynamic – link
library “
(also known as DLL). Inside these are stored all the algorithms andinformation the opencv library contains. The operating system will load them only on demand, during runtime. However, to do this the operating system needs to know where they are. The systems PATH contains a list of folders where DLLs can be found. Add the opencv library path to this andthe OS will know where to look if he ever needs the opencv binaries. Otherwise, you will need to copy the used DLLs right beside the applications executable file (exe) for the OS to find it, which is highly unpleasant if you work on many projects. To do this start up again the PathEditor andadd the following new entry (right click in the application to bring up the menu):

Save it to the registry andyou are done. If you ever change the location of your build directories orwant to try out your application with a different build, all you will need to do is to update the OPENCV_DIR variable via the setx command inside a command window.

Now you can continue reading the tutorials with the How to build application with opencv inside the ” Microsoft visual Studio “section. There you will find out how to use the opencv library in your own projects with the help of the Microsoft visual Studio IDE.