OpenCV memo

[2019.07.31]
Jetson Nano build-in OpenCV is 3.3.1, and it came from ubuntu without CUDA enabling.

How to install pre-built OpenCV ?

prebuild opencv package will enable CUDA, gstreamer and V4L support.

Remove Nano build-in OpenCV 3.3.1

sudo apt-get purge *libopencv*

Download the pre-build deb package and install

For using git on Nano, you will need to generate SSH key on Nano, and add key to gitLAB
git clone git@172.18.18.18:Jetson_Nano/opencv-build.git
cd opencv-build
sudo apt install ./OpenCV-3.4.0-aarch64-dev.deb
sudo apt install ./OpenCV-3.4.0-aarch64-libs.deb
sudo apt install ./OpenCV-3.4.0-aarch64-python.deb
After deb installation, OpenCV files can be found in the following folders and ready to be used, but if you need to re-build OpenCV by any reason, please refer to the following section.

Include files:

/usr/include/opencv
/usr/include/opencv2

Libraries:

/usr/lib

TODO

  1. make install/strip to get smaller size

How to compile/run OpenCV sample code ?

After installing the prebuild opencv, you can download the opencv sample code to build/run.

Download OpenCV source code

mkdir ~/work
cd ~/work
git clone git@172.18.18.18:Jetson_Nano/opencv-build.git
cd opencv-build-script
./prepare_opencv3.4.0_Nano.sh ~/work

Build opencv sample code

cd opencv-3.4.0/samples
mkdir build
cd build
cmake ..
make -j3

Build OpenCV CUDA sample code

CUDA sample code can not be built with default cmake config, please use the following command to build cuda sample code.
cd opencv-3.4.0/samples/gpu
g++ -o houghlines houghlines.cpp `pkg-config opencv --cflags --libs`

How to build OpenCV for Nano with CUDA/gstreamer enabled ?

It's time consuming work, usually you only need to install the prebuild opencv in previous section.

Prepare the environment

  • Enable swap file to 4GB to make sure memory is enough
Steps to enable 4GB swap file
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon --show
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
or you can run ./enable_4G_swap.sh in Jetson_Nano/opencv-build.git
Reboot the device to enable swap file.

Download opencv and Build

git clone git@172.18.18.18:Jetson_Nano/opencv-build.git
cd opencv-build-script
./prepare_opencv3.4.0_Nano.sh ~/
./build_opencv3.4.0_Nano.sh ~/
build_opencv3.4.0_Nano.sh will config and "make -j3" to build opencv binaries
See "Install" section to install opencv on Nano

Build

cd opencv-3.4.0/release
make -j3

Install

cd opencv-3.4.0/release
make install
Default will install opencv to /usr/local

Uninstall

cd opencv-3.4.0/release
make uninstall

Create deb file

cd opencv-3.4.0/release
make -j3 package
NOTE: add the following define when use cmake to make deb work.
-D OPENCV_VCSVERSION="3.4.0" -D CPACK_BINARY_DEB=ON

留言

這個網誌中的熱門文章

以樂透為例,用Python統計馬可夫矩陣

將 Jenkins Job 的歷史結果整理出視覺化的 Daily Report mail (一)

如何用 Jenkins API 取得 Job Build Result