Installing OpenCV on Linux Mint
OpenCV is a powerful tool that allows developers and researchers to build applications that can process and analyze images and videos in various ways.
In this tutorial, we'll walk through the steps to install OpenCV on your Linux Mint system. We'll cover everything from the prerequisites to the actual installation process, and finally, how to verify that OpenCV has been installed correctly. We'll also discuss some common pitfalls and how to avoid them.
Prerequisites
Before we dive into the installation, ensure that your system is up to date and that you have the necessary development tools installed. You'll need the following:
- GCC and G++: The GNU Compiler Collection for compiling C and C++ code.
- CMake: A cross-platform build system that OpenCV uses for configuration.
- Python (optional): If you want to use OpenCV with Python, make sure you have Python installed.
- pip (optional): Python's package installer, if you're planning to install the Python bindings for OpenCV.
To update your system and install these prerequisites, open a terminal and run the following commands:
sudo apt update
sudo apt upgrade
sudo apt install build-essential cmake
sudo apt install python3 python3-pip python3-dev
Step 1: Install Dependencies
OpenCV relies on several external libraries to function correctly. To install these dependencies, execute the following command:
sudo apt install libjpeg-dev libpng-dev libtiff-dev
sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt install libxvidcore-dev libx264-dev
sudo apt install libgtk-3-dev
sudo apt install libatlas-base-dev gfortran
Step 2: Download OpenCV
You can download the latest version of OpenCV from the official repository. We'll clone the repository using git
. If you don't have git
installed, you can install it using:
sudo apt install git
Now, let's clone the OpenCV and OpenCV contrib repositories:
cd ~
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
Step 3: Compile OpenCV
We'll use CMake to configure the build process. Create a build directory and navigate into it:
mkdir ~/opencv/build
cd ~/opencv/build
Now, run CMake with the path to the OpenCV source code and the OpenCV contrib modules:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
Once CMake has finished configuring, compile OpenCV using make
:
make -j$(nproc)
The -j$(nproc)
flag tells make
to use all available CPU cores to speed up the compilation process.
Step 4: Install OpenCV
After the compilation is complete, you can install OpenCV:
sudo make install
sudo ldconfig
Step 5: Verify the Installation
To verify that OpenCV has been installed correctly, you can run the following command to display the version of OpenCV:
pkg-config --modversion opencv4
You should see the version number of OpenCV printed in the terminal.
For Python users, you can verify the installation by running a Python shell and importing the cv2
module:
import cv2
print(cv2.__version__)
If everything is set up correctly, this will print the version of OpenCV installed for Python.
Common Pitfalls and Tips
- Permissions: Make sure you use
sudo
where necessary, especially during the installation step. - Python Virtual Environments: If you're using Python virtual environments, ensure that you activate the correct environment before installing the
opencv-python
package. - CMake Configuration: Pay close attention to the CMake configuration flags. They can be adjusted based on your needs (e.g., enabling/disabling certain modules or examples).
- Build Time: The build process can take a significant amount of time, especially if you're not using the
-j$(nproc)
flag withmake
.
You have now successfully installed OpenCV on your Linux Mint system. With this powerful library at your disposal, you can start developing computer vision applications right away.