In this post, we will provide step by step instructions on how to install OpenCV 3 (C++ and Python) on Windows.
Step 1: Install Visual Studio
Download and install Visual Studio 2015 community edition from https://www.visualstudio.com/vs/older-downloads/. You can use Visual Studio 2017 as well but if you are planning to use Dlib you have to use Visual Studio 2015 as Dlib can’t be compiled using Visual Studio 2017.
Note: Screenshots posted in this article are for Visual Studio 2017. We will update screenshots soon. Meanwhile please select the options corresponding to VS 2015 wherever you see that VS 2017 is mentioned.
Run installer, select “Custom” in “type of installation”.
Now click next. It will take some time to complete the installation.
We have finished installation of Visual Studio 2015.
Step 2: Install CMake
Download and install CMake v3.8.2 from https://cmake.org/download/.
During installation select “Add CMake to system PATH”
Step 3: Install Anaconda (a python distribution)
Anaconda is a Python distribution which comes with a lot of useful Python packages. Installing Python modules on Windows can be a huge pain sometimes. Installing Anaconda is not necessary for generating OpenCV binary for Python. You can use official Python and NumPy as well.
Download and install Anaconda 64-bit version from https://www.continuum.io/downloads. You can install Anaconda 2 or Anaconda 3 or both.
While installing Anaconda make sure that you check both options:
- Add Anaconda to my PATH environment variable
- Register Anaconda as my default Python
Step 4: Download opencv and opencv_contrib
Go to https://github.com/opencv/opencv/releases and download opencv-3.2.0 source code zip
Go to https://github.com/opencv/opencv_contrib/releases and download opencv_contrib-3.2.0 source code zip
Extract both zip files. Although you can keep opencv and opencv_contrib folders anywhere, I suggest that you should keep both in same directory. I have placed these two folders in “My Documents” directory.
From hereon we will refer the path to opencv-3.2.0 folder as OPENCV_PATH. In my case OPENCV_PATH is C:/Users/Vaibhaw Chandel/Documents/opencv-3.2.0
Depending upon where you have kept opencv-3.2.0 folder, this path would be different.
Step 5: Generate Visual Studio project using CMake
Step 5.1: Configure default flags
Run Cmake GUI. In “Where is the source code” box write value of OPENCV_PATH (which is path to opencv-3.2.0 folder) and path to build directory. We will choose build directory as OPENCV_PATH/build. Click configure.
Step 5.2: Configure additional CMake flags
We will make few changes in the default configuration generated by CMake.
- Check “INSTALL_C_EXAMPLES” and “INSTALL_PYTHON_EXAMPLES”
- In flag “OPENCV_EXTRA_MODULES_PATH”, give path of modules directory within opencv_contrib-3.2.0. In our case we have kept opencv_contrib-3.2.0 in Documents folder so path is “C:/Users/Vaibhaw Chandel/Documents/opencv_contrib-3.2.0/modules”
Step 5.3: Configure Python flags
This section is only for people who want to generate OpenCV binary for both Python2 and Python 3. If you are going to use just one Python either 2 or 3, you should skip this section.
CMake found paths for Python2 but it was unable to find paths for my Python3 files.
So I manually added paths for Python3.
Now click configure again. After configuring is done, search opencv_python in search bar, both BUILD_opencv_python2 and BUILD_opencv_python3 will be automatically checked. Now we are sure that OpenCV binaries for both Python2 and Python 3 will be generated after compilation.
Step 5.4: Generate CMake files
If CMake is able to configure without any errors it should say “Configuring done”. Click generate.
This is a very important point. Whenever you make any changes(check/uncheck boxes or change path) to configuration generated by CMake, always click configure and generate.
Step 6: Compile OpenCV
Step 6.1: Open project in Visual Studio
Once CMake has generated necessary files, click on Open Project. Choose Visual Studio 2017 if it asks for which app to use to open project.
OR instead of clicking “Open Project” you can go to your build folder i.e. OPENCV_PATH/build and double click on .sln file. This will open Visual Studio.
In Visual Studio, you can build a project in different configuration settings. We will build OpenCV with two configurations:
- Debug mode – x64 platform
- Release mode – x64 platform.
Pay special attention to this. When you build solution in Visual Studio make sure that mode is either Debug or Release and platform is x64.
Step 6.2: Configuring x64 or Release mode
Now click x86 and select New from dropdown menu. In new window that appears, select x64 as “new platform” and in “copy settings from” keep x86. Click Ok.
Similarly if Release mode is not present, click Configuration Manager -> Debug -> New. Type Release in “Name” and Debug in “copy settings from”. Now we are done with creating Build Configurations in Visual Studio.
Step 6.3: Build OpenCV in debug mode
Select mode as Debug and target as x64 from the drop down menu. In Solution Explorer (a panel situated in left) under CMakeTargets find INSTALL, right-click select Build.
It may give an error for generating Python binary. You can ignore that because we will use Python binaries generated from Release mode.
Step 6.4: Build OpenCV in Release mode
Select Mode as Release and target as x64. Again right click on “INSTALL” under CMakeTargets and select Build. Once the build is completed check the log. It should look something like this:
Check - Build: 117 succeeded, 0 failed, 0 skipped
Now that we have compiled OpenCV we will find out how to configure a Visual Studio project to use OpenCV library.
Step 7: Testing C++ code
Download C++ and Python codes from here and extract them into a folder.
Step 7.1: Update System Environment Variables
First of all we will add OpenCV dll files’ path to our system PATH. Press Windows Super key, search for “environment variables”.
Click Environment Variables in System Properties window.
Under System Variables, Select Path and click edit.
Click New, and give path to OPENCV_PATH\build\install\x64\vc15\bin and click Ok. Depending upon where you have kept opencv-3.2.0 folder and what version of Visual Studio you used to compile OpenCV, this path would be different. In my case full path is:
Step 7.2: Create New Project
Now we will create a new project in Visual Studio.
In next window, within Templates select Visual C++ then “Win32 Console Application”, give Project Name and Solution Name, then click OK. I gave “RedEyeRemover” in both Project Name and Solution Name.
In next window, select C++ file and give name to this file. I have given name redEyeRemover.cpp
Step 7.3: Specify OpenCV include directories path
NOTE : Before proceeding, make sure that the configurations in the drop down menu are Debug and x64, because when we create a new project, it might open in Debug and x86 by default.
As you can see Visual Studio is showing a lot of errors (definition not found errors). This is because Visual Studio doesn’t know where OpenCV’s header files are kept. We need to add paths of OpenCV’s header files so that we can compile our project and path of OpenCV’s library files so that linker can access library files. Right click on your project and select Properties.
Under “Configuration Properties” -> C/C++ -> General, click on “Additional Include Directories”.
You can add a path by clicking on Folder icon left to Red Cross Icon. You have to add 3 paths here:
OPENCV_PATH\build\install\include OPENCV_PATH\build\install\include\opencv OPENCV_PATH\build\install\include\opencv2
In my case paths are:
C:\Users\Vaibhaw Chandel\Documents\opencv-3.2.0\build\install\include C:\Users\Vaibhaw Chandel\Documents\opencv-3.2.0\build\install\include\opencv C:\Users\Vaibhaw Chandel\Documents\opencv-3.2.0\build\install\include\opencv2
Now you will see, definition not found errors are gone in Visual Studio.
Step 7.4: Specify OpenCV libraries path
Now we will add path to library directories. Click on Linker in left panel, then Additional Library Directories.
You have to add path OPENCV_PATH\build\install\x64\vc15\lib here.
Step 7.5: Specify OpenCV libraries
Now within Linker click on Input. Here we will mention which library files we are going to use. Now in field “Additional Dependencies” we will add the names of all OpenCV library files.
In the screenshot here, you can see the first two files are named as
opencv_aruco320.lib and opencv_aruco320d.lib. First file was created in Release mode and second in Debug mode. d in second file denotes debug.
The important point to note here is that if you selected Configuration as “Debug” you should add debug library files in “Additional Dependencies” and if you selected Configuration as “Release” you should add release library files here. I have created a list of all OpenCV library files, copy them and add in “Additional Dependencies” box. 320 stands for version 3.2.0.
opencv_aruco320d.lib opencv_bgsegm320d.lib opencv_bioinspired320d.lib opencv_calib3d320d.lib opencv_ccalib320d.lib opencv_core320d.lib opencv_datasets320d.lib opencv_dnn320d.lib opencv_dpm320d.lib opencv_face320d.lib opencv_features2d320d.lib opencv_flann320d.lib opencv_fuzzy320d.lib opencv_hdf320d.lib opencv_highgui320d.lib opencv_imgcodecs320d.lib opencv_imgproc320d.lib opencv_line_descriptor320d.lib opencv_ml320d.lib opencv_objdetect320d.lib opencv_optflow320d.lib opencv_phase_unwrapping320d.lib opencv_photo320d.lib opencv_plot320d.lib opencv_reg320d.lib opencv_rgbd320d.lib opencv_saliency320d.lib opencv_shape320d.lib opencv_stereo320d.lib opencv_stitching320d.lib opencv_structured_light320d.lib opencv_superres320d.lib opencv_surface_matching320d.lib opencv_text320d.lib opencv_tracking320d.lib opencv_video320d.lib opencv_videoio320d.lib opencv_videostab320d.lib opencv_xfeatures2d320d.lib opencv_ximgproc320d.lib opencv_xobjdetect320d.lib opencv_xphoto320d.lib
Another Important Point: Since we have built OpenCV for x64 platform we will always use platform “x64” in our projects.
Step 7.6: Copy project files
Now right-click project (RedEyeRemover), click “Open Folder In File Explorer”. Copy files red_eyes.jpg, red_eyes2.jpg and haarcascade_eye.xml to this folder from the redEyeRemover project that we download and extracted. All these files should be next to redEyeRemover.cpp file.
Step 7.7: Build redEyeRemover project
We are now all set to build our application. Again make sure that mode if Debug and platform is x64 in Visual Studio. Click debug, select “Start Without Debugging”.
When it asks to build the project select “Yes”.
If the application is built correctly, you will see two image display windows. One with red-eyes and another without red-eyes.
Step 8: Testing Python code
Step 8.1: Quick check
A quick way to check whether OpenCV for Python is installed correctly or not is to import cv2 in python interpreter. Open command prompt in Windows, run python command. This will open Python interpreter. Run these two commands
import cv2 print cv2.__version__
NOTE: It’s cv2.__version__ . Underscores are not visible in the code snippet above.
Anaconda comes with a feature-rich Python interpreter called IPython. I tested these commands in IPython.
If OpenCV for Python is installed correctly, running command “import cv2” will give no errors. If any error comes up it means installation failed.
Step 8.2: Run redEyeRemover python script
Open windows command prompt, go to the folder where you have saved your Python file (removeRedEyes.py) and run
I have done the same in Windows Power Shell. You can do it in either Command Prompt or Power Shell.
If the program runs successfully, you will see two image windows, one with red-eyes and other with black eyes.
Subscribe & Download Code
If you liked this article and would like to download code (C++ and Python) and example images used in other posts in this blog, please subscribe to our newsletter. You will also receive a free Computer Vision Resource Guide. In our newsletter, we share OpenCV tutorials and examples written in C++/Python, and Computer Vision and Machine Learning algorithms and news.