1 Installation in Windows {#tutorial_windows_install}
2 =======================
4 @prev_tutorial{tutorial_linux_eclipse}
5 @next_tutorial{tutorial_windows_visual_studio_opencv}
8 The description here was tested on Windows 7 SP1. Nevertheless, it should also work on any other
9 relatively modern version of Windows OS. If you encounter errors after following the steps described
10 below, feel free to contact us via our [OpenCV Q&A forum](http://answers.opencv.org). We'll do our
13 @note To use the OpenCV library you have two options: @ref tutorial_windows_install_prebuilt or
14 @ref tutorial_windows_install_build. While the first one is easier to complete, it only works if you are coding
15 with the latest Microsoft Visual Studio IDE and do not take advantage of the most advanced
16 technologies we integrate into our library. .. _Windows_Install_Prebuild:
18 Installation by Using the Pre-built Libraries {#tutorial_windows_install_prebuilt}
19 =============================================
21 -# Launch a web browser of choice and go to our [page on
22 Sourceforge](http://sourceforge.net/projects/opencvlibrary/files/opencv-win/).
23 -# Choose a build you want to use and download it.
24 -# Make sure you have admin rights. Unpack the self-extracting archive.
25 -# You can check the installation at the chosen path as you can see below.
27 ![](images/OpenCV_Install_Directory.png)
29 -# To finalize the installation go to the @ref tutorial_windows_install_path section.
31 Installation by Using git-bash (version>=2.14.1) and cmake (version >=3.9.1){#tutorial_windows_gitbash_build}
32 ===============================================================
34 -# You must download [cmake (version >=3.9.1)](https://cmake.org) and install it. You must add cmake to PATH variable during installation
36 -# You must install [git-bash (version>=2.14.1)](https://git-for-windows.github.io/). Don't add git to PATH variable during installation
38 -# Run git-bash. You observe a command line window.
39 Suppose you want to build opencv and opencv_contrib in c:/lib
41 -# In git command line enter following command (if folder does not exist) :
47 -# save this script with name installOCV.sh in c:/lib
51 CMAKE_CONFIG_GENERATOR="Visual Studio 14 2015 Win64"
52 if [ ! -d "$myRepo/opencv" ]; then
54 git clone https://github.com/opencv/opencv.git
56 mkdir -p Install/opencv
62 if [ ! -d "$myRepo/opencv_contrib" ]; then
63 echo "cloning opencv_contrib"
64 git clone https://github.com/opencv/opencv_contrib.git
65 mkdir -p Build/opencv_contrib
72 pushd Build/$RepoSource
73 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'
74 cmake -G"$CMAKE_CONFIG_GENERATOR" $CMAKE_OPTIONS -DOPENCV_EXTRA_MODULES_PATH="$myRepo"/opencv_contrib/modules -DCMAKE_INSTALL_PREFIX="$myRepo"/install/"$RepoSource" "$myRepo/$RepoSource"
75 echo "************************* $Source_DIR -->debug"
76 cmake --build . --config debug
77 echo "************************* $Source_DIR -->release"
78 cmake --build . --config release
79 cmake --build . --target install --config release
80 cmake --build . --target install --config debug
83 In this script I suppose you use VS 2015 in 64 bits
85 CMAKE_CONFIG_GENERATOR="Visual Studio 14 2015 Win64"
87 and opencv will be installed in c:/lib/install
89 -DCMAKE_INSTALL_PREFIX="$myRepo"/install/"$RepoSource" "$myRepo/$RepoSource"
91 with no Perf tests, no tests, no doc, no CUDA and no example
93 CMAKE_OPTIONS='-DBUILD_PERF_TESTS:BOOL=OFF -DBUILD_TESTS:BOOL=OFF -DBUILD_DOCS:BOOL=OFF -DBUILD_EXAMPLES:BOOL=OFF'
95 -# In git command line enter following command :
99 -# Drink a coffee or two... opencv is ready : That's all!
100 -# Next time you run this script, opencv and opencv_contrib will be updated and rebuild
103 Installation by Making Your Own Libraries from the Source Files {#tutorial_windows_install_build}
104 ===============================================================
106 You may find the content of this tutorial also inside the following videos:
107 [Part 1](https://www.youtube.com/watch?v=NnovZ1cTlMs) and [Part 2](https://www.youtube.com/watch?v=qGNWMcfWwPU), hosted on YouTube.
109 @youtube{NnovZ1cTlMs}
110 @youtube{qGNWMcfWwPU}
114 These videos above are long-obsolete and contain inaccurate information. Be careful, since
115 solutions described in those videos are no longer supported and may even break your install.
117 If you are building your own libraries you can take the source files from our [Git
118 repository](https://github.com/opencv/opencv.git).
120 Building the OpenCV library from scratch requires a couple of tools installed beforehand:
122 - An IDE of choice (preferably), or just a CC++ compiler that will actually make the binary files.
123 Here we will use the [Microsoft Visual Studio](https://www.microsoft.com/visualstudio/en-us).
124 However, you can use any other IDE that has a valid CC++ compiler.
125 - [CMake](http://www.cmake.org/cmake/resources/software.html), which is a neat tool to make the project files (for your chosen IDE) from the OpenCV
126 source files. It will also allow an easy configuration of the OpenCV build files, in order to
127 make binary files that fits exactly to your needs.
128 - Git to acquire the OpenCV source files. A good tool for this is [TortoiseGit](http://code.google.com/p/tortoisegit/wiki/Download). Alternatively,
129 you can just download an archived version of the source files from our [page on
130 Sourceforge](http://sourceforge.net/projects/opencvlibrary/files/opencv-win/)
132 OpenCV may come in multiple flavors. There is a "core" section that will work on its own.
133 Nevertheless, there is a couple of tools, libraries made by 3rd parties that offer services of which
134 the OpenCV may take advantage. These will improve its capabilities in many ways. In order to use any
135 of them, you need to download and install them on your system.
137 - The [Python libraries](http://www.python.org/downloads/) are required to build the *Python interface* of OpenCV. For now use the
138 version `2.7.{x}`. This is also a must if you want to build the *OpenCV documentation*.
139 - [Numpy](http://numpy.scipy.org/) is a scientific computing package for Python. Required for the *Python interface*.
140 - [Intel Threading Building Blocks (*TBB*)](http://threadingbuildingblocks.org/file.php?fid=77) is used inside OpenCV for parallel code
141 snippets. Using this will make sure that the OpenCV library will take advantage of all the cores
142 you have in your system's CPU.
143 - [Intel Integrated Performance Primitives (*IPP*)](http://software.intel.com/en-us/articles/intel-ipp/) may be used to improve the performance
144 of color conversion, Haar training and DFT functions of the OpenCV library. Watch out, since
145 this is not a free service.
146 - OpenCV offers a somewhat fancier and more useful graphical user interface, than the default one
147 by using the [Qt framework](http://qt.nokia.com/downloads). For a quick overview of what this has to offer, look into the
148 documentations *highgui* module, under the *Qt New Functions* section. Version 4.6 or later of
149 the framework is required.
150 - [Eigen](http://eigen.tuxfamily.org/index.php?title=Main_Page#Download) is a C++ template library for linear algebra.
151 - The latest [CUDA Toolkit](http://developer.nvidia.com/cuda-downloads) will allow you to use the power lying inside your GPU. This will
152 drastically improve performance for some algorithms (e.g the HOG descriptor). Getting more and
153 more of our algorithms to work on the GPUs is a constant effort of the OpenCV team.
154 - [OpenEXR](http://www.openexr.com/downloads.html) source files are required for the library to work with this high dynamic range (HDR)
156 - 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, and body
157 motion tracking. Prebuilt binaries can be found [here](http://structure.io/openni). The source code of [OpenNI](https://github.com/OpenNI/OpenNI) and [OpenNI2](https://github.com/OpenNI/OpenNI2) are also available on Github.
158 - [Doxygen](http://www.doxygen.nl) is a documentation generator and is the tool that will actually create the
159 *OpenCV documentation*.
161 Now we will describe the steps to follow for a full build (using all the above frameworks, tools and
162 libraries). If you do not need the support for some of these, you can just freely skip this section.
164 ### Building the library
166 -# Make sure you have a working IDE with a valid compiler. In case of the Microsoft Visual Studio
167 just install it and make sure it starts up.
168 -# Install [CMake](http://www.cmake.org/cmake/resources/software.html). Simply follow the wizard, no need to add it to the path. The default install
170 -# Download and install an up-to-date version of msysgit from its [official
171 site](http://code.google.com/p/msysgit/downloads/list). There is also the portable version,
172 which you need only to unpack to get access to the console version of Git. Supposing that for
173 some of us it could be quite enough.
174 -# Install [TortoiseGit](http://code.google.com/p/tortoisegit/wiki/Download). Choose the 32 or 64 bit version according to the type of OS you work in.
175 While installing, locate your msysgit (if it does not do that automatically). Follow the
176 wizard -- the default options are OK for the most part.
177 -# Choose a directory in your file system, where you will download the OpenCV libraries to. I
178 recommend creating a new one that has short path and no special characters in it, for example
179 `D:/OpenCV`. For this tutorial, I will suggest you do so. If you use your own path and know, what
180 you are doing -- it is OK.
181 -# Clone the repository to the selected directory. After clicking *Clone* button, a window will
182 appear where you can select from what repository you want to download source files
183 (<https://github.com/opencv/opencv.git>) and to what directory (`D:/OpenCV`).
184 -# Push the OK button and be patient as the repository is quite a heavy download. It will take
185 some time depending on your Internet connection.
187 -# In this section, I will cover installing the 3rd party libraries.
188 -# Download the [Python libraries](http://www.python.org/downloads/) and install it with the default options. You will need a
189 couple other python extensions. Luckily installing all these may be automated by a nice tool
190 called [Setuptools](http://pypi.python.org/pypi/setuptools#downloads). Download and install
193 -# The easiest way to install Numpy is to just download its binaries from the [sourceforge page](http://sourceforge.net/projects/numpy/files/NumPy/).
194 Make sure your download and install
195 exactly the binary for your python version (so for version `2.7`).
197 -# For the [Intel Threading Building Blocks (*TBB*)](http://threadingbuildingblocks.org/file.php?fid=77)
198 download the source files and extract
199 it inside a directory on your system. For example let there be `D:/OpenCV/dep`. For installing
200 the [Intel Integrated Performance Primitives (*IPP*)](http://software.intel.com/en-us/articles/intel-ipp/)
201 the story is the same. For
202 extracting the archives, I recommend using the [7-Zip](http://www.7-zip.org/) application.
204 ![](images/IntelTBB.png)
206 -# In case of the [Eigen](http://eigen.tuxfamily.org/index.php?title=Main_Page#Download) library it is again a case of download and extract to the
207 `D:/OpenCV/dep` directory.
208 -# Same as above with [OpenEXR](http://www.openexr.com/downloads.html).
209 -# For the [OpenNI Framework](http://www.openni.org/) you need to install both the [development
210 build](http://www.openni.org/downloadfiles/opennimodules/openni-binaries/21-stable) and the
212 Module](http://www.openni.org/downloadfiles/opennimodules/openni-compliant-hardware-binaries/32-stable).
213 -# For the CUDA you need again two modules: the latest [CUDA Toolkit](http://developer.nvidia.com/cuda-downloads) and the *CUDA Tools SDK*.
214 Download and install both of them with a *complete* option by using the 32 or 64 bit setups
215 according to your OS.
216 -# In case of the Qt framework you need to build yourself the binary files (unless you use the
217 Microsoft Visual Studio 2008 with 32 bit compiler). To do this go to the [Qt
218 Downloads](http://qt.nokia.com/downloads) page. Download the source files (not the
221 ![](images/qtDownloadThisPackage.png)
223 Extract it into a nice and short named directory like `D:/OpenCV/dep/qt/` . Then you need to
224 build it. Start up a *Visual* *Studio* *Command* *Prompt* (*2010*) by using the start menu
225 search (or navigate through the start menu
226 All Programs --\> Microsoft Visual Studio 2010 --\> Visual Studio Tools --\> Visual Studio Command Prompt (2010)).
228 ![](images/visualstudiocommandprompt.jpg)
230 Now navigate to the extracted folder and enter inside it by using this console window. You
231 should have a folder containing files like *Install*, *Make* and so on. Use the *dir* command
232 to list files inside your current directory. Once arrived at this directory enter the
235 configure.exe -release -no-webkit -no-phonon -no-phonon-backend -no-script -no-scripttools
236 -no-qt3support -no-multimedia -no-ltcg
238 Completing this will take around 10-20 minutes. Then enter the next command that will take a
239 lot longer (can easily take even more than a full hour):
243 After this set the Qt environment variables using the following command on Windows 7:
245 setx -m QTDIR D:/OpenCV/dep/qt/qt-everywhere-opensource-src-4.7.3
247 Also, add the built binary files path to the system path by using the [PathEditor](http://www.redfernplace.com/software-projects/patheditor/). In our
248 case this is `D:/OpenCV/dep/qt/qt-everywhere-opensource-src-4.7.3/bin`.
251 If you plan on doing Qt application development you can also install at this point the *Qt
252 Visual Studio Add-in*. After this you can make and build Qt applications without using the *Qt
253 Creator*. Everything is nicely integrated into Visual Studio.
255 -# Now start the *CMake (cmake-gui)*. You may again enter it in the start menu search or get it
256 from the All Programs --\> CMake 2.8 --\> CMake (cmake-gui). First, select the directory for the
257 source files of the OpenCV library (1). Then, specify a directory where you will build the
258 binary files for OpenCV (2).
260 ![](images/CMakeSelectBin.jpg)
262 Press the Configure button to specify the compiler (and *IDE*) you want to use. Note that in
263 case you can choose between different compilers for making either 64 bit or 32 bit libraries.
264 Select the one you use in your application development.
266 ![](images/CMake_Configure_Windows.jpg)
268 CMake will start out and based on your system variables will try to automatically locate as many
269 packages as possible. You can modify the packages to use for the build in the WITH --\> WITH_X
270 menu points (where *X* is the package abbreviation). Here are a list of current packages you can
273 ![](images/CMakeBuildWithWindowsGUI.jpg)
275 Select all the packages you want to use and press again the *Configure* button. For an easier
276 overview of the build options make sure the *Grouped* option under the binary directory
277 selection is turned on. For some of the packages CMake may not find all of the required files or
278 directories. In case of these, CMake will throw an error in its output window (located at the
279 bottom of the GUI) and set its field values to not found constants. For example:
281 ![](images/CMakePackageNotFoundWindows.jpg)
283 ![](images/CMakeOutputPackageNotFound.jpg)
285 For these you need to manually set the queried directories or files path. After this press again
286 the *Configure* button to see if the value entered by you was accepted or not. Do this until all
287 entries are good and you cannot see errors in the field/value or the output part of the GUI. Now
288 I want to emphasize an option that you will definitely love:
289 ENABLE --\> ENABLE_SOLUTION_FOLDERS. OpenCV will create many-many projects and turning this
290 option will make sure that they are categorized inside directories in the *Solution Explorer*.
291 It is a must have feature, if you ask me.
293 ![](images/CMakeBuildOptionsOpenCV.jpg)
295 Furthermore, you need to select what part of OpenCV you want to build.
297 - *BUILD_DOCS* -\> It creates two projects for building the documentation of OpenCV (there
298 will be a separate project for building the HTML and the PDF files). Note that these are not
299 built together with the solution. You need to make an explicit build project command on
301 - *BUILD_EXAMPLES* -\> OpenCV comes with many example applications from which you may learn
302 most of the libraries capabilities. This will also come handy to easily try out if OpenCV is
303 fully functional on your computer.
304 - *BUILD_PACKAGE* -\> Prior to version 2.3 with this you could build a project that will
305 build an OpenCV installer. With this, you can easily install your OpenCV flavor on other
306 systems. For the latest source files of OpenCV, it generates a new project that simply
307 creates a zip archive with OpenCV sources.
308 - *BUILD_SHARED_LIBS* -\> With this you can control to build DLL files (when turned on) or
309 static library files (\*.lib) otherwise.
310 - *BUILD_TESTS* -\> Each module of OpenCV has a test project assigned to it. Building these
311 test projects is also a good way to try out, that the modules work just as expected on your
313 - *BUILD_PERF_TESTS* -\> There are also performance tests for many OpenCV functions. If
314 you are concerned about performance, build them and run.
315 - *BUILD_opencv_python* -\> Self-explanatory. Create the binaries to use OpenCV from the
317 - *BUILD_opencv_world* -\> Generate a single "opencv_world" binary (a shared or static library, depending on *BUILD_SHARED_LIBS*) including all the modules instead of a collection of separate binaries, one binary per module.
319 Press again the *Configure* button and ensure no errors are reported. If this is the case, you
320 can tell CMake to create the project files by pushing the *Generate* button. Go to the build
321 directory and open the created **OpenCV** solution. Depending on just how much of the above
322 options you have selected the solution may contain quite a lot of projects so be tolerant on the
323 IDE at the startup. Now you need to build both the *Release* and the *Debug* binaries. Use the
324 drop-down menu on your IDE to change to another of these after building for one of them.
326 ![](images/ChangeBuildVisualStudio.jpg)
328 In the end, you can observe the built binary files inside the bin directory:
330 ![](images/OpenCVBuildResultWindows.jpg)
332 For the documentation, you need to explicitly issue the build commands on the *doxygen* project for
333 the HTML documentation. It will call *Doxygen* to do
334 all the hard work. You can find the generated documentation inside the `build/doc/doxygen/html`.
336 To collect the header and the binary files, that you will use during your own projects, into a
337 separate directory (similarly to how the pre-built binaries ship) you need to explicitly build
338 the *Install* project.
340 ![](images/WindowsBuildInstall.png)
342 This will create an *Install* directory inside the *Build* one collecting all the built binaries
343 into a single place. Use this only after you built both the *Release* and *Debug* versions.
345 To test your build just go into the `Build/bin/Debug` or `Build/bin/Release` directory and start
346 a couple of applications like the *contours.exe*. If they run, you are done. Otherwise,
347 something definitely went awfully wrong. In this case you should contact us at our [Q&A forum](http://answers.opencv.org/).
348 If everything is okay, the *contours.exe* output should resemble the following image (if
349 built with Qt support):
351 ![](images/WindowsQtContoursOutput.png)
354 If you use the GPU module (CUDA libraries), make sure you also upgrade to the latest drivers of
355 your GPU. Error messages containing invalid entries in (or cannot find) the nvcuda.dll are
356 caused mostly by old video card drivers. For testing the GPU (if built) run the
357 *performance_gpu.exe* sample application.
359 Set the OpenCV environment variable and add it to the systems path {#tutorial_windows_install_path}
360 =================================================================
362 First we set an environment variable to make easier our work. This will hold the build directory of
363 our OpenCV library that we use in our projects. Start up a command window and enter:
365 setx -m OPENCV_DIR D:\OpenCV\Build\x86\vc11 (suggested for Visual Studio 2012 - 32 bit Windows)
366 setx -m OPENCV_DIR D:\OpenCV\Build\x64\vc11 (suggested for Visual Studio 2012 - 64 bit Windows)
368 setx -m OPENCV_DIR D:\OpenCV\Build\x86\vc12 (suggested for Visual Studio 2013 - 32 bit Windows)
369 setx -m OPENCV_DIR D:\OpenCV\Build\x64\vc12 (suggested for Visual Studio 2013 - 64 bit Windows)
371 setx -m OPENCV_DIR D:\OpenCV\Build\x64\vc14 (suggested for Visual Studio 2015 - 64 bit Windows)
373 Here the directory is where you have your OpenCV binaries (*extracted* or *built*). You can have
374 different platform (e.g. x64 instead of x86) or compiler type, so substitute appropriate value.
375 Inside this, you should have two folders called *lib* and *bin*. The -m should be added if you wish
376 to make the settings computer wise, instead of user wise.
378 If you built static libraries then you are done. Otherwise, you need to add the *bin* folders path
379 to the systems path. This is because you will use the OpenCV library in form of *"Dynamic-link
380 libraries"* (also known as **DLL**). Inside these are stored all the algorithms and information the
381 OpenCV library contains. The operating system will load them only on demand, during runtime.
382 However, to do this the operating system needs to know where they are. The systems **PATH** contains
383 a list of folders where DLLs can be found. Add the OpenCV library path to this and the OS will know
384 where to look if he ever needs the OpenCV binaries. Otherwise, you will need to copy the used DLLs
385 right beside the applications executable file (*exe*) for the OS to find it, which is highly
386 unpleasant if you work on many projects. To do this start up again the [PathEditor](http://www.redfernplace.com/software-projects/patheditor/) and add the
387 following new entry (right click in the application to bring up the menu):
392 ![](images/PathEditorOpenCVInsertNew.png)
394 ![](images/PathEditorOpenCVSetPath.png)
396 Save it to the registry and you are done. If you ever change the location of your build directories
397 or want to try out your application with a different build, all you will need to do is to update the
398 OPENCV_DIR variable via the *setx* command inside a command window.
400 Now you can continue reading the tutorials with the @ref tutorial_windows_visual_studio_opencv section.
401 There you will find out how to use the OpenCV library in your own projects with the help of the
402 Microsoft Visual Studio IDE.