windows_install.markdown 21.7 KB
Newer Older
1 2 3 4 5 6 7 8
Installation in Windows {#tutorial_windows_install}
=======================

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](http://answers.opencv.org). We'll do our
best to help you out.

9 10
@note To use the OpenCV library you have two options: @ref tutorial_windows_install_prebuilt or
@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 13
technologies we integrate into our library. .. _Windows_Install_Prebuild:

14 15
Installation by Using the Pre-built Libraries {#tutorial_windows_install_prebuilt}
=============================================
16

M
Maksim Shabunin 已提交
17
-#  Launch a web browser of choice and go to our [page on
18
    Sourceforge](http://sourceforge.net/projects/opencvlibrary/files/opencv-win/).
M
Maksim Shabunin 已提交
19 20 21
-#  Choose a build you want to use and download it.
-#  Make sure you have admin rights. Unpack the self-extracting archive.
-#  You can check the installation at the chosen path as you can see below.
22

M
Maksim Shabunin 已提交
23
    ![](images/OpenCV_Install_Directory.png)
24

M
Maksim Shabunin 已提交
25
-#  To finalize the installation go to the @ref tutorial_windows_install_path section.
26

27 28
Installation by Making Your Own Libraries from the Source Files {#tutorial_windows_install_build}
===============================================================
29

30 31
You may find the content of this tutorial also inside the following videos:
[Part 1](https://www.youtube.com/watch?v=NnovZ1cTlMs) and [Part 2](https://www.youtube.com/watch?v=qGNWMcfWwPU), hosted on YouTube.
32 33 34 35 36 37 38

\htmlonly
<div align="center">
<iframe title="Install OpenCV by using its source files - Part 1" width="560" height="349" src="http://www.youtube.com/embed/NnovZ1cTlMs?rel=0&loop=1" frameborder="0" allowfullscreen align="middle"></iframe>
<iframe title="Install OpenCV by using its source files - Part 2" width="560" height="349" src="http://www.youtube.com/embed/qGNWMcfWwPU?rel=0&loop=1" frameborder="0" allowfullscreen align="middle"></iframe>
</div>
\endhtmlonly
39

40 41 42 43 44 45
**warning**

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

If you are building your own libraries you can take the source files from our [Git
46
repository](https://github.com/opencv/opencv.git).
47 48 49 50 51 52

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

-   An IDE of choice (preferably), or just a CC++ compiler that will actually make the binary files.
    Here we will use the [Microsoft Visual Studio](https://www.microsoft.com/visualstudio/en-us).
    However, you can use any other IDE that has a valid CC++ compiler.
53
-   [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
54 55
    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.
56
-   Git to acquire the OpenCV source files. A good tool for this is [TortoiseGit](http://code.google.com/p/tortoisegit/wiki/Download). Alternatively,
57 58 59 60 61 62 63 64
    you can just download an archived version of the source files from our [page on
    Sourceforge](http://sourceforge.net/projects/opencvlibrary/files/opencv-win/)

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 and install them on your system.

65
-   The [Python libraries](http://www.python.org/downloads/) are required to build the *Python interface* of OpenCV. For now use the
66
    version `2.7.{x}`. This is also a must if you want to build the *OpenCV documentation*.
67 68
-   [Numpy](http://numpy.scipy.org/) is a scientific computing package for Python. Required for the *Python interface*.
-   [Intel Threading Building Blocks (*TBB*)](http://threadingbuildingblocks.org/file.php?fid=77) is used inside OpenCV for parallel code
69
    snippets. Using this will make sure that the OpenCV library will take advantage of all the cores
70
    you have in your system's CPU.
71
-   [Intel Integrated Performance Primitives (*IPP*)](http://software.intel.com/en-us/articles/intel-ipp/) may be used to improve the performance
72
    of color conversion, Haar training and DFT functions of the OpenCV library. Watch out, since
73
    this is not a free service.
74
-   [Intel IPP Asynchronous C/C++](http://software.intel.com/en-us/intel-ipp-preview) is currently focused delivering Intel Graphics
75 76
    support for advanced image processing and computer vision functions.
-   OpenCV offers a somewhat fancier and more useful graphical user interface, than the default one
77
    by using the [Qt framework](http://qt.nokia.com/downloads). For a quick overview of what this has to offer, look into the
78 79
    documentations *highgui* module, under the *Qt New Functions* section. Version 4.6 or later of
    the framework is required.
80 81
-   [Eigen](http://eigen.tuxfamily.org/index.php?title=Main_Page#Download) is a C++ template library for linear algebra.
-   The latest [CUDA Toolkit](http://developer.nvidia.com/cuda-downloads) will allow you to use the power lying inside your GPU. This will
82 83
    drastically improve performance for some algorithms (e.g the HOG descriptor). Getting more and
    more of our algorithms to work on the GPUs is a constant effort of the OpenCV team.
84
-   [OpenEXR](http://www.openexr.com/downloads.html) source files are required for the library to work with this high dynamic range (HDR)
85
    image file format.
86
-   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
S
StevenPuttemans 已提交
87
    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.
M
Maksim Shabunin 已提交
88 89
-   [Doxygen](http://www.stack.nl/~dimitri/doxygen/) is a documentation generator and is the tool that will actually create the
    *OpenCV documentation*.
90 91

Now we will describe the steps to follow for a full build (using all the above frameworks, tools and
92
libraries). If you do not need the support for some of these, you can just freely skip this section.
93 94 95

### Building the library

M
Maksim Shabunin 已提交
96
-#  Make sure you have a working IDE with a valid compiler. In case of the Microsoft Visual Studio
97
    just install it and make sure it starts up.
M
Maksim Shabunin 已提交
98
-#  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
99
    options are OK.
M
Maksim Shabunin 已提交
100
-#  Download and install an up-to-date version of msysgit from its [official
101 102 103
    site](http://code.google.com/p/msysgit/downloads/list). 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.
M
Maksim Shabunin 已提交
104
-#  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.
105
    While installing, locate your msysgit (if it does not do that automatically). Follow the
106
    wizard -- the default options are OK for the most part.
M
Maksim Shabunin 已提交
107
-#  Choose a directory in your file system, where you will download the OpenCV libraries to. I
108 109 110
    recommend creating a new one that has short path and no special characters in it, for example
    `D:/OpenCV`. For this tutorial, I will suggest you do so. If you use your own path and know, what
    you are doing -- it is OK.
111
    -#  Clone the repository to the selected directory. After clicking *Clone* button, a window will
112
        appear where you can select from what repository you want to download source files
113
        (<https://github.com/opencv/opencv.git>) and to what directory (`D:/OpenCV`).
114
    -#  Push the OK button and be patient as the repository is quite a heavy download. It will take
115 116
        some time depending on your Internet connection.

117
-#  In this section, I will cover installing the 3rd party libraries.
118
    -#  Download the [Python libraries](http://www.python.org/downloads/) and install it with the default options. You will need a
119 120 121
        couple other python extensions. Luckily installing all these may be automated by a nice tool
        called [Setuptools](http://pypi.python.org/pypi/setuptools#downloads). Download and install
        again.
122 123 124 125 126 127 128 129 130 131

    -#  The easiest way to install Numpy is to just download its binaries from the [sourceforge page](http://sourceforge.net/projects/numpy/files/NumPy/).
        Make sure your download and install
        exactly the binary for your python version (so for version `2.7`).

    -#  For the [Intel Threading Building Blocks (*TBB*)](http://threadingbuildingblocks.org/file.php?fid=77)
        download the source files and extract
        it inside a directory on your system. For example let there be `D:/OpenCV/dep`. For installing
        the [Intel Integrated Performance Primitives (*IPP*)](http://software.intel.com/en-us/articles/intel-ipp/)
        the story is the same. For
132
        extracting the archives, I recommend using the [7-Zip](http://www.7-zip.org/) application.
133

M
Maksim Shabunin 已提交
134
        ![](images/IntelTBB.png)
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

    -#  For the [Intel IPP Asynchronous C/C++](http://software.intel.com/en-us/intel-ipp-preview) download the source files and set environment
        variable **IPP_ASYNC_ROOT**. It should point to
        `<your Program Files(x86) directory>/Intel/IPP Preview */ipp directory`. Here \* denotes the
        particular preview name.
    -#  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
        `D:/OpenCV/dep` directory.
    -#  Same as above with [OpenEXR](http://www.openexr.com/downloads.html).
    -#  For the [OpenNI Framework](http://www.openni.org/) you need to install both the [development
        build](http://www.openni.org/downloadfiles/opennimodules/openni-binaries/21-stable) and the
        [PrimeSensor
        Module](http://www.openni.org/downloadfiles/opennimodules/openni-compliant-hardware-binaries/32-stable).
    -#  For the CUDA you need again two modules: the latest [CUDA Toolkit](http://developer.nvidia.com/cuda-downloads) and the *CUDA Tools SDK*.
        Download and install both of them with a *complete* option by using the 32 or 64 bit setups
        according to your OS.
    -#  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](http://qt.nokia.com/downloads) page. Download the source files (not the
        installers!!!):

M
Maksim Shabunin 已提交
155
        ![](images/qtDownloadThisPackage.png)
156 157 158 159 160 161

        Extract it into a nice and short 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)).

M
Maksim Shabunin 已提交
162
        ![](images/visualstudiocommandprompt.jpg)
163 164 165 166 167 168 169 170 171 172 173 174 175 176

        Now navigate to the extracted folder and enter inside it by using this console window. You
        should have a folder containing files like *Install*, *Make* and so on. Use the *dir* command
        to list files inside your current directory. Once arrived at this directory enter the
        following command:
        @code{.bash}
        configure.exe -release -no-webkit -no-phonon -no-phonon-backend -no-script -no-scripttools
                      -no-qt3support -no-multimedia -no-ltcg
        @endcode
        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):
        @code{.bash}
        nmake
        @endcode
177
        After this set the Qt environment variables using the following command on Windows 7:
178 179 180 181 182 183 184 185 186 187 188
        @code{.bash}
        setx -m QTDIR D:/OpenCV/dep/qt/qt-everywhere-opensource-src-4.7.3
        @endcode
        Also, add the built binary files path to the system path by using the [PathEditor](http://www.redfernplace.com/software-projects/patheditor/). In our
        case this is `D:/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 and build Qt applications without using the *Qt
        Creator*. Everything is nicely integrated into Visual Studio.

M
Maksim Shabunin 已提交
189
-#  Now start the *CMake (cmake-gui)*. You may again enter it in the start menu search or get it
190 191 192 193
    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).

M
Maksim Shabunin 已提交
194
    ![](images/CMakeSelectBin.jpg)
195 196 197 198 199

    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 or 32 bit libraries.
    Select the one you use in your application development.

M
Maksim Shabunin 已提交
200
    ![](images/CMake_Configure_Windows.jpg)
201 202 203 204 205 206

    CMake will start out and based 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 or off:

M
Maksim Shabunin 已提交
207
    ![](images/CMakeBuildWithWindowsGUI.jpg)
208 209 210 211

    Select all the packages you want to use and press again the *Configure* button. For an easier
    overview of the build options make sure the *Grouped* option under the binary directory
    selection is turned on. For some of the packages CMake may not find all of the required files or
212 213
    directories. In case of these, CMake will throw an error in its output window (located at the
    bottom of the GUI) and set its field values to not found constants. For example:
214

M
Maksim Shabunin 已提交
215
    ![](images/CMakePackageNotFoundWindows.jpg)
216

M
Maksim Shabunin 已提交
217
    ![](images/CMakeOutputPackageNotFound.jpg)
218 219 220 221 222 223 224 225 226

    For these you need to manually set the queried directories or files path. After this press again
    the *Configure* button to see if the value entered by you was accepted or not. Do this until all
    entries are good and you cannot see errors in the field/value or the 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 and turning this
    option will make sure that they are categorized inside directories in the *Solution Explorer*.
    It is a must have feature, if you ask me.

M
Maksim Shabunin 已提交
227
    ![](images/CMakeBuildOptionsOpenCV.jpg)
228 229 230 231

    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
232
        will be a separate project for building the HTML and the PDF files). Note that these are not
233 234 235 236 237 238
        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
239
        build an OpenCV installer. With this, you can easily install your OpenCV flavor on other
240 241
        systems. For the latest source files of OpenCV, it generates a new project that simply
        creates a zip archive with OpenCV sources.
242 243 244 245 246 247
    -   *BUILD_SHARED_LIBS* -\> With this you can control to build DLL files (when turned on) or
        static 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
248
        you are concerned about performance, build them and run.
249 250 251
    -   *BUILD_opencv_python* -\> Self-explanatory. Create the binaries to use OpenCV from the
        Python language.

252
    Press again the *Configure* button and ensure no errors are reported. If this is the case, you
253 254 255 256 257 258
    can tell CMake to create the project files by pushing the *Generate* button. Go to the build
    directory and open the created **OpenCV** solution. Depending on just how much of the above
    options you have selected the solution may contain quite a lot of projects so be tolerant on the
    IDE at the startup. Now you need to build both the *Release* and the *Debug* binaries. Use the
    drop-down menu on your IDE to change to another of these after building for one of them.

M
Maksim Shabunin 已提交
259
    ![](images/ChangeBuildVisualStudio.jpg)
260

261
    In the end, you can observe the built binary files inside the bin directory:
262

M
Maksim Shabunin 已提交
263
    ![](images/OpenCVBuildResultWindows.jpg)
264

M
Maksim Shabunin 已提交
265
    For the documentation, you need to explicitly issue the build commands on the *doxygen* project for
266
    the HTML documentation. It will call *Doxygen* to do
M
Maksim Shabunin 已提交
267
    all the hard work. You can find the generated documentation inside the `build/doc/doxygen/html`.
268 269

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

M
Maksim Shabunin 已提交
273
    ![](images/WindowsBuildInstall.png)
274 275 276 277 278 279

    This will create an *Install* directory inside the *Build* one collecting all the built binaries
    into a single place. Use this only after you built both the *Release* and *Debug* versions.

    To test your build just go into the `Build/bin/Debug` or `Build/bin/Release` directory and start
    a couple of applications like the *contours.exe*. If they run, you are done. Otherwise,
280
    something definitely went awfully wrong. In this case you should contact us at our [Q&A forum](http://answers.opencv.org/).
281
    If everything is okay, the *contours.exe* output should resemble the following image (if
282 283
    built with Qt support):

M
Maksim Shabunin 已提交
284
    ![](images/WindowsQtContoursOutput.png)
285 286

@note
287
   If you use the GPU module (CUDA libraries), make sure you also upgrade to the latest drivers of
288 289 290 291
    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.

292
Set the OpenCV environment variable and add it to the systems path {#tutorial_windows_install_path}
293
=================================================================
294

295
First we set an environment variable to make easier our work. This will hold the build directory of
296
our OpenCV library that we use in our projects. Start up a command window and enter:
297
@code
298 299
    setx -m OPENCV_DIR D:\OpenCV\Build\x86\vc11     (suggested for Visual Studio 2012 - 32 bit Windows)
    setx -m OPENCV_DIR D:\OpenCV\Build\x64\vc11     (suggested for Visual Studio 2012 - 64 bit Windows)
300 301 302 303 304

    setx -m OPENCV_DIR D:\OpenCV\Build\x86\vc12     (suggested for Visual Studio 2013 - 32 bit Windows)
    setx -m OPENCV_DIR D:\OpenCV\Build\x64\vc12     (suggested for Visual Studio 2013 - 64 bit Windows)

    setx -m OPENCV_DIR D:\OpenCV\Build\x64\vc14     (suggested for Visual Studio 2015 - 64 bit Windows)
305
@endcode
306 307
Here the directory is where you have your OpenCV binaries (*extracted* or *built*). You can have
different platform (e.g. x64 instead of x86) or compiler type, so substitute appropriate value.
308
Inside this, you should have two folders called *lib* and *bin*. The -m should be added if you wish
309 310 311 312 313 314 315 316 317 318
to make the settings computer wise, instead of user wise.

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
libraries"* (also known as **DLL**). Inside these are stored all the algorithms and information 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 and the 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
319
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
320
following new entry (right click in the application to bring up the menu):
321
@code
322
    %OPENCV_DIR%\bin
323
@endcode
324

M
Maksim Shabunin 已提交
325
![](images/PathEditorOpenCVInsertNew.png)
326

M
Maksim Shabunin 已提交
327
![](images/PathEditorOpenCVSetPath.png)
328 329

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

333
Now you can continue reading the tutorials with the @ref tutorial_windows_visual_studio_Opencv section.
334 335
There you will find out how to use the OpenCV library in your own projects with the help of the
Microsoft Visual Studio IDE.