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!.
Step 1: Dependencies
Delete Wolfram Engine and LibreOffice to free 500MB ~ 1GB of disk space:
Update and upgrade your current packages, and install opencv dependencies:
# x264 Libraries
# GStreamer libraries
Step 2: Download OpenCV
Download Opencv 2.4.13.5 from https://opencv.org/releases.html
Step 3: Edit the compiler options
Open the file opencvcompileroptions.cmake :
Locate the line containing:
And replace it with:
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:
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:
Use htop to verify that the swap size increased.
Step 5: Compile and install OpenCV
Now let’s build OpenCV libraries:
Here comes the time consuming part, now run:
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:
Step 6: Decrease the swap size
Edit the file /etc/dphys-swapfile and change the CONF_SWAPSIZE variable:
Restart the swapfile service:
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
In step 5, add the flag: 7




