Optimizing opencv 2.4 and 3.1 in Raspberry Pi 3

Optimizing opencv 2.4 and 3.1 in Raspberry Pi 3

raspberry photo

raspberry

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

This optimizations consist on 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.

Step 1: Dependencies

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

sudo apt-get purge wolfram-engine
sudo apt-get purge libreoffice*
sudo apt-get clean
sudo apt-get autoremove

 

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

sudo apt-get update && sudo apt-get upgrade
sudo apt-get install build-essential cmake pkg-config -y
sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev -y
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev -y
sudo apt-get install libxvidcore-dev libx264-dev -y
sudo apt-get install libgtk2.0-dev libgtk-3-dev -y
sudo apt-get install libcanberra-gtk* -y
sudo apt-get install libatlas-base-dev gfortran -y
sudo apt-get install python2.7-dev python3-dev -y

 

Step 2: Download OpenCV

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

wget

https://github.com/opencv/opencv/archive/2.4.13.5.zip

unzip 2.4.13.5.zip
cd opencv-2.4.13.5/

Step 3: Edit the compiler options

Open the file opencvcompileroptions.cmake :

nano cmake/OpenCVCompilerOptions.cmake

Locate the lines containing:

set(OPENCV_EXTRA_C_FLAGS “”)  and

set(OPENCV_EXTRA_CXX_FLAGS “”)

And replace them with:

set(OPENCV_EXTRA_C_FLAGS “-mcpu=cortex-a7 -mfpu=neon-vfpv4 -ftree-vectorize -mfloat-abi=hard”)

set(OPENCV_EXTRA_CXX_FLAGS “-mcpu=cortex-a7 -mfpu=neon-vfpv4 -ftree-vectorize -mfloat-abi=hard”)

Also, under the line containing:

if (ENABLE_NEON)

Replace -mfpu=neon

with -mfpu=neon-vfpv4

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:

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:

sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start

Use htop to verify that the swap size increased.

htop

Step 5: Compile and install OpenCV

Now let’s build opencv libraries:

mkdir build
cd build
export CFLAGS="-mcpu=cortex-a7 -mfpu=neon-vfpv4 -ftree-vectorize -mfloat-abi=hard" # Notice here does not have -fPIC and -O3
export CXXFLAGS="-mcpu=cortex-a7 -mfpu=neon-vfpv4 -ftree-vectorize -mfloat-abi=hard" # Notice here does not have -fPIC and -O3

cmake -D CMAKE_BUILD_TYPE=RELEASE \
   -D CMAKE_INSTALL_PREFIX=/usr/local \
   -D ENABLE_NEON=ON \
   -D ENABLE_VFPV3=ON \
   -D WITH_TBB=ON \
   -D WITH_GSTREAMER=ON \
   -D EXTRA_C_FLAGS=-mcpu=cortex-a7 -mfpu=neon-vfpv4 -ftree-vectorize -mfloat-abi=hard \
   -D EXTRA_CXX_FLAGS=-mcpu=cortex-a7 -mfpu=neon-vfpv4 -ftree-vectorize -mfloat-abi=hard \
   -D BUILD_TESTS=OFF \
   -D INSTALL_PYTHON_EXAMPLES=OFF \
   -D BUILD_EXAMPLES=OFF ..

Here comes the time consuming part, now run:

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:

sudo make install
sudo ldconfig

Step 6: Decrease the swap size

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

CONF_SWAPSIZE=100

Restart the swapfile service:

sudo /etc/init.d/dphys-swapfile stop
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 with:

wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.3.0.zip
unzip opencv.zip
wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.3.0.zip
unzip opencv_contrib.zip

In step 5, add the flag:

-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.3.0/modules

 

References:

http://hopkinsdev.blogspot.com.co/2016/06/opencv-310-optimized-for-raspberry-pi.html

https://www.pyimagesearch.com/2017/10/09/optimizing-opencv-on-the-raspberry-pi/

http://ebenezertechs.com/instalar-opencv-2-4-13-en-ubuntu-14-04-y-16-04/

https://www.pexels.com

, ,
ZH-CN NL EN DE PT RU ES