Install OpenCV in Ubuntu for Python and C++

Install OpenCV in Ubuntu easily using apt for quick setup or build from source for customization. Verify installation using Python or C++ commands.

🤖AI Overview:

Installing OpenCV in Ubuntu enables powerful computer vision capabilities for Python and C++ development. Use Ubuntu’s package manager for a fast setup or build from source to access the latest features. Verify the installation by checking the OpenCV version to ensure it is working properly.

Prerequisites to Install OpenCV in Ubuntu 20.04 & 22.04

To Install OpenCV in Ubuntu for Python and C++, your machine needs to meet all the below specifications:

  • A Linux VPS running Ubuntu.
  • A non-root user with sudo privileges.
  • Access to Terminal.
  • A text editor (e.g., Vim or Nano, or a graphical editor like Gedit)

Method 1: Install OpenCV from Ubuntu Repositories (Quick Setup)

This method is the quickest way to get started but might not provide the latest OpenCV version or specific build options.

Step 1: Update package lists

OpenCV is available for installation from the default Ubuntu 20.04 repositories. To install OpenCV Ubuntu, run the commands below:

sudo apt update
sudo apt install libopencv-dev python3-opencv

This will install OpenCV packages.

Step 2: Verify installation

To make sure the OpenCV installation Ubuntu process was successful, run:

python3 -c "import cv2; print(cv2.__version__)"

Once the output prints the latest version, you are done here.

Method 2: Install OpenCV in Ubuntu from the Source

This method is recommended if you consider customization and the latest features.

Follow the below steps to install OpenCV in Ubuntu for Python and C++ through this method:

Step 1: Install dependencies

To install dependencies and build tools, run:

sudo apt install build-essential cmake git pkg-config libgtk-3-dev \
    libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
    libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev \
    gfortran openexr libatlas-base-dev python3-dev python3-numpy \
    libtbb2 libtbb-dev libdc1394-22-dev libopenexr-dev \
    libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev

Step 2: Clone OpenCV and OpenCV Contrib repositories:

To pass this step, type:

mkdir ~/opencv_build && cd ~/opencv_build
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

Step 3:  Create build directory and configure CMake:

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_build/opencv_contrib/modules \
    -D BUILD_EXAMPLES=ON ..

For CMake errors, refer to the official CMake documentation. Ensure correct library paths and consider using environment variables if necessary.

For GPU acceleration, consider installing CUDA and cuDNN and enabling relevant options during CMake configuration.

To speed up the compilation process, you can use the following command:

make -j<number_of_cores>

Replace <number_of_cores> with the actual number of cores on your system.

After installing OpenCV from source, you need to update the shared library cache:

sudo ldconfig

This command ensures that the system can find the newly installed OpenCV libraries.

Step 4: Build and install OpenCV:

Use the below command to let the compilation process start which may take several minutes (About an hour) depending on your system configuration:

make -j8

The -j8 flag in the make command specifies the number of parallel jobs for building. Adjust it based on your system’s core count.

Step 5: Install OpenCV

sudo make install

Step 6: Verify installation:

To verify installation, run the commands below and check the OpenCV version:

For C++:

pkg-config --modversion opencv4

For Python:

python3 -c "import cv2; print(cv2.__version__)"

How to Update OpenCV to a Newer Version?

If installed using apt (Method 1), update the package lists and upgrade OpenCV.

For source-built versions (Method 2), rebuild from the latest source code.

How to Uninstall OpenCV in Ubuntu

After OpenCV Ubuntu Setup is finished, you can remove it anytime you no longer need it.

According to your installation method, you can uninstall OpenCV from your Ubuntu system:

Uninstalling OpenCV Installed via apt

If you installed OpenCV using the apt package manager, you can uninstall it using the following command:

sudo apt purge libopencv-dev python3-opencv

The purge option removes the package and its configuration files.

Uninstalling OpenCV Installed from Source

If you built OpenCV from source and installed it to /usr/local, you can try the following steps:

Remove the build directory:

rm -rf ~/opencv_build

Uninstall using make Uninstall:

cd ~/opencv/build
sudo make uninstall

Note: This might not remove all files.

Removing Remaining Files Manually

Manually removing files can be risky. If you’re unsure, consider using a package manager like apt for installation and removal.

However, the following command removes OpenCV manually from Ubuntu:

sudo find / -name "*opencv*" -exec rm -rf {} \;

Note: This command will remove all files containing “opencv” in their name. Use it with extreme care.

What are the best Advantages of OpenCV in Ubuntu?

Rich ecosystem: Ubuntu provides a strong foundation with essential tools and libraries for computer vision development.

Hardware acceleration: Utilize powerful GPUs for faster image processing and deep learning tasks.

Active community: Benefit from a large and supportive community for troubleshooting and knowledge sharing.

Open-source flexibility: Customize and extend OpenCV to fit specific project requirements.

Why compilation errors are shown during OpenCV installation?

Common causes include missing dependencies, incorrect CMake configuration, insufficient system resources, or compiler issues.

Check the error messages carefully for clues.

How to resolve OpenCV library path issues?

Ensure the OpenCV libraries are in the system’s library path. You might need to set environment variables or update LD_LIBRARY_PATH.

Conclusion

This guide presented two effective methods to install OpenCV in Ubuntu: a quick installation using Ubuntu’s package manager and a customizable build from source. Beginners may prefer the package installation for its simplicity, while advanced users needing the latest features and control will benefit from compiling OpenCV themselves. By following these steps and verifying the installation, you can start building powerful computer vision applications on Ubuntu with OpenCV.

FAQ

You can quickly install from Ubuntu repositories using apt or build from source to customize and get the latest features.

Run "sudo apt update" then "sudo apt install libopencv-dev python3-opencv" in the terminal.

For Python, run "python3 -c "import cv2; print(cv2.__version__)""; for C++, use "pkg-config --modversion opencv4".

Installing from source allows customization, enabling features like GPU support and accessing the latest OpenCV versions.

Check for missing dependencies, correct CMake configurations, and update library paths if errors occur.

For apt installations, use "sudo apt purge libopencv-dev python3-opencv"; for source builds, remove build files and use "sudo make uninstall".

Update system packages if installed via apt, or rebuild from source with the latest code for source installs.

You need Ubuntu 20.04 or newer, sudo privileges, and terminal access.

Ubuntu offers strong hardware support, an active community, and flexibility for computer vision development.

Leave a Reply

Your email address will not be published. Required fields are marked.