Opencv how to install windows

Install OpenCV on Windows – C++ / Python

February 8, 2021 By Leave a Comment

In this blog post, we will be installing OpenCV on Windows for C++ and Python. The C++ installation is done with the help of custom setup exe files. Whereas Python installation is done with Anaconda.

Installing OpenCV from source takes up a lot of time. Depending on your hardware, and the installation configuration, it can take anywhere from 5 minutes up to 2 hours.

Moreover, the installation is not a cakewalk. Hence, we have come up with a solution for Windows users – OpenCV Windows installers. These installers will only work for installing OpenCV for C++. If you want to install OpenCV for Python, you’ll find the information later in the blog.

If you want to install OpenCV 4 on Windows from source, then check out this blog post.

Contents

Install OpenCV on Windows for CPP

Step 1: Prerequisites

You need Visual Studio pre-installed on your system. You can download the latest version of Visual Studio from here. Otherwise, you can find older versions from here.

Step 2: Download the Installer

Once you have set up Visual Studio on your system, download the installer according to the Visual Studio version you have installed.

OpenCV Version Visual Studio 16 Visual Studio 15 Visual Studio 14
OpenCV-4.5.0 OpenCV-4.5.0-vc16.exe OpenCV-4.5.0-vc15.exe OpenCV-4.5.0-vc14.exe
OpenCV-4.4.0 OpenCV-4.4.0-vc16.exe OpenCV-4.4.0-vc15.exe OpenCV-4.4.0-vc14.exe
OpenCV-4.1.0 OpenCV-4.1.0-vc16.exe OpenCV-4.1.0-vc15.exe OpenCV-4.1.0-vc14.exe

Step 3: Install OpenCV on Windows

The installer starts with a welcome screen. Click on Next to read the License.

If you accept the license, click on I accept the agreement and click on Next.

Finally, the installer will ask you for confirmation to install OpenCV on the system. Click on Install to continue.

After OpenCV is installed, you can exit the installer.

Step 4: Execute a sample code

Once the OpenCV installation is completed, you can start coding right away. We have provided with a sample code to test the installation. Below is the code snippet.

If you plan on executing C++ code using Cmake, then download and install from here (windows win64-x64 Installer), and follow instructions below. The third line specifies the Visual Studio version on your system.

The above code reads an input image as grayscale, writes it to the disk, and displays it. Following are input and output to the code.

How are the installers different from Official OpenCV Installers for Windows

These are general-purpose installers, which can install and uninstall OpenCV in under 15 seconds. It’s a quick and clean approach to install OpenCV for your Windows system. You can test out the latest OpenCV version and see if it’s for you. Otherwise, you can install OpenCV without configuring and waiting for hours on end for it to install.

Although the installers are useful for new users, or users who want one-click install, or simply for the ones who struggle with the installation, but these are not the official installers. You can find the official OpenCV binaries from here. There are differences between the installers which we provide and the OpenCV official binaries

Install OpenCV 3 on Windows

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.continuum.io/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:

  1. Add Anaconda to my PATH environment variable
  2. 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.

  1. Check “INSTALL_C_EXAMPLES” and “INSTALL_PYTHON_EXAMPLES”
  2. 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.

  1. 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

Step 6.2 : Compile opencv in Debug mode

Open CMake GUI again as mentioned in Step 5.

  1. Search “python” in search box
  2. Uncheck INSTALL_PYTHON_EXAMPLES, BUILD_opencv_python3 and BUILD_opencv_python2
  3. Click configure
  4. Click generate

Now in windows command prompt
Go to OPENCV_PATH/build directory and run this command

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.

This file has information about opencv’s include and library paths.

Now we will compile removeRedEyes.cpp and run it.

You can see in the screenshot below that CMake found the OpenCV on my machine.

Now we will build our application.

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.

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

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:

If the program runs successfully, you will see two image windows one with red-eyes other with black eyes.

Читайте также:  Как полностью откатить windows 10 до заводских настроек
Оцените статью