py_setup_in_windows.markdown 6.2 KB
Newer Older
M
Maksim Shabunin 已提交
1 2 3 4 5 6 7 8 9
Install OpenCV-Python in Windows {#tutorial_py_setup_in_windows}
================================

Goals
-----

In this tutorial
    - We will learn to setup OpenCV-Python in your Windows system.

10 11
Below steps are tested in a Windows 7-64 bit machine with Visual Studio 2010 and Visual Studio 2012.
The screenshots shows VS2012.
M
Maksim Shabunin 已提交
12 13 14 15 16 17

Installing OpenCV from prebuilt binaries
----------------------------------------

-#  Below Python packages are to be downloaded and installed to their default locations.

18
    -#  [Python-2.7.x](http://www.python.org/ftp/python/2.7.13/python-2.7.13.msi).
19

20
    -#  [Numpy](https://sourceforge.net/projects/numpy/files/NumPy/1.10.2/numpy-1.10.2-win32-superpack-python2.7.exe/download).
21

22
    -#  [Matplotlib](https://sourceforge.net/projects/matplotlib/files/matplotlib/matplotlib-1.5.0/windows/matplotlib-1.5.0.win32-py2.7.exe/download) (*Matplotlib is optional, but recommended since we use it a lot in our tutorials*).
M
Maksim Shabunin 已提交
23

24
-#  Install all packages into their default locations. Python will be installed to `C:/Python27/`.
25

26
-#  After installation, open Python IDLE. Enter **import numpy** and make sure Numpy is working fine.
27 28

-#  Download latest OpenCV release from [sourceforge
M
Maksim Shabunin 已提交
29 30 31
    site](http://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.4.6/OpenCV-2.4.6.0.exe/download)
    and double-click to extract it.

32 33 34 35 36 37 38 39 40
-#  Goto **opencv/build/python/2.7** folder.

-#  Copy **cv2.pyd** to **C:/Python27/lib/site-packages**.

-#  Open Python IDLE and type following codes in Python terminal.
    @code
        >>> import cv2
        >>> print cv2.__version__
    @endcode
M
Maksim Shabunin 已提交
41 42 43 44 45 46 47 48 49

If the results are printed out without any errors, congratulations !!! You have installed
OpenCV-Python successfully.

Building OpenCV from source
---------------------------

-#  Download and install Visual Studio and CMake.

50 51 52
    -#  [Visual Studio 2012](http://go.microsoft.com/?linkid=9816768)

    -#  [CMake](http://www.cmake.org/files/v2.8/cmake-2.8.11.2-win32-x86.exe)
M
Maksim Shabunin 已提交
53 54 55

-#  Download and install necessary Python packages to their default locations

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    -#  [Python 2.7.x](http://python.org/ftp/python/2.7.5/python-2.7.5.msi)

    -#  [Numpy](http://sourceforge.net/projects/numpy/files/NumPy/1.7.1/numpy-1.7.1-win32-superpack-python2.7.exe/download)

    -#  [Matplotlib](https://downloads.sourceforge.net/project/matplotlib/matplotlib/matplotlib-1.3.0/matplotlib-1.3.0.win32-py2.7.exe)
        (*Matplotlib is optional, but recommended since we use it a lot in our tutorials.*)

    @note In this case, we are using 32-bit binaries of Python packages. But if you want to use
    OpenCV for x64, 64-bit binaries of Python packages are to be installed. Problem is that, there
    is no official 64-bit binaries of Numpy. You have to build it on your own. For that, you have to
    use the same compiler used to build Python. When you start Python IDLE, it shows the compiler
    details. You can get more [information here](http://stackoverflow.com/q/2676763/1134940). So
    your system must have the same Visual Studio version and build Numpy from source.

    @note Another method to have 64-bit Python packages is to use ready-made Python distributions
    from third-parties like [Anaconda](http://www.continuum.io/downloads),
    [Enthought](https://www.enthought.com/downloads/) etc. It will be bigger in size, but will have
    everything you need. Everything in a single shell. You can also download 32-bit versions also.

-#  Make sure Python and Numpy are working fine.
M
Maksim Shabunin 已提交
76 77 78

-#  Download OpenCV source. It can be from
    [Sourceforge](http://sourceforge.net/projects/opencvlibrary/) (for official release version) or
79
    from [Github](https://github.com/opencv/opencv) (for latest source).
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
-#  Extract it to a folder, opencv and create a new folder build in it.
-#  Open CMake-gui (*Start \> All Programs \> CMake-gui*)
-#  Fill the fields as follows (see the image below):

    -#  Click on **Browse Source...** and locate the opencv folder.

    -#  Click on **Browse Build...** and locate the build folder we created.

    -#  Click on **Configure**.

        ![image](images/Capture1.jpg)

    -#  It will open a new window to select the compiler. Choose appropriate compiler (here,
        Visual Studio 11) and click **Finish**.

        ![image](images/Capture2.png)

    -#  Wait until analysis is finished.
M
Maksim Shabunin 已提交
98 99 100 101 102 103 104 105 106 107 108

-#  You will see all the fields are marked in red. Click on the **WITH** field to expand it. It
    decides what extra features you need. So mark appropriate fields. See the below image:

    ![image](images/Capture3.png)

-#  Now click on **BUILD** field to expand it. First few fields configure the build method. See the
    below image:

    ![image](images/Capture5.png)

109
-#  Remaining fields specify what modules are to be built. Since GPU modules are not yet supported
M
Maksim Shabunin 已提交
110 111 112 113 114
    by OpenCV-Python, you can completely avoid it to save time (But if you work with them, keep it
    there). See the image below:

    ![image](images/Capture6.png)

115
-#  Now click on **ENABLE** field to expand it. Make sure **ENABLE_SOLUTION_FOLDERS** is unchecked
M
Maksim Shabunin 已提交
116 117 118 119
    (Solution folders are not supported by Visual Studio Express edition). See the image below:

    ![image](images/Capture7.png)

120
-#  Also make sure that in the **PYTHON** field, everything is filled. (Ignore
M
Maksim Shabunin 已提交
121 122 123 124
    PYTHON_DEBUG_LIBRARY). See image below:

    ![image](images/Capture80.png)

125 126 127
-#  Finally click the **Generate** button.

-#  Now go to our **opencv/build** folder. There you will find **OpenCV.sln** file. Open it with
M
Maksim Shabunin 已提交
128
    Visual Studio.
129 130 131 132

-#  Check build mode as **Release** instead of **Debug**.

-#  In the solution explorer, right-click on the **Solution** (or **ALL_BUILD**) and build it. It
M
Maksim Shabunin 已提交
133
    will take some time to finish.
134 135

-#  Again, right-click on **INSTALL** and build it. Now OpenCV-Python will be installed.
M
Maksim Shabunin 已提交
136 137 138

    ![image](images/Capture8.png)

139
-#  Open Python IDLE and enter import cv2. If no error, it is installed correctly.
M
Maksim Shabunin 已提交
140 141 142 143 144 145 146 147 148 149

@note We have installed with no other support like TBB, Eigen, Qt, Documentation etc. It would be
difficult to explain it here. A more detailed video will be added soon or you can just hack around.

Additional Resources
--------------------

Exercises
---------

150 151
If you have a windows machine, compile the OpenCV from source. Do all kinds of hacks. If you meet
any problem, visit OpenCV forum and explain your problem.