Install OpenCV on Windows – C++ / Python (2024)

Home > Install > Install OpenCV on Windows – C++ / Python

Install OpenCV on Windows – C++ / Python (1) Labhesh Valechha

February 8, 2021 8 Comments

Install OpenCV OpenCV 4 Windows

By 8 Comments

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.

Table of 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

Master Generative AI for CV

Get expert guidance, insider tips & tricks. Create stunning images, learn to fine tune diffusion models, advanced Image editing techniques like In-Painting, Instruct Pix2Pix and many more

Step 3: Install OpenCV on Windows

Once you download the installer, double click it to run the installer. Before the installer starts, it’ll ask you permission to run the executable. Click on More info and then on Run anyway.

Install OpenCV on Windows – C++ / Python (2)
Install OpenCV on Windows – C++ / Python (3)

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

Install OpenCV on Windows – C++ / Python (4)

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

Install OpenCV on Windows – C++ / Python (5)

Next, it’ll ask you to choose the installation folder. You can install OpenCV anywhere on the system. The default location is C:.

Install OpenCV on Windows – C++ / Python (6)

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

Install OpenCV on Windows – C++ / Python (7)
Install OpenCV on Windows – C++ / Python (8)

After OpenCV is installed, you can exit the installer.

Install OpenCV on Windows – C++ / Python (9)

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.

You can download the code using this link.

#include <opencv2/opencv.hpp>using namespace cv;int main(void) {// Read image in GrayScale modeMat image = imread("boy.jpg", 0);// Save grayscale imageimwrite("boyGray.jpg", image);// To display the imageimshow("Grayscale Image", image);waitKey(0);return 0;}

Download CodeTo easily follow along this tutorial, please download code by clicking on the button below. It’s FREE!

Download Code

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.

mkdir build cd buildcmake -G "Visual Studio 16 2019" ..cmake --build . --config Releasecd ...\build\Release\sampleCode.exe

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.

Install OpenCV on Windows – C++ / Python (11)
Install OpenCV on Windows – C++ / Python (12)

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

  1. Contrib modules: The official binaries do not contain opencv_contrib modules. The installer which we provide does.
  2. Additional setup: The official binaries require additional setup, i.e., editing the environment variables, and using the correct folder address. Our installers will not require additional setup.
  3. Size: Official binaries are larger in size. They take about 1.21GB. The installers which we provide take around 367MB.
  4. List of libraries: If you code in Visual Studio, we have provided you with the list of libraries, in Debug and Release mode, as a separate text file. It is available in the root directory of the installation.
  5. No availability for VS16: The official binaries are not available for VS16, only for VS14 and VS15. Our installers are created for VS14, VS15, and VS16.

Install OpenCV on Windows for Python

Step 1: Install Anaconda for Python 3

Download and install Anaconda Python 3 version from Anaconda’s download page.

Install OpenCV on Windows – C++ / Python (13)

While installing Anaconda make sure that you check both options:

  • Add Anaconda to my PATH environment variable
  • Register Anaconda as my default Python

Step 2: Create a Virtual Environment

We will use Virtual Environment to install Python libraries. It is generally a good practice in order to separate your project environment and global environment.

Open the command prompt or Power shell and execute the following command.

conda create --name virtualenv python=3.8

Step 3: Install OpenCV on Windows

Activate the environment we created just now (virtualenv) and install all the required libraries using the commands below.

conda activate virtualenvpip install opencv-contrib-python

And that’s it. OpenCV has been installed on your system. To close the virtual environment, use conda deactivate.

Step 4: Test Installation

Next, we will check whether the installation was successful. From the command prompt execute the following commands.

# activate environmentconda activate virtualenv# start python promptpython# import cv2 and print versionimport cv2print(cv2.__version__)# If OpenCV is installed correctly, the above command should output OpenCV version.# Exit and deactivate environmentexit()conda deactivate

Summary

In this blog post, we installed OpenCV on Windows with the quickest and easiest method. For C++, we used a simple .exe installer and installed in under 30 seconds. For Python, we used Anaconda as the package manager and installed OpenCV in a virtual environment. We also executed sample programs for both, C++ and Python, to test the installation.This concludes the OpenCV installation.

Subscribe & Download Code

If you liked this article and would like to download code (C++ and Python) and example images used in this post, please click here. Alternately, sign up to 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.

Download Example Code

Install OpenCV on Windows – C++ / Python (2024)
Top Articles
Latest Posts
Article information

Author: Saturnina Altenwerth DVM

Last Updated:

Views: 6201

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Saturnina Altenwerth DVM

Birthday: 1992-08-21

Address: Apt. 237 662 Haag Mills, East Verenaport, MO 57071-5493

Phone: +331850833384

Job: District Real-Estate Architect

Hobby: Skateboarding, Taxidermy, Air sports, Painting, Knife making, Letterboxing, Inline skating

Introduction: My name is Saturnina Altenwerth DVM, I am a witty, perfect, combative, beautiful, determined, fancy, determined person who loves writing and wants to share my knowledge and understanding with you.