OpenCV Installation

Before using OpenCV, you need to install it in your development environment first.

The method of installing OpenCV depends on the operating system and development language you use. We take installation in a Python environment as an example, because Python is one of the most commonly used programming languages for OpenCV.


1. Install OpenCV (Python version)

pip Installation

Prerequisites for installing OpenCV (Python version):

  • Python installed (Python 3.x is recommended).
  • pip (Python package management tool) installed.

Use the following commands to verify the installation:

python3 -V
pip -V

If Python is not yet installed, refer to:Python Environment Setup。

Note:Both Python 2.7.9+ and Python 3.4+ and above come with the pip tool.

Using pip to install OpenCV is the simplest and most direct method. You just need to enter the following command in the command line:

pip install opencv-python

This command will automatically download and install the Python version of OpenCV from PyPI (Python Package Index).

If you need additional modules (such as the contrib module), you can install opencv-contrib-python:

pip install opencv-contrib-python

This version contains more OpenCV modules and features, suitable for developers who need complex tasks such as deep learning and object recognition.

The opencv-python and opencv-contrib-python packages are different versions of OpenCV. They contain the core modules and extension modules respectively. Generally speaking, opencv-contrib-python includes more features and is suitable for users who need advanced applications.

If the installation speed is slow, you can switch the pip source (for example, use the Tsinghua mirror):

pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple

After installation, you can verify whether OpenCV was installed successfully with the following code:

Example

import cv2
print(cv2.__version__)

If the OpenCV version number is output (for example, 4.8.0), the installation is successful.

2. Install via Anaconda (Recommended for data science and deep learning development)

If you use Anaconda to manage your Python environment, you can easily install OpenCV with the following commands:

First, create a new virtual environment (optional):

conda create -n opencv_env python=3.x

Activate the environment:

conda activate opencv_env

Install OpenCV:

conda install -c conda-forge opencv

When installing with Anaconda, opencv will automatically handle all dependencies and can be installed smoothly in most cases.

Anaconda Related Content:Anaconda Tutorial。

Other Extensions