← Blog

Optimizing opencv 3.4 in Raspberry Pi 3 using GStreamer

Optimizing opencv 3.4 in Raspberry Pi 3 using GStreamer

OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library.

In this tutorial we will follow the steps to build an opencv library optimized for the raspberry pi 3 arm cortex processor with gstreamer.

These optimizations consist of building OpenCV binaries that make use of the NEON instructions and VFPV architecture.

These technologies are a great aid when dealing with large arrays of data, and SIMD instructions.

Use the following table to navigate the content, and let’s start!.

Content Table Step 1: Dependencies Step 2: Download OpenCV Step 3: Edit the compiler options Step 4: Increase the swap size Step 5: Compile and install OpenCV Step 6: Decrease the swap size Changes for OpenCV 3.1 Step 1: Dependencies

Delete Wolfram Engine and LibreOffice to free 500MB ~ 1GB of disk space:

Copy to Clipboard Syntax Highlighter 1 sudo apt-get purge wolfram-engine 2 sudo apt-get purge libreoffice* 3 sudo apt-get clean 4 sudo apt-get autoremove

Update and upgrade your current packages, and install opencv dependencies:

Copy to Clipboard Syntax Highlighter 1 sudo apt update && sudo apt upgrade 2 sudo apt install -y build-essential cmake pkg-config 3 sudo apt install -y libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev 4 sudo apt install -y libavcodec-dev libavformat-dev libswscale-dev libv4l-dev 5 sudo apt install -y libgtk2.0-dev libgtk-3-dev 6 sudo apt install -y libcanberra-gtk* 7 sudo apt install -y libatlas-base-dev gfortran 8 sudo apt install -y python2.7-dev python3-dev

# x264 Libraries

Copy to Clipboard Syntax Highlighter 1 sudo apt install -y libxvidcore-dev libx264-dev

# GStreamer libraries

Copy to Clipboard Syntax Highlighter 1 sudo apt install -y libgstreamer1.0- 2 sudo apt install -y libgstreamer-plugins-base1.0- 3 sudo apt install -y gstreamer1.0-plugins-* Step 2: Download OpenCV

Download Opencv 2.4.13.5 from https://opencv.org/releases.html

Copy to Clipboard Syntax Highlighter 1 $ cd 2 $ wget https://github.com/opencv/opencv/archive/3.4.1.zip 3 $ unzip 3.4.1.zip 4 $ wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.4.1.zip 5 $ unzip opencv_contrib.zip 6 $ cd opencv-3.4.1/ Step 3: Edit the compiler options

Open the file opencvcompileroptions.cmake :

Copy to Clipboard Syntax Highlighter 1 $ nano cmake/OpenCVCompilerOptions.cmake

Locate the line containing:

Copy to Clipboard Syntax Highlighter 1 # ocv_update(CPU_VFPV3_FLAGS_ON "-mfpu=vfpv3")

And replace it with:

Copy to Clipboard Syntax Highlighter 1 # ocv_update(CPU_VFPV3_FLAGS_ON "-mcpu=cortex-a7 -mfpu=neon-vfpv4 -ftree-vectorize -mfloat-abi=hard")

Save the file, verify the changes made.

Step 4: Increase the swap size

Now, let’s increase the swap size so gcc can use all 4 cores of the Raspberry Pi 3.

Open the file /etc/dphys-swapfile and edit CONF_SWAPSIZE variable:

Copy to Clipboard Syntax Highlighter 1 CONF_SWAPSIZE=1024

If you do not perform this step it’s very likely that gcc will crash when building the libraries using 4 threads.

Remember to change the swap size back to CONF_SWAPSIZE=100 once the installation is done.

WARNING: Increasing the SWAP_SIZE will reduce the lifespan of your SD card due to multiple rewrites in the flash card. Please consider doing a backup of your SD-card. Avoid using this SD-card for a final product, instead consider cloning the content of the memory into an SD-card image once your product is fully configured (with all its dependencies compiled and installed).

Restart the swapfile service:

Copy to Clipboard Syntax Highlighter 1 $ sudo /etc/init.d/dphys-swapfile stop 2 $ sudo /etc/init.d/dphys-swapfile start

Use htop to verify that the swap size increased.

Step 5: Compile and install OpenCV

Now let’s build OpenCV libraries:

Copy to Clipboard Syntax Highlighter 1 $ mkdir build 2 $ cd build 3 cmake \ 4 -D CMAKE_BUILD_TYPE=RELEASE \ 5 -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ 6 -D ENABLE_NEON=ON \ 7 -D ENABLE_VFPV3=ON \ 8 -D WITH_TBB=ON \ 9 -D WITH_GSTREAMER=ON \ 10 -D EXTRA_C_FLAGS=-mcpu=cortex-a7 -mfpu=neon-vfpv4 -ftree-vectorize -mfloat-abi=hard \ 11 -D EXTRA_CXX_FLAGS=-mcpu=cortex-a7 -mfpu=neon-vfpv4 -ftree-vectorize -mfloat-abi=hard \ 12 -D BUILD_TESTS=OFF \ 13 -D WITH_LAPACK=OFF \ 14 -D WITH_EIGEN=OFF \ 15 -D INSTALL_C_EXAMPLES=OFF \ 16 -D INSTALL_PYTHON_EXAMPLES=OFF \ 17 -D BUILD_EXAMPLES=OFF

Here comes the time consuming part, now run:

Copy to Clipboard Syntax Highlighter 1 $ make -j4

And wait for 2-3 hours until it finishes. Remember to check the process every now and then.

Note: using a heatsink for the processor and keeping it as cool as possible, will likely reduce the compilation time.

When (and if) OpenCV is finally compiled, install it in the linux system using:

Copy to Clipboard Syntax Highlighter 1 $ sudo make install 2 $ sudo ldconfig Step 6: Decrease the swap size

Edit the file /etc/dphys-swapfile and change the CONF_SWAPSIZE variable:

Copy to Clipboard Syntax Highlighter 1 CONF_SWAPSIZE=100

Restart the swapfile service:

Copy to Clipboard Syntax Highlighter 1 $ sudo /etc/init.d/dphys-swapfile stop 2 $ sudo /etc/init.d/dphys-swapfile start

Now you can have an OpenCV built to make the best use of the processor in your Raspberry Pi 3.

Changes for OpenCV 3.1

In step 2, download OpenCV 3.1

Copy to Clipboard Syntax Highlighter 1 $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.3.0.zip 2 $ unzip opencv.zip 3 $ wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.3.0.zip 4 $ unzip opencv_contrib.zip

In step 5, add the flag: 7

Copy to Clipboard Syntax Highlighter 1 -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.3.0/modules