Difference between revisions of "Compiling OpenCV from Source"

From RidgeRun Developer Connection
Jump to: navigation, search
m
m
 
Line 1: Line 1:
<seo title="Compiling OpenCV from Source | OpenCV | RidgeRun" titlemode="replace" metakeywords="OpenCV, GStreamer, CUDA, NVIDIA, Jetson, JetPack, Tegra, Nano, TX2, TX1, Xavier, OpenCV Source, OpenCV Installation" metadescription="This wiki provides instructions on how to build OpenCV from source with support for GStreamer and CUDA."></seo>
+
<seo title="Compiling OpenCV from Source | OpenCV | RidgeRun" titlemode="replace" metakeywords="OpenCV, GStreamer, CUDA, NVIDIA, Jetson, JetPack, Tegra, Nano, TX2, TX1, Xavier, OpenCV Source, OpenCV Installation, OpenCV project" metadescription="This wiki provides instructions on how to build OpenCV from source with support for GStreamer and CUDA."></seo>
  
 
<table>
 
<table>
Line 40: Line 40:
 
</source>
 
</source>
  
=== GStreamer ===
+
=== GStreamer Support ===
  
 
If you are planning on adding support for GStreamer, install the following dependencies as well.
 
If you are planning on adding support for GStreamer, install the following dependencies as well.
Line 50: Line 50:
 
</source>
 
</source>
  
=== CUDA ===
+
=== CUDA Support===
  
 
If you are planning on adding support for CUDA, make sure you have it installed on your system.
 
If you are planning on adding support for CUDA, make sure you have it installed on your system.
Line 58: Line 58:
 
: We recommend installing CUDA via [https://developer.nvidia.com/nvidia-sdk-manager NVIDIA SDK Manager].
 
: We recommend installing CUDA via [https://developer.nvidia.com/nvidia-sdk-manager NVIDIA SDK Manager].
  
== Clone the Project ==
+
== 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.
 
You may use [[RidgeRun OpenCV Fork]], or use the original project. RidgeRun's fork contains some improvements around speed and efficiency.
Line 100: Line 100:
 
</source>
 
</source>
  
== Configure the Project ==
+
== Configure the OpenCV Project ==
 
<source lang="bash">
 
<source lang="bash">
 
cd opencv
 
cd opencv
Line 127: Line 127:
 
</source>
 
</source>
  
=== GStreamer ===
+
=== GStreamer Support===
  
 
If support for GStreamer is to be added, append the following configuration:
 
If support for GStreamer is to be added, append the following configuration:
Line 134: Line 134:
 
</source>
 
</source>
  
=== CUDA ===
+
=== CUDA Support===
  
 
If support for CUDA is to be added, append the following configuration:
 
If support for CUDA is to be added, append the following configuration:
Line 168: Line 168:
 
[[File:Opencv configure.png|600px|center]]
 
[[File:Opencv configure.png|600px|center]]
  
== Build the Project ==
+
== Build the OpenCV Project ==
  
 
This will take some time. Please be patient.
 
This will take some time. Please be patient.
Line 176: Line 176:
  
 
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:
 
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:
 +
 
<source lang="bash" highlight=3>
 
<source lang="bash" highlight=3>
 
[ 98%] Built target opencv_test_mcc
 
[ 98%] Built target opencv_test_mcc
Line 187: Line 188:
 
In such cases try removing the '''-j8''' from the make call and freeing up some space in your FS.
 
In such cases try removing the '''-j8''' from the make call and freeing up some space in your FS.
  
== Install the Project ==
+
== Install the OpenCV Project ==
  
 
<source lang="bash">
 
<source lang="bash">
Line 195: Line 196:
  
 
== Troubleshooting ==
 
== Troubleshooting ==
 +
 
=== Eigen/core ===
 
=== Eigen/core ===
  

Latest revision as of 11:55, 8 August 2023

Nvidia-preferred-partner-badge-rgb-for-screen.png

RR Contact Us.png

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.

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.

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:

Table 1. Jetson Compute Capabilities related to its architecture (CUDA_ARCH_BIN)
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)

Opencv configure.png

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*


RidgeRun Resources

Quick Start Client Engagement Process RidgeRun Blog Homepage
Technical and Sales Support RidgeRun Online Store RidgeRun Videos Contact Us

OOjs UI icon message-progressive.svg Contact Us

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. Ridgerun-logo.svg
RR Contact Us.png