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