Last modified: Jan 15, 2025 By Alexander Williams

How to Install OpenCV Python: Step-by-Step Guide

OpenCV is a powerful library for computer vision and image processing. It is widely used in Python for tasks like object detection, face recognition, and more. This guide will walk you through the installation process step by step.

Prerequisites

Before installing OpenCV, ensure you have Python installed on your system. You can check this by running python --version in your terminal. If Python is not installed, download it from the official website.

You will also need pip, Python's package installer. Most Python installations come with pip pre-installed. Verify it by running pip --version.

Installing OpenCV Using pip

The easiest way to install OpenCV is using pip. Open your terminal or command prompt and run the following command:


pip install opencv-python

This command installs the core OpenCV library. For additional functionalities, you can install opencv-contrib-python:


pip install opencv-contrib-python

This package includes extra modules and features not available in the standard OpenCV package.

Verifying the Installation

After installation, verify that OpenCV is correctly installed. Open a Python shell and type the following:


import cv2
print(cv2.__version__)

If OpenCV is installed correctly, this will print the version number. For example:


4.5.5

Installing OpenCV from Source

For advanced users, installing OpenCV from source allows customization. This method is more complex but offers greater control over the build process.

First, install the necessary dependencies. On Ubuntu, you can use:


sudo apt-get update
sudo apt-get install -y build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

Next, clone the OpenCV repository:


git clone https://github.com/opencv/opencv.git
cd opencv
mkdir build
cd build

Configure the build using CMake:


cmake ..

Finally, compile and install OpenCV:


make -j4
sudo make install

Common Issues and Troubleshooting

Sometimes, you may encounter issues during installation. One common problem is missing dependencies. Ensure all required libraries are installed.

If you face errors related to cv2 import, check your Python environment. Ensure you are using the correct Python version and that OpenCV is installed in the same environment.

Using OpenCV in Your Projects

Once installed, you can start using OpenCV in your Python projects. For example, to read and display an image:


import cv2

# Load an image
image = cv2.imread('path_to_image.jpg')

# Display the image
cv2.imshow('Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

This code reads an image from the specified path and displays it in a window. Press any key to close the window.

For more advanced tasks, such as cropping images by contour or cropping by coordinates, OpenCV offers a wide range of functions.

Conclusion

Installing OpenCV in Python is straightforward with pip. For advanced users, building from source provides additional customization. Once installed, OpenCV opens up a world of possibilities in computer vision and image processing.

Start exploring OpenCV today and unlock the potential of computer vision in your projects. Whether you're working on object detection, face recognition, or image manipulation, OpenCV has the tools you need.