OpenCV Installation
OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library.
OpenCV Features
-
High Performance
Optimized with C++ under the hood, it provides interfaces for Python, Java, and other languages, balancing speed and usability. Supports GPU acceleration (CUDA, OpenCL).
-
Cross-Platform
Supports Windows, Linux, macOS, Android, and iOS, and can run on various hardware architectures including x86 and ARM.
-
Multi-language Support
Offers interfaces for C++, Python, Java, and more, catering to different development needs. Compatible with deep learning frameworks like TensorFlow and PyTorch.
-
Rich Functionality
OpenCV provides a wide range of computer vision algorithms, including image processing, feature detection, object detection, and machine learning.
Installing OpenCV
This guide covers two different methods to install OpenCV: binary installation and source code compilation. Choose the one that best suits your needs.
Installation Method | Advantages | Disadvantages | Best For |
---|---|---|---|
Binary Installation | - Quick and easy installation - No need to compile from source - Easy to maintain and upgrade | - May lack some features - Less customizable | Beginners, daily development, Python users |
Source Installation | - Customizable compilation parameters and modules - Access to latest/specific versions - Supports multiple languages | - Complex process - Requires build environment - Time-consuming compilation | Advanced users, C++ developers, special requirements |
Binary Installation
You can install the Python version of OpenCV using pip
, or the C++ version using apt
.
- Python
- C++
pip3 install opencv-python opencv-contrib-python
- Verify OpenCV Installation
Open a terminal and enter the Python interactive mode by typing python3
.
python3
Once in the Python interactive mode, enter the following commands to verify OpenCV installation:
import cv2
print(cv2.__version__)
If the OpenCV version number is displayed, the installation was successful.
sudo apt update
sudo apt install libopencv-dev
- Verify OpenCV Installation
In the terminal, run the following command to verify the OpenCV installation:
pkg-config --modversion opencv4
If the OpenCV version number is displayed, the installation was successful.
Source Installation
This method involves compiling OpenCV from source.
Install Build Dependencies
sudo apt update
sudo apt install cmake gcc g++ python3-dev python3-numpy libavcodec-dev libavformat-dev libswscale-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libgtk-3-dev -y
Install Optional Dependencies
sudo apt install libpng-dev libjpeg-dev libopenexr-dev libtiff-dev libwebp-dev -y
Download OpenCV Source Code
Use git
to download the OpenCV source code.
sudo apt install git -y
git clone https://github.com/opencv/opencv.git
Compile OpenCV
Navigate to the OpenCV source directory and run the following commands to compile OpenCV:
cd opencv
mkdir build
cd build
cmake ../
make -j4
sudo make install
Verify OpenCV Installation
Open a terminal and enter the Python interactive mode by typing python3
.
python3
Once in the Python interactive mode, enter the following commands to verify OpenCV installation:
import cv2
print(cv2.__version__)
If the OpenCV version number is displayed, the installation was successful.