In this post, we will provide step by step instructions on how to install OpenCV 3 (C++ and Python) on Windows.
If you want to install OpenCV 4 from source, please check out this tutorial: Install OpenCV from source on Windows
We have used Windows Power Shell to run commands. Alternatively, you can use the command prompt too.
Step 1: Install Visual Studio
Download and install Visual Studio 2015 community edition from https://www.visualstudio.com/vs/older-downloads/. If you are finding it difficult to search for Visual Studio 2015, use this link. If you don’t have a Visual Studio Dev Essentials account, create account and login. Run installer, select “Custom” in “type of installation”.
In next screen within Programming Languages, select Visual C++ and Python tools for Visual Studio. Click next.
Now click next. It will take some time to complete the installation.
We have finished installation of Visual Studio 2015.
Note: Since Visual Studio 2017 fails to compile Dlib, we switched back to Visual Studio 2015.
Step 2: Install CMake
Download and install CMake v3.10.0 from https://cmake.org/download/.
During installation select “Add CMake to system PATH”
Step 3: Install Anaconda (a python distribution)
Download and install Anaconda 64-bit version from https://www.anaconda.com/products/individual#Downloads.
Note: Dlib ships a prebuilt binary for Python 3 and not Python 2. Building Dlib’s Python bindings from source is a hassle as you have to build Boost.Python first.
So it is advised to install Anaconda 3. In case you want to build OpenCV’s Python bindings for Python 2, you can install Anaconda 2 as well but you won’t be able to use Dlib in Python 2.
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 and extract opencv-3.3.1 and opencv_contrib-3.3.1
Go to https://github.com/opencv/opencv/releases and download opencv-3.3.1 source code zip
Go to https://github.com/opencv/opencv_contrib/releases and download opencv_contrib-3.3.1 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 the same directory. I have placed these two folders in “My Documents” directory.
NOTE : From hereon we will refer the path to opencv-3.3.1 folder as OPENCV_PATH. For example, in my case OPENCV_PATH is C:/Users/Vaibhaw Chandel/Documents/opencv-3.3.1
Depending upon where you have kept opencv-3.3.1 folder, this path would be different.
Step 5: Generate Visual Studio project using CMake
Run Cmake, in box “Where is the source code” write value of OPENCV_PATH (which is path to opencv-3.3.1 folder) and path to build directory. We will choose build directory as OPENCV_PATH/build
Now click configure.
You will be asked for permission to create the build folder. Click Yes.
When prompted to select a compiler, select Visual Studio 14 2015 Win64.
Click finish and in the next window keep the default parameters checked.
Click finish. Now CMake will look in the system directories and generate the makefiles.
Step 5.1: Additional changes to CMake config
We will make few changes in the default configuration generated by CMake. Click on the images to see a larger version.
- Check “INSTALL_C_EXAMPLES” and “INSTALL_PYTHON_EXAMPLES”
- In flag “OPENCV_EXTRA_MODULES_PATH”, give path of modules directory within opencv_contrib-3.3.1. In our case we have kept opencv_contrib-3.3.1 in Documents folder so path is “C:/Users/Vaibhaw Chandel/Documents/opencv_contrib-3.3.1/modules”
Now click configure to apply these changes.
- On Windows 10, opencv_saliency module fails to build. So we will disable it. Uncheck BUILD_opencv_saliency
Now click on configure again to include this change.
Step 5.2 : Add Python paths for both Python2 and Python3 (optional)
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 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.3 : Generate build files
If CMake is able to configure without any errors it should say “Configuring done”.
Click generate.
Note: 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:Compile opencv in Release mode
Open Windows Command Prompt (cmd).
Go to OPENCV_PATH/build directory and run this command
cmake.exe --build . --config Release --target INSTALL
Step 6.2 : Compile opencv in Debug mode
Open CMake GUI again as mentioned in Step 5.
- Search “python” in search box
- Uncheck INSTALL_PYTHON_EXAMPLES, BUILD_opencv_python3 and BUILD_opencv_python2
- Click configure
- Click generate
Now in windows command prompt
Go to OPENCV_PATH/build directory and run this command
cmake.exe --build . --config Debug --target INSTALL
Now that we have compiled OpenCV we will find out how to test a OpenCV project using CMake.
Step 7: Update System Environment Variables
Step 7.1 : Update environment variable – PATH
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\vc14\bin and click Ok. Depending upon where you have kept opencv-3.3.1 folder and what version of Visual Studio you used to compile OpenCV, this path would be different. In my case full path is:
C:\Users\Vaibhaw Chandel\Documents\opencv-3.3.1\build\install\x64\vc14\bin
Now click Ok to save. Don’t close the Environment Variables window yet. We will update OPENCV_DIR variable in next step.
Step 7.2 : Update user environment variable – OPENCV_DIR
Click New in “User Variables” (upper half of right hand side window). Under variable name write OPENCV_DIR and under variable value write OPENCV_PATH\build\install.
As you can see in my case variable value is:
C:\Users\Vaibhaw Chandel\Documents\opencv-3.3.1\build\install
This directory contains file “OpenCVConfig.cmake”. This is used by CMake to configure OpenCV_LIBS and OpenCV_INCLUDE_DIRS variables to generate project files.
Now click OK to save and close environment variables window.
Note: If you have an open Command Prompt/Power Shell window before these values were updated, you have to close and open a new Command Prompt/Power Shell window again.
Step 8: Testing C++ code
Download this redEyeRemover code and extract it into a folder.
Now open Windows Power Shell and navigate to this directory.
Create a file named CMakeLists.txt and put this code in this file.
cmake_minimum_required(VERSION 2.8)
project( redEyeRemover )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( removeRedEyes removeRedEyes.cpp )
target_link_libraries( removeRedEyes ${OpenCV_LIBS} )
This file has information about opencv’s include and library paths.
Now we will compile removeRedEyes.cpp and run it.
# create build directory
mkdir build
cd build
# create Visual Studio project files using cmake
cmake -G "Visual Studio 14 2015 Win64" ..
You can see in the screenshot below that CMake found the OpenCV on my machine.
Now we will build our application.
# build our application
cmake --build . --config Release
# once the build is complete, it will generate exe file in build\Release directory
Since our C++ code assumes that jpg files are in the current directory, we will move to directory RedEyeRemover and run removeRedEyes.exe file from there.
cd ..
.\build\Release\removeRedEyes.exe
After running the application you will see two image windows, one with red eyes and another with black eyes.
Step 9: Testing Python code
Step 9.1 : Quick check
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__)
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 9.2 : Testing redEyeRemover application
Open Windows Power Shell and navigate to directory where you have extracted RedEyeRemover.zip
Now run python code like this:
python .\removeRedEyes.py
If the program runs successfully, you will see two image windows one with red-eyes other with black eyes.