Easily Compiling OpenCV in Raspberry PI

Are you having problems in compiling OpenCV in Raspberry PI? Here you can learn how to do it easily.

Story

Overview

OpenCV is the most popular (and, in my opinion, most powerful too) image processing framework available. And even better: it’s open-source and free to use (including in comercial solutions)!

Although, compiling OpenCV can be something a little bit tricky for some platforms. Therefore, this post/article will show how to compile OpenCV in your Raspberry PI, making things easier for you and saving your time for cool computer vision projects.

Recommendations

Here are some recommendations about this process:

  • Use a reliable power source. This process requires a lot of computer resources (CPU processing and memory usage), so using a non-reliable power source may cause Raspberry PI stop working (shutdown due to insuficient power sourcing or something like that).
  • Don’t use a fanless Raspberry PI case. As this process required a lot of computer resources, the processor will easily get hot. Therefore, keeping your Raspberry PI in a fanless case may cause shutdown for high processor temperature.
  • I strongly recommend class 10 SD Card usage. This process requires a lot of file access (as every compilation of big softwares), so using class 10 SD Card will reduce compilation time a lot.

Process

The full process comprises:

  • Installing dependencies / necessary packages
  • Download of OpenCV source-code
  • Compile OpenCV source-code
  • Install compiled OpenCV library

In order to make it easier for you, I’ve created a shell script (it’s in code session of this post/article ). This shell script automatically does everything needed, so all you have to do is give execution permission to the script and execute it on your Raspberry PI as sudo.

In another words, all you need is put this script into Raspberry PI’s memory (in your home folder) and use the following commands:

chmod +x OpenCV_CompileAndInstallScript.sh
sudo ./OpenCV_CompileAndInstallScript

That’s it! Just wait the end of the proccess and you’re ready to use OpenCV.

Obs:

  • Tthe whole process will take some hours to complete. So, a good tip is using overnight period to do this.
  • This script also prepares Python wrappers for OpenCV. It means that, after doing this process, you can make projects using Python and OpenCV.

Code

#!/bin/bash

echo ""
echo "-----------------------------------------------"
echo "Installation of all necessary packages for OpenCV"
echo "-----------------------------------------------"
echo ""

sudo apt-get install -y build-essential cmake pkg-config
sudo apt-get install -y libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install -y libxvidcore-dev libx264-dev
sudo apt-get install -y libgtk2.0-dev
sudo apt-get install -y libatlas-base-dev gfortran
sudo apt-get install -y python2.7-dev python3-dev

echo ""
echo "----------------------------------------"
echo "OpenCV 3.1.0 source-code download"
echo "----------------------------------------"
echo ""

cd ~
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip
unzip opencv.zip

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

echo ""
echo "------------------------------------------------------------------------"
echo "numpy installation"
echo "------------------------------------------------------------------------"
echo ""

pip install numpy

echo ""
echo "--------------------------"
echo "OpenCV 3.1.0 compilation"
echo "--------------------------"
echo ""

cd ~/opencv-3.1.0/
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D ENABLE_PRECOMPILED_HEADERS=OFF \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \
    -D BUILD_EXAMPLES=ON ..


make

echo ""
echo "--------------------------"
echo "OpenCV 3.1.0 installation"
echo "--------------------------"
echo ""

sudo make install

echo ""
echo "--------------------------------------------"
echo "Create OpenCV library cache and links"
echo "--------------------------------------------"
echo ""

sudo ldconfig

Source: Easily Compiling OpenCV in Raspberry PI
Scroll to Top