We’re going to learn in this tutorial how to install Opencv 4.1 on the Nvidia Jetson Nano.

First of all we need to make sure that there is enough memory to proceed with the installation.
The Jetson Nano has 4GB of ram, and they’re not enough for some installations, and Opencv is one of them. To solve this problem we need to add some extra memory, called “Swap memory” which will take place when all the RAM has been used.

We will see in this post how we can create and or extend the Swap memory and then how to install Opencv.

Add Swap Memory

By default the Ubuntu 18.04 distribution of Jetson Nano comes with 2 gb of Swap memory.

To increase it we need to open the terminal and type the line:

sudo apt-get install zram-config

The zram module, on the Jetson nano allocates by default 2gb of Swap memory, so now we’re going to extend the size to 4gb by changing the configuration file.

Just type on the terminal:

sudo gedit /usr/bin/init-zram-swapping

Replace the line:
mem=$(((totalmem / 2 / ${NRDEVICES}) * 1024))

with this line:

mem=$(((totalmem / ${NRDEVICES}) * 1024))

And then reboot.

Install Opencv 4.1

The installation of Opencv on the Jetson Nano takes around one hour.
We need to build Opencv from the source code, and we can do it by following these 7 steps below.

1. Updating the packages:

sudo apt update
sudo apt install -y build-essential cmake git libgtk2.0-dev pkg-config  libswscale-dev libtbb2 libtbb-dev
sudo apt install -y python-dev python3-dev python-numpy python3-numpy
sudo apt install -y curl

2. Install video & image formats:

sudo apt install -y  libjpeg-dev libpng-dev libtiff-dev libjasper-dev 
sudo apt install -y libavcodec-dev libavformat-dev
sudo apt install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt install -y libv4l-dev v4l-utils qv4l2 v4l2ucp libdc1394-22-dev

3. Download OpenCV & Contribs Modules:

curl -L https://github.com/opencv/opencv/archive/4.1.0.zip -o opencv-4.1.0.zip
curl -L https://github.com/opencv/opencv_contrib/archive/4.1.0.zip -o opencv_contrib-4.1.0.zip

4. Unzipping packages:

unzip opencv-4.1.0.zip 
unzip opencv_contrib-4.1.0.zip 
cd opencv-4.1.0/

5. Create directory:

mkdir release
cd release/

6. Build Opencv using Cmake:

cmake 	-D WITH_CUDA=ON \
		-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.1.0/modules \
		-D WITH_GSTREAMER=ON \
		-D WITH_LIBV4L=ON \
		-D BUILD_opencv_python2=ON \
		-D BUILD_opencv_python3=ON \
		-D BUILD_TESTS=OFF \
		-D BUILD_PERF_TESTS=OFF \
		-D BUILD_EXAMPLES=OFF \
		-D CMAKE_BUILD_TYPE=RELEASE \
		-D CMAKE_INSTALL_PREFIX=/usr/local ..

7. Compile the OpenCV with Contribs Modules:

make -j4
sudo make install

The installation is now completed.