Tuesday, February 11, 2020

Installing OpenCV 4 on a Raspberry Pi 4

I have recently been updating the Computer Vision chapter of the third edition of my Raspberry Pi Cookbook. Way back when I was writing the seconds edition, I decided to use the SimpleCV wrapper to OpenCV because it was -- well very simple to use.

SimpleCV is no longer maintained and in any case requires Python2. So the chapter's code needed to be changed to use OpenCV directly. Here's what I had to do on a Raspberry Pi 4 running Raspbian Buster (10).

To install OpenCV, first install the prerequisite packages using these commands:
$ sudo apt-get update
$ sudo apt-get install libhdf5-dev libhdf5-serial-dev libhdf5-103 
$ sudo apt-get install libqtgui4 libqtwebkit4 libqt4-test python3-pyqt5
You may also need to update pip using:
$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python3 get-pip.py
Then install OpenCV itself and Python image utilities using these commands:
$ sudo pip install opencv-contrib-python==
$ pip install imutils
After installation is complete, you can check that everything is working by starting Python 3, importing cv2 and checking the version:
pi@raspberrypi:~ $ python3
Python 3.7.3 (default, Apr  3 2019, 05:39:12) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
>>> exit()