OpenCV Tutorial
Image Processing
Feature Detection and Description
Drawing Functions
Video Processing
Applications and Projects
Installing OpenCV for Python on Linux involves a few steps. Here's a tutorial to guide you through the process:
Update and Upgrade:
Before installing any new package on your Linux system, it's a good idea to update the current package list and upgrade the installed packages to their latest versions.
sudo apt update && sudo apt upgrade -y
Install Required Dependencies:
OpenCV has some dependencies that need to be installed.
sudo apt install -y cmake g++ wget unzip pkg-config libjpeg-dev libpng-dev libtiff-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libgtk-3-dev libatlas-base-dev libblas-dev liblapack-dev libeigen3-dev
Install Python Development Headers:
Depending on your Python version (whether it's Python 3.x or Python 2.x), you might need to install the development headers.
For Python 3.x:
sudo apt install -y python3-dev python3-pip
For Python 2.x:
sudo apt install -y python-dev python-pip
Install OpenCV using pip:
The simplest way to install OpenCV on Linux is to use the pip package manager.
For Python 3.x:
pip3 install opencv-python opencv-python-headless
For Python 2.x:
pip install opencv-python opencv-python-headless
Note: The opencv-python-headless
package is a lightweight version without GUI-related dependencies. This can be particularly useful in server environments.
Verify Installation:
To ensure that OpenCV has been installed correctly, you can open Python and try to import the library.
python3 # or just `python` for Python 2.x
Then, in the Python REPL:
import cv2 print(cv2.__version__)
If everything is correctly installed, this will print the version of OpenCV you installed.
Optional (For extended functionalities):
If you need additional functionalities from OpenCV, like SIFT
or SURF
, you might want to also install the opencv-contrib-python
package:
pip3 install opencv-contrib-python
That's it! You now have OpenCV installed on your Linux machine for Python. Depending on the exact functionalities and modules you need, you might need to install additional packages, but this tutorial covers the majority of standard OpenCV uses in Python.
Python OpenCV setup on Linux with pip:
pip install opencv-python
Sample code for verifying OpenCV installation on Linux:
import cv2 print(cv2.__version__)
Using virtual environments for OpenCV in Python on Linux:
# Create a virtual environment python3 -m venv myenv # Activate the virtual environment source myenv/bin/activate # Install OpenCV within the virtual environment pip install opencv-python # Verify installation python -c "import cv2; print(cv2.__version__)" # Deactivate the virtual environment deactivate
Python OpenCV installation with conda on Linux:
conda install -c conda-forge opencv