Home > Install > Install OpenCV on Windows – C++ / Python
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.1
OpenCV-4.5.1-vc16.exe
OpenCV-4.5.1-vc15.exe
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
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.
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.
Next, it’ll ask you to choose the installation folder. You can install OpenCV anywhere on the system. The default location is C:
.
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.
#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.
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
- Contrib modules: The official binaries do not contain
opencv_contrib
modules. The installer which we provide does. - 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.
- Size: Official binaries are larger in size. They take about 1.21GB. The installers which we provide take around 367MB.
- 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.
- 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.
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.
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 CodeSubscribe & Download 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.
-
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.
-
Download the Installer:
- Different installers are available based on the Visual Studio version.
- Examples of OpenCV versions and corresponding installers are provided.
-
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.
-
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.
-
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.
-
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.
-
Test Installation (Python):
- The article outlines commands to activate the virtual environment, start Python prompt, import and print OpenCV version, and deactivate the environment.
-
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.