OpenCV Installation
Before using OpenCV, you first need to install it in your development environment.
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 is installed (Python 3.x is recommended).
- pip is installed (the Python package management tool).
Use the following command to verify the installation:
python3 -V
pip -V
If Python is not yet installed, refer to:Python Environment Setup。
**Note:** Python 2.7.9+ or Python 3.4+ and above all come with the pip tool.
Using pip to install OpenCV is the simplest and most direct method; you only need to enter the following command in the command line:
pip install opencv-python
This command automatically downloads and installs the Python version of OpenCV from PyPI (Python Package Index).
If you need additional modules (such as the contrib modules), 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 extended modules respectively. Generally speaking, opencv-contrib-python contains more features and is suitable for users who need advanced applications.
If the installation is slow, you can switch the pip source (for example, use a 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
(
cv2.version
)
If the OpenCV version number is output (for example, 4.8.0), the installation was 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 command:
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 automatically handles all dependencies and in most cases installs smoothly.
Anaconda related content:Anaconda Tutorial。