How to install OpenCV for Python using Anaconda in Windows

Welcome to the OpenCV installatin guide. Before we go any further, let’s prepare our working environment and set ourselves up for the exercises we will be doing as we move along. Here we will start by downloading and installing the required software libraries and packages.

Using Anaconda, we can easily get started with OpenCV. There are many pacakages which are needed to smoothly run OpenCV in Python. Fortunately the Anaconda will install those libraries such as SciPy, Matplotlib, NumPy, etc. You don't need to install those packages one by one as many will be installed as default in Anaconda. You need to select the appropriate version of the Anaconda according to your system. If its 64-bit then you must install the 64 bit version otherwise install 32-bit version. Choose Python 3.9. So, let us get started by first installing Anaconda and then OpenCV.

Installing Anaconda

First download the Anaconda from the following website:

https://www.anaconda.com/

I personally suggest to install the Individual Edition (Distribution) which is the open-source version. You can follow the steps for installation of the Anaconda environment.

Creating the virtual environment (Optional)

This is an optional step. I generally prefer to create a virtual environment before even starting the instllation so that existing Python packages will not be affected. To create a new virtual environment, from start menu open Anaconda Navigator and from within Anaconda Navigator launch CMD.exe Prompt. In the CMD.exe write following command:

Create Virtual Environment
conda create -n NAME python=3.8 anaconda

Installing OpenCV

Again launch CMD.exe Prompt from Anaconda Navigator and enter following command to install OpenCV:

Installing OpenCV
conda install -c conda-forge opencv

I congratulate you that you completed the first step and now we can start simply doing some simple tasks related to image processing. You can start the Anaconda Navigator and launch JuypiterLab. It will open the Juyper Lab in your default browser. In my case it is Google Chrome. Right now we do not have any notebook so we can create a new notebook by clicking on the and create a new notebook for us where we can start writing our code .You can also optionally rename your first notebook by right clicking on it and selecting the rename option.


Verifying the OpenCV Installtion

You can verify the installation of the OpenCV by first importing the OpenCV in python. Type at the prompt:

Verify OpenCV
1
2
import cv2
print(cv2.__version__)

This should display the current version of OpenCV if it is correctly installed. In case of any issues you need to check again is the Anaconda and OpenCv are correctly installed or not.

Installing Numpy and Matplotlib

Both Numpy and Matplotlib are frequently used. Without numpy OpenCV will not work. Matplotlib is used to display images or graphics. Luckly, Anaconda has already installed these two packages and you don't need to install them. However, if you are not using Anaconda environment, then you must install those pacakges individually.


Installing Pythong, OpenCV, numpy, and Matplotlib Seprately

You can also install them independently. As in the world of data science and machine learning, having a solid foundation of essential software is crucial for success. These tools enable you to manipulate data, perform complex calculations, create visualizations, and even delve into computer vision tasks. In this guide, we will walk you through the step-by-step installation process of four fundamental software packages: Python, NumPy, Matplotlib, and OpenCV.

1. Python Installation

Python is the backbone of the data science world, offering a versatile and powerful programming language. Follow these steps to install Python on your system:

Download

Visit the official Python website at python.org. Choose the latest version of Python suitable for your operating system (Windows, macOS, or Linux).

Installer

Run the downloaded installer. Make sure to check the box that says "Add Python to PATH" during installation. This will allow you to use Python from the command line.

Verification

Open a command prompt (Windows) or terminal (macOS/Linux) and type the following command to ensure that Python is installed correctly. You should see the version number displayed.

Python Installation
python --version

2. NumPy Installation

NumPy is a fundamental package for scientific computing with Python. It provides support for large, multi-dimensional arrays and matrices. To install NumPy:

Package Manager

Open a command prompt or terminal and run the following command: pip install numpy

Verification

You can verify the installation by opening a Python interpreter and entering import numpy.

3. Matplotlib Installation

Matplotlib is a popular data visualization library that allows you to create a wide range of charts, graphs, and plots. To install Matplotlib:

Package Manager

In the same command prompt or terminal, run: pip install matplotlib

Verification

Open a Python interpreter again and execute import matplotlib.pyplot as plt

4. OpenCV Installation

OpenCV (Open Source Computer Vision Library) is vital for tasks involving image and video processing. Installing OpenCV might require a few additional steps due to its complexity:

Package Manager

Install OpenCV's Python package using pip by running the following command in windows powershell:

OpenCV Installation
 pip install opencv-python

Verifying OpenCV Installation

To verify the installation of OpenCV, you can follow these steps:

  • Open the Windows PowerShell.
  • Copy and paste the following command into the PowerShell window:
  • Press Enter to execute the command.
OpenCV Installation
python -c "import cv2; print('OpenCV version:', cv2.__version__)"
OpenCV Installation
1
2
3
4
5
6
7
8
import cv2
import numpy as np
path = r'F:\img\lena.jpg'
img = cv2.imread(path,cv2.IMREAD_COLOR)
ROI = img[80:170, 60:160,:]
cv2.imshow('ROI',ROI)
cv2.waitKey(0)
cv2.destroyAllWindows()

The script will output the version of OpenCV installed on your system. Make sure you have Python and OpenCV properly installed and added to your system's PATH.

Additional Dependencies

Depending on your use case, you might need additional OpenCV modules like OpenCV-contrib. You can install it by running the following command on windows powershell:

Package Manager
pip install opencv-python

Conclusion

By following this guide, you've successfully installed Python and three essential libraries for data science and machine learning: NumPy, Matplotlib, and OpenCV. These software packages provide you with the tools necessary to manipulate data, create visualizations, and explore computer vision tasks. With a solid foundation in these tools, you're well-equipped to embark on your data science journey. Happy coding!

OpenCV Tutorials