/ ai

Installing OpenCV 3.4.3 on Raspberry Pi 3 Model B+

I previously wrote a step-by-step guide showing how to make OpenCV 3.4.1 run on a Raspberry Pi 3 B. That article generated a lot of feedback. I have completed a few installations since then, so here's a new, streamlined, process for getting OpenCV 3.4.1 (most recent version!) to run on your Raspberry Pi 3 B plus.

Why 3.4.1? It offers an improved DNN module and many other improvements and bug fixes. Overall, about 250 patches have been integrated and over 200 issues have been closed since OpenCV 3.4.0.
pi

Warning: compiling OpenCV is a CPU-intensive task - all 4 cores will be maxed out for 1..2 hours. To avoid overheating, make sure your Raspberry Pi has radiators and a fan (or place a powerful external fan next to it). No, it won't die from overheating, but it will throttle its CPU performance, potentially increasing build time from 2 hours to 6 hours, and you probably don't want that.

So, here's the new process:

Step 1: make sure your OS is current. (< 5min)

Current OS version is Raspbian Stretch (April 2018). You can either do a clean install from SD (follow the instructions listed here), or upgrade your existing version.

To upgrade, open (as sudo) the files /etc/apt/sources.list and /etc/apt/sources.list.d/raspi.list in your favorite editor, and change all occurences of your current distro name (e.g. "jessie") to "stretch". Then, open a terminal and run the update:

sudo apt-get update
sudo apt-get -y dist-upgrade

If you're already running Stretch, simply update all packages before proceeding:

sudo apt-get update
sudo apt-get upgrade

Step 2: configure SSH and utilities (< 2min)

Make sure SSH is enabled. Change the default password, too!

Some of my favorite utilities on Linux are screen (to keep processes running if your terminal session is dropped) and htop (performance monitoring) - some of these may already be pre-installed:

sudo apt-get install screen
sudo apt-get install htop

Step 3: free up 1GB+ by ditching Wolfram and Libreoffice (< 2min)

It's unlikely that you will need these two packages on your computer vision box, so:

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

Step 4: install dependencies (< 10min)

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

Step 5: install Python 3 (< 2min)

We need this in order to enable Python bindings in Open CV:

In OpenCV, all algorithms are implemented in C++. But these algorithms can be used from different languages like Python, Java etc. This is made possible by the bindings generators. These generators create a bridge between C++ and Python which enables users to call C++ functions from Python. To get a complete picture of what is happening in background, a good knowledge of Python/C API is required. A simple example on extending C++ functions to Python can be found in official Python documentation[1]. So extending all functions in OpenCV to Python by writing their wrapper functions manually is a time-consuming task. So OpenCV does it in a more intelligent way. OpenCV generates these wrapper functions automatically from the C++ headers using some Python scripts which are located in modules/python/src2

sudo apt-get install python3-dev

Step 6: install pip3 (< 2min)

sudo apt-get install python3-pip

Step 7: get the latest (3.4.3) OpenCV source code (< 5min)

I am using version 3.4.3 of OpenCV. You can check the Releases section of the official site (or Github) to see what the current build is. If your desired version is different, update the commands and paths below accordingly.

Download and unzip OpenCV 3.4.3 and its experimental modules (those are stored in the opencv_contrib repository):

wget -O opencv.zip https://github.com/opencv/opencv/archive/3.4.3.zip

wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/3.4.3.zip

unzip opencv.zip

unzip opencv_contrib.zip

Step 8: install Numpy, Scipy (< 3min)

sudo pip3 install numpy scipy

Step 9: compile OpenCV (< 10min)

Again, I am using version 3.4.3 of OpenCV. If you aren't - update your paths accordingly:

cd ~/opencv-3.4.3/
mkdir build
cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.4.3/modules \
    -D BUILD_EXAMPLES=ON ..

Make sure cmake finishes without errors.

Step 10: build OpenCV (90-120min)

Note: this step will take a long time. Took almost 2 hours on my device. Also, your Raspberry Pi will overheat without proper cooling.
This will work faster if you use all four CPU cores:

make -j4

Once OpenCV builds successfully, continue the installation:

sudo make install
sudo ldconfig
sudo apt-get update

... then reboot the system - and you should be good to go!

sudo reboot

Step 11: test your OpenCV installation (< 1min)

$ python3
Python 3.5.3 (default, September 5 2018, 14:11:04) 
[GCC 6.3.0 20170124] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.4.3'
>>> 

Comments and suggestions are welcome!
IMG_6381