Compiling OpenCV from Source
Contents
Introduction
This guide will help you build OpenCV from source. It will guide you through the process of configuring your build according to your needs. As per 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
sudo apt install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev
sudo apt install python3-pip python3-numpy
Gstreamer
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
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.
- NVIDIA Jetson
- We recommend installing CUDA via SDK Manager.
Clone the 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.3.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 for consultancy if you plan to use them in a commercial product. |
VERSION=4.3.0
git clone https://github.com/opencv/opencv_contrib.git -b $VERSION --depth 1
Configure the Project
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
If support for GStreamer is to be added, append the following configuration:
-D WITH_GSTREAMER=ON
CUDA
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 configuration 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 GStreamer lib package)
Building for OpenCV with GStreamer
It will last some time. Please be patient.
sudo make -j4
Install OpenCV with GStreamer support package
sudo make install
sudo ldconfig
RidgeRun Resources | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Visit our Main Website for the RidgeRun Products and Online Store. RidgeRun Engineering informations are available in RidgeRun Professional Services, RidgeRun Subscription Model and Client Engagement Process wiki pages. Please email to support@ridgerun.com for technical questions and contactus@ridgerun.com for other queries. Contact details for sponsoring the RidgeRun GStreamer projects are available in Sponsor Projects page. | ![]() ![]() |