Compiling OpenCV from Source
|
Introduction
This guide will help you build OpenCV from the source. It will guide you through the process of configuring your build according to your needs. As of now, these instructions are for Ubuntu and, in general, Debian based systems.
Uninstall Current OpenCV Installation
In order to avoid conflicts with existing versions, remove the current installation from your system.
This will remove the current system OpenCV installation. Generally this is okay if it can be reinstalled using your package manager. |
sudo apt purge libopencv-dev libopencv-python libopencv-samples libopencv*
Install Dependencies
This are general dependencies that you'll need.
sudo apt install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev \ python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev python3-pip python3-numpy
GStreamer Support
If you are planning on adding support for GStreamer, install the following dependencies as well.
sudo apt install gstreamer1.0* sudo apt install ubuntu-restricted-extras sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
CUDA Support
If you are planning on adding support for CUDA, make sure you have it installed on your system.
- x86
- Follow the instructions provided by NVIDIA CUDA Installation Guide for Linux.
- NVIDIA Jetson
- We recommend installing CUDA via NVIDIA SDK Manager.
Clone the OpenCV Project
You may use RidgeRun OpenCV Fork, or use the original project. RidgeRun's fork contains some improvements around speed and efficiency.
These commands will avoid downloading the full repo history (which is a lot), so it's faster. If you'd like to keep the history, get rid of the --depth 1.
RidgeRun's Fork
git clone https://github.com/ridgerun/opencv.git --depth 1
Original Project
VERSION=4.4.0 git clone https://github.com/opencv/opencv.git -b $VERSION --depth 1
Clone the Contrib Extra Modules
If you'd like to install the non-free modules please clone the following project.
There are commercial restrictions to using these modules. Please seek consultancy if you plan to use them in a commercial product. |
RidgeRun's Fork
git clone https://github.com/RidgeRun/opencv_contrib.git --depth 1
Original Project
VERSION=4.4.0 git clone https://github.com/opencv/opencv_contrib.git -b $VERSION --depth 1
Configure the OpenCV Project
cd opencv mkdir build cd build cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D OPENCV_GENERATE_PKGCONFIG=ON \ -D BUILD_EXAMPLES=OFF \ -D INSTALL_PYTHON_EXAMPLES=OFF \ -D INSTALL_C_EXAMPLES=OFF \ -D PYTHON_EXECUTABLE=$(which python2) \ -D BUILD_opencv_python2=OFF \ -D PYTHON3_EXECUTABLE=$(which python3) \ -D PYTHON3_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \ -D PYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \ ..
Contrib Extra Modules
If you decide to also build the contrib extra modules, append the following configuration:
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules/ \
GStreamer Support
If support for GStreamer is to be added, append the following configuration:
-D WITH_GSTREAMER=ON \
CUDA Support
If support for CUDA is to be added, append the following configuration:
-D WITH_CUDA=ON \
The script will try to detect the architecture. If you'd like to explicitly specify your configuration, set CUDA_ARCH_BIN to one of the following values:
GPU | Compute Capability |
---|---|
Jetson AGX Xavier | 7.2 |
Jetson TX2 | 6.2 |
Jetson TX1 | 5.3 |
Jetson Nano | 5.3 |
Check the Log
✔ Verify if it has Python 3: section and all interpreter and path are right. (If they aren’t there, check the NumPy package)
✔ Check the GStreamer section. (If it does not indicate YES, go check the GStreamer lib package)
Build the OpenCV Project
This will take some time. Please be patient.
make -j8
Note that on smaller systems (like the Jetson Nano), the parallel build may eat up all the memory, resulting in a failed build as the following:
[ 98%] Built target opencv_test_mcc [ 98%] Built target opencv_test_face Segmentation fault (core dumped) CMake Error at cuda_compile_1_generated_pyrlk.cu.o.RELEASE.cmake:281 (message): Error generating file /home/mgruner/RidgeRun/opencv/build/modules/cudaoptflow/CMakeFiles/cuda_compile_1.dir/src/cuda/./cuda_compile_1_generated_pyrlk.cu.o
In such cases try removing the -j8 from the make call and freeing up some space in your FS.
Install the OpenCV Project
sudo make install sudo ldconfig
Troubleshooting
Eigen/core
In case of issues with Eigen/core, example:
opencv/modules/core/include/opencv2/core/private.hpp:66:12: fatal error: Eigen/Core: No such file or directory # include <Eigen/Core>
Disable the precompiled headers from the cmake configuration, append the next flag:
-DENABLE_PRECOMPILED_HEADERS=OFF \
Make sure that precompiled headers are disabled in the configuration summary.
Out of Space in Jetson Boards
A full OpenCV build can take up to 2GB of space. This is a lot for smaller platforms. Here are some things I usually erase in order to make some space on NVIDIA Jetson boards:
# # Please double check you actually don't need these before purging # sudo apt purge chromium-browser sudo apt purge thunderbird* sudo apt purge libreoffice* # If you're not planning on using docker sudo apt purge docker*
For direct inquiries, please refer to the contact information available on our Contact page. Alternatively, you may complete and submit the form provided at the same link. We will respond to your request at our earliest opportunity.
Links to RidgeRun Resources and RidgeRun Artificial Intelligence Solutions can be found in the footer below.