CODEMONDAY
Published in

CODEMONDAY

OpenCV4, Python3, Raspberry Pi Installation Guide

I wrote this guide to walk through OpenCV installation which is messier than what I’ve thought. It’s not like one command and done.

Much easier alternative

If you only use OpenCV with python, consider $ pip3 install opencv-python linked here.

Warning
To make this simple, I limit this guide to platform mentioned. There are no additional configuration or intermediate stuffs like virtual environment.

Overview

It’s likely that the process will not be smooth.That’s why I add “Explanation”. That will hint you where the problem might occur and what to Google for when you stuck.

Step 1: Dependency***
Step 2: Python, Numpy
Step 3: Download OpenCV
Step 4: Cmake
Step 5: Compile
Step 6: Done

  • Main things here (Step 3–5) is just download and C/C++ style installation i.e. make, make install.
  • What make this messy is at Step 1 or possibly Step 2.

Step 1: Dependencies ***

This will be different between installation guide. Depends on what the author assume you already have in your machine.

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install \
build-essential cmake unzip pkg-config gfortran \
libjpeg-dev libpng-dev libtiff-dev \
libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
libxvidcore-dev libx264-dev \
libhdf5-dev libhdf5-serial-dev \
libgtk-3-dev \
libatlas-base-dev libblas-dev liblapack-dev \
python3-dev \

Explanation:

This step install what OpenCV build on. There are several categories here:

  • C/C++, Fortran related stuff (OpenCV is written in C/C++) e.g. build essential, cmake, pkg-config
  • Read image video stuff e.g. libjpeg-devfor jpeg, libx264-dev for H.264 video …
  • Library for Math e.g liblapack-dev Linear algebra …
  • Python related stuffs e.g. python3-dev header files …
  • Necessary command line e.g. wget (I don’t add this. It’s default on Pi4.)
  • Some are deprecated i.e. used in older version of OpenCV but you will find in some installation guide

Possible Error:
Wrong or not updated package name → Package not found. Search Google for the correct package name.

You might need more or less.
Depends on what you have install or indirectly installed by installed some package on your machine. Also, build for python and Java require different dependencies. If you miss some packages, it will fail the Step 4 or Step 5.

Most problems occurs here. You might need to return here soon to install the missing package. Or worse, conflict version of package.

Step 2: Python 3 Numpy

$ pip3 install numpy

Explaination:

You just need additional package call numpy. Python3 and pip3 (python package manager) is installed by default. If pip3 is not found, you might need to add path instructed here or reinstall python3, pip3.

Step 3: Download OpenCV

Just download and unzip.
If your raspberry pi can open web browser, you might not need command line. Just go to the link, download and unzip to your home folder.

https://github.com/opencv/opencv/archive/4.1.2.zip
https://github.com/opencv/opencv_contrib/archive/4.1.2.zip

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

Step 4: Cmake

$ cd ~/opencv-3.4.1/
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-4.1.2/modules \
-D BUILD_EXAMPLES=ON ..

Explanation:

Create folder build/ to prevent the cmake output mixed with stuffs in opencv folder. Then go into it and cmake, which will prepare installation in different OS/Platform.

The final .. is specify to use CMakeLists.txt on above folder.

How do you know what to specify after -D?
Just see CMakeLists.txt and files in the cmake/ above folder. There are plenty for configuration but only main 3–4 things in the above code block. The others flags are. for example, include patent non-free algorithm or not, build for Java or not etc.

The -D flag will be different between installation guide. Don’t get confused.

At the end of this process you must see path of python3, numpy and so on

--   Python 3:
-- Interpreter: /usr/bin/python3 (ver 3.7.3)
...

Common Problems

  • Wrong path: you put opencv_contrib-4.1.2 somewhere else or rename
  • Missing packages in Step 1
  • Cannot find python or wrong/conflict version

Fail Silently

Missing package might not prompt error but show in make result below. In this example, I forget to install libavcodec-dev, libavformat-devso the installed version won’t work on this file format and we have to go back and rebuild OpenCV.

--   Video I/O:
..
-- FFMPEG: NO
-- avcodec: NO
-- avformat: NO
..
After fixing flags turn to YES
-- Video I/O:
..
-- FFMPEG: YES
-- avcodec: YES
-- avformat: YES
..

Step 5: Compile

make -j4

This take 2–3 hours.
Then this command

$ sudo make install
$ sudo ldconfig

It might stuck at Building CXX Object ... which takes 20–30 mins. Do not worry, it just take time.

Explanation:

makebuild object and prepare ingredients need for installation. Flag -j4 let 4 cores of processors work to speed things up.

This different from cmake which prepare for different platform but not build it self.

make install install. ldconfig creates the necessary links and cache to the most recent shared libraries found here.

Step 6: Done

$ python3
>>> import cv2
>>> cv2.__version__
4.1.2

Explanation:
Test import cv2 in python. Return the correct version. Done.

Software house & Artificial intelligent service
www.codemonday.com

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store