How to Build OpenCV for Python in Windows

Online documentation for OpenCV is spotty at best. And for many users, OpenCV’s full set of features doesn’t work in pre-compiled builds. This can be a serious barrier to entry for developers and students who want to write computer vision applications. This guide is designed to make the installation process less painful for Windows developers.

This also includes steps for building OpenCV contrib (aka OpenCV_NonFree), which contain APIs for the popular (but copyrighted) SIFT and SURF algorithms.

If you are interested in building OpenCV for C++ or Android, just skip steps 5 and 7.

My Hardware and Version Info

Operating System: Windows 10 Enterprise
Processor: Intel i7-6700K
Python Manager: Anaconda3 4.2.0 x64
Python Version: 3.5
OpenCV Version: 2.4.13
Cmake 3.6.1 x64
Git 2.9.3 x64
Visual Studios Community 2015

Required Software Downloads

  1. Anaconda for Python 3.5
  2. Cmake x64 Installer
  3. 64-bit Git for Windows Setup
  4. Visual Studios Community
A note on OpenCV version:

OpenCV is available in 3.x and 2.4.x flavors. This reflects different development branches. For the purposes of this guide, we will focus on OpenCV 3.x.

1. Install Git

Download Git for Windows. In this guide, I will be using Git Setup (non-portable) 2.9.3 64-bit version of Git for Windows.

Run Git-2.9.3-64-bit.exe and follow the installation wizard with the following options selected.

git_install_2 git_install_3 git_install_4

2. Install Visual Studios Community

You’re just installing Visual Studios for the C++ compiler and Python Tools. The other options depend on your preferences, but don’t matter much for this guide.vsc_install_1

3. Install Cmake

Install cmake-3.6.1-win64-x64.msi. Select whichever options you wish. For this guide, I installed Cmake for all users on my computer.

4. Pull the latest OpenCV Build from Github

Open Git BASH. Go to whatever directory you want to store your OpenCV files. For me, I navigated to my Development directory.

In Git Bash enter the following command:

This will create a copy of the latest OpenCV source on your local computer.

4.1 OpenCV_contrib

Then pull the latest OpenCV_contrib source. For the purposes of this guide, I recommend NOT putting your OpenCV_contrib build directory into a subdirectory of your OpenCV build directory.

5. Install Python with Anaconda

Of course you don’t need to use Anaconda. But it’s my favorite Python manager, especially in the Windows development environment. So it’s what I’ll be using in this guide.

Go to https://www.continuum.io/downloads and download the Windows 64-bit installer for Python 3.5+. As of the time this guide was written, the current version was Anaconda 4.2.0.

anaconda_install2
Although the default installation is for 1 user, I think it’s easier and more useful to install Anaconda and Python system-wide. This will mean that Anaconda’s install paths will be clean and neat, like:

anaconda_install
As you go through the installation wizard, make sure you check these 2 boxes. This makes Anaconda’s version of Python your system’s default version of Python. It also adds Anaconda to your system PATH, which makes using command line tools easier.

6. Building OpenCV

OK, here is where things start to come together. First, we are going to select our OpenCV build options with CMAKE. Then we are going to feed those CMAKE-generated build option to our Visual Studios compiler.

Aside: If you plan to do any GPU-enabled computations with OpenCV, such as Deep Learning, I highly recommend downloading and installing CUDA, CuDNN, and the related drives from Nvidia before building OpenCV.

In Git Bash, navigate to your OpenCV folder and enter the following command:

opencv_git_update
This ensures you are using the most up-to-date version of OpenCV.

Next, open up the CMAKE GUI.cmake_base
Now select Browse Source and select the folder that contains CMakeLists.txt. For me, this is just my base ../opencv/ directory. Then create a new nested directory ../opencv/builds. Now select Browse Build and select that folder you just created.

Next, hit Configure only once. You should be prompted with which compiler to use. We want to use the x64 version of the Visual Studios compiler we just installed. In our case, that’s Visual Studio 12 2013 Win64.
cmake_choosecomp1

Then, you should see something like this:
cmake_config

As an aside, these build options are easier to navigate when the Grouped flag is checked.

Now we perform a sanity check. Open up the PYTHON3 group. The paths should be automatically populated, and it should look like this:
cmake_py3
If not, then you have to manually enter each of these values. This is very important. It’s OK if your PYTHON2 paths are blank.

NOTE: If the OpenCV source you pull from Github does not already include OpenCV_contrib, you must follow the steps outlined in Section 4.1. You must then copy your <opencv_contrib_build_directory> path into the OPENCV_EXTRA_MODULES_PATH in CMake.

Aside from the defaults, I am checking:
INSTALL -> INSTALL_PYTHON_EXAMPLES
OPENCV -> OPENCV_ENABLE_NONFREE

Then, click Generate.

Now you should have a file called OpenCV.sln in ../opencv/builds. Go ahead, and open that file up with Visual Studios.

First, select ALL_BUILD. If you value your time, change your compiler from Debug to Release. Then Build.
This should take a while. Go get a coffee.

vsc_build_1
Then build INSTALL. This should also take a while. Go get another coffee.

Note: Some users have reported that ONLY build INSTALL is needed, and doing build ALL_BUILD beforehand is unnecessary. Please let me know what works for you.

If anything goes wrong, and you need to restart the build process:
1. Delete everything in your ../opencv/builds directory
2. Go to File -> Delete Cache in CMAKE
3. Start the build process over again.

7. Testing your OpenCV Build in Python

Through Anaconda, open up an instance of Spyder. Create a file called opencv_test.py. Copy and paste the following lines:

If you are able to import OpenCV and detect the SIFT-keyword functions, you should be good to go.

Good luck!


If you have found any errors, please leave a comment. I will do my best to respond as quickly as possible!

8 thoughts on “How to Build OpenCV for Python in Windows”

  1. Hi, I meet a mistake when I use VS to build the ALL BUILD item, which is “56 successful 1 failed”(cannot find “opencv_version.exe in Release folder”)
    Before that I have made the ALL BUILD builded successfully but when I build INSTALL my “red umbrella” warn me and delete one of my files, so I go over all these again and I shut down it.
    From that on, I cannot make all 57 successfuly. Could you tell me what should I do?

  2. I cannot import cv2 could you show your PYTHON3 LIBRARY path more clearly I have to manully add this path but I am not sure which one I should add.

  3. Hey Eric

    Thanks a lot for this site. With this I could build opencv for the first time (after some failed attempts with others). the only thing is: The python test script does not work in my spyder. It says after find_function() definition there should be identation. After I inserted identation it says it could not import cv2 (line 3). Do I need a path definition to cv2 in the opencv folder?

    Thank you for help!
    Frank

    1. Hi Frank and TT.
      Thanks for the nice comments.
      Yes, you need to let conda (Anaconda’s package manager) know where cv2 is.
      The easiest thing is to add cv2 to your conda search path.
      You can also bundle cv2 or any other package into a conda package.
      I don’t have my machine in front of me, but for more details see:
      https://conda.io/docs/build_tutorials/pkgs2.html
      And
      https://continuum.io/blog/developer-blog/python-packages-and-environments-conda
      Let me know if this works for you.

  4. Thanks for the tutorial! This helped more than any of the slew of other sites that I visited.

    Why didn’t you have to pull the opencv contrib file off github and put its path in the OPENCV_EXTRA_MODULES_PATH on CMake?

    Also when it came to building in Visual Studio, all I had to do was build INSTALL and then ALL_BUILD was already built when I tried to build it.¯\_(ツ)_/¯

    1. Thanks Victor!
      Yep, looks like I forgot to include the steps for OPENCV_EXTRA_MODULES. Thanks for catching that, I’ll make the correction.
      And I’m not sure if your versions are the same as mine, but INSTALL should also work. Good catch again.

    2. On second inspection, you might not need to pull the opencv contrib file off github and put its path in the OPENCV_EXTRA_MODULES_PATH on CMake if instead you set the CMAKE flag OPENCV -> OPENCV_ENABLE_NONFREE
      I assume this would only work if the version of OpenCV you pull off github already includes the OpenCV_contrib source, but is not build by default.
      Feel free to let me know which methods work for you.
      Thanks again!

Leave a Reply

Your email address will not be published. Required fields are marked *