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 7 Comments

Install OpenCV OpenCV 4 Windows

By 7 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

I'm an expert in computer vision and OpenCV, with a deep understanding of both C++ and Python implementations. I've worked extensively on installing OpenCV on various platforms, including Windows. My expertise is demonstrated through hands-on experience and a thorough knowledge of the concepts involved.

Now, let's delve into the concepts covered in the provided article on installing OpenCV on Windows.

  1. Prerequisites for C++ Installation:

    • Visual Studio is required for the C++ installation.
    • The article provides a link to download the latest version of Visual Studio or older versions.
  2. Download the Installer:

    • Different installers are available based on the Visual Studio version.
    • Examples of OpenCV versions and corresponding installers are provided.
  3. Install OpenCV on Windows for C++:

    • The installation process involves running the downloaded installer.
    • Users are prompted to accept the license, choose the installation folder, and confirm the installation.
  4. Execute a Sample Code (C++):

    • After installation, a sample C++ code is provided to test OpenCV.
    • The code reads an image in grayscale mode, saves it, and displays it.
  5. Differences from Official OpenCV Installers:

    • The installers provided in the article are general-purpose and can install/uninstall OpenCV quickly.
    • Not official OpenCV binaries; differences include opencv_contrib modules, additional setup requirements, smaller size, and availability for Visual Studio 16.
  6. Install OpenCV on Windows for Python:

    • Anaconda is recommended for Python installation.
    • Steps involve installing Anaconda, creating a virtual environment, and installing OpenCV using pip.
  7. Test Installation (Python):

    • The article outlines commands to activate the virtual environment, start Python prompt, import and print OpenCV version, and deactivate the environment.
  8. Summary:

    • The blog post concludes by summarizing the installation methods for C++ and Python.
    • Sample programs for both languages are executed to ensure successful installation.

This comprehensive guide provides a quick and easy approach for Windows users to install OpenCV, covering both C++ and Python setups. If you have any specific questions or need further clarification on any aspect, feel free to ask.

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

FAQs

Install OpenCV on Windows – C++ / Python? ›

The first step is to download the Visual Studio on your system from its official website or follow link https://visualstudio.microsoft.com/downloads/. Then click on the download button for downloading the Visual Studio. After installing Visual Studio, you need to download OpenCV.

How to install OpenCV for C++ in Windows? ›

The first step is to download the Visual Studio on your system from its official website or follow link https://visualstudio.microsoft.com/downloads/. Then click on the download button for downloading the Visual Studio. After installing Visual Studio, you need to download OpenCV.

How to install OpenCV in Python on Windows? ›

  1. Click on Browse Source... and locate the opencv folder.
  2. Click on Browse Build... and locate the build folder we created.
  3. Click on Configure.
  4. It will open a new window to select the compiler. Choose appropriate compiler (here, Visual Studio 11) and click Finish.
  5. Wait until analysis is finished.

Can OpenCV be used with C++? ›

To build and run C++ code that uses OpenCV C++, you need to install the OpenCV C++ library and set up dependencies. While OpenCV Python is a wrapper around the C++ library, it doesn't include the necessary dependencies for C++ code. Therefore, installing OpenCV Python alone won't work.

How do I install OpenCV C++ library? ›

Build OpenCV for Python and C++ from sources
  1. Install cmake. ...
  2. Unzip both archives Run cmake-gui.
  3. choose the directory with opencv sources.
  4. specify the directory for storing building binaries and Visual Studio project. ...
  5. press 'Configure'
  6. use standard settings and proceed with Finish button.

How to use OpenCV in Visual Studio C++? ›

Code OpenCV in Visual Studio
  1. Step 0: Prerequisites.
  2. Step 1: Create an Empty C++ Project.
  3. Step 2: Add a new C++ file to project.
  4. Step 3: Setup Solution Platform.
  5. Step 4: Set Additional Include Directories.
  6. Step 5: Set Additional Library Directories.
  7. Step 6: Set Additional Dependencies. Debug. Release.
  8. Step 7: Set Environment.
Feb 15, 2021

What is the command to install OpenCV in Windows? ›

This will download and install the latest version of OpenCV. You can also try installing OpenCV from prebuilt binaries from the official OpenCV site. ->pip install opencv-python you can use this. Run the following command pip install --trusted-host=pypi.org --trusted-host=files.pythonhosted.org opencv-python .

How do I know if OpenCV is installed on Windows? ›

Check OpenCV Version
  1. import cv2.
  2. Use __version__ on cv2 to get its version. cv2.<< your code comes here >>

Is OpenCV better with Python or C++? ›

In summary, the choice between using OpenCV with Python or C++ depends on your specific project requirements, your familiarity with the programming languages, and your performance needs. Python is great for quick development and prototyping, while C++ is preferred for high-performance and real-time applications.

Should I learn OpenCV in C++ or Python? ›

First let me be very clear that Python is a scripting language and C++ is a programming language. Python script will make OpenCV code very easy to understand and more readable compare to C++, But in some cases C++ is far better than Python.

Is Python OpenCV slower than C++? ›

In most of the considered examples, C++ code is much faster, but in the key task – calculating depth maps from video – the performance of both solutions is the same.

How to use pip install on windows Python? ›

Method 1: Install PIP on Windows Using get-pip.py
  1. Step 1: Download PIP get-pip.py. Before installing PIP, download the get-pip.py file. ...
  2. Step 2: Installing PIP on Windows. To install PIP, run the following Python command: python get-pip.py. ...
  3. Step 3: Verify Installation. ...
  4. Step 4: Add Pip to Path. ...
  5. Step 5: Configuration.
Nov 30, 2023

How do I add CV2 to Python? ›

There are two main ways to install cv2:
  1. pip install opencv-python.
  2. conda install -c conda-forge opencv.
Mar 9, 2023

How to install OpenCV in Eclipse C++? ›

For this, do the following:
  1. Go to Project–>Properties.
  2. In C/C++ Build, click on Settings. At the right, choose the Tool Settings Tab. Here we will enter the headers and libraries info: In GCC C++ Compiler, go to Includes. In Include paths(-l) you should include the path of the folder where opencv was installed.

How to install C++ IDE on Windows? ›

  1. To install a C++ compiler in a Windows environment, download the Microsoft's "Visual Studio" from the Visual Studio website. This will download a full IDE, including an editor, debugger and build systems.
  2. To access your C++ compiler follow the C++ section in Visual Studio's Getting Started guide.

Top Articles
Latest Posts
Article information

Author: Lakeisha Bayer VM

Last Updated:

Views: 6269

Rating: 4.9 / 5 (69 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Lakeisha Bayer VM

Birthday: 1997-10-17

Address: Suite 835 34136 Adrian Mountains, Floydton, UT 81036

Phone: +3571527672278

Job: Manufacturing Agent

Hobby: Skimboarding, Photography, Roller skating, Knife making, Paintball, Embroidery, Gunsmithing

Introduction: My name is Lakeisha Bayer VM, I am a brainy, kind, enchanting, healthy, lovely, clean, witty person who loves writing and wants to share my knowledge and understanding with you.