OpenCV installation procedure on Raspberry Pi

I had the opportunity to use OpenCV on my Raspberry Pi at work, but I couldn't install it using only the information on the official OpenCV page, and I had a hard time finding reliable information, so I would like to share the information.

Article flow

__1. There are two main installation methods __ -Install the unofficial pre-built package (opencv-python) -Build official OpenCV on Raspberry Pi

__2. OpenCV operation check __ -Check if it can be imported -Operation check by camera reading sample program

__3. About OpenCV license __ -OpenCV is a BCD license -Contrib modules include those that are not commercially available

environment

-Build an environment on RaspberryPi4 (OS: Raspbian Buster with desktop). -It is assumed that OpenCV is used in Python. The version of Pyhton is Python 3.7.3. -The version of OpenCV to be installed is OpenCV4.

1. There are two main installation methods.

■ Install the unofficial pre-built package (opencv-python)

__ ● Source / Basis for installation procedure __

<Pypi(Python Package Index) opencv-python> https://pypi.org/project/opencv-python/ Pypi is a package management service for pip. On the opencv-python page, it was written as follows.

Note that the wheel (especially manylinux) format does not currently support properly ARM architecture so there are no packages for ARM based platforms in PyPI. However, opencv-python packages for Raspberry Pi can be found from https://www.piwheels.org/.

\ <google translate ...>

The wheel (especially manylinux) format does not currently properly support the ARM architecture, so PyPI does not have a package for ARM-based platforms. However, the opencv-python package for Raspberry Pi is available at https://www.piwheels.org/.

And that. By the way, Raspberry Pi is ARM Linux. The package for Raspberry Pi is not here, but it seems to say that it can be downloaded from piwheels. So, let's take a look at the piwheels page. <piwheels opencv-python> https://www.piwheels.org/

There is a description of the site at the beginning.

piwheels is a Python package repository providing Arm platform wheels (pre-compiled binary Python packages) specifically for the Raspberry Pi, making pip installations much faster. Packages are natively compiled on Raspberry Pi 3 hardware using the Mythic Beasts Pi cloud.

\ <google translate ...>

piwheels is a Python package repository that provides Arm platform wheels (compiled binary Python packages) specifically for the Raspberry Pi, which significantly speeds up pip installation. The package is compiled natively on Raspberry Pi 3 hardware using the Mythic Beasts Pi cloud.

The point is that it is a service that provides packages for Raspberry Pi. Miscellaneous. It says RaspberryPi3, but I was able to install it with 4.

The installation method of opencv-python introduced on this page is on the following page. <piwheels opencv-python> https://www.piwheels.org/project/opencv-python/

__ ● Result: The installation was completed by the following procedure. __

Except for the version of opencv-python, the installation method is the same as the piwheel page. Paste the command into the Raspberry Pi terminal and execute it.

<Procedure 1> First, upgrade apt and pip

(Update the list of package management tools and update installed packages)

sudo apt update
sudo apt upgrade
sudo pip install --upgrade pip

<Step 2> Installation of dependent libraries

sudo apt install libavutil56 libcairo-gobject2 libgtk-3-0 libqtgui4 libpango-1.0-0 libqtcore4 libavcodec58 libcairo2 libswscale5 libtiff5 libqt4-test libatk1.0-0 libavformat58 libgdk-pixbuf2.0-0 libilmbase23 libjasper1 libopenexr23 libpangocairo-1.0-0 libwebp6

<Step 3> Install opencv-python

Installation method according to piwheel (This did not work.)

sudo pip3 install opencv-python

When installing with the above command, it could not be imported, so as a result of investigation, it was found that it can be solved by lowering the version. (The version installed by the above command was 4.1.1.) Install as follows. (This worked.)

sudo pip3 install opencv-python==4.1.0.25

■ Build official OpenCV on Raspberry Pi

<Procedure 1> Install dependent library / build tool cmake

<Step 2> Download OpenCV source

<Procedure 3> Compile

__ <Procedure 1> ~ <Procedure 3> are all described in OpencvInstall.sh below. __

□ Reference page For the option to link CMake's OpenCV and Python, I referred to the installation page for linux on the official page of OpenCV. <OpenCV Installation in Linux> https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html

I referred to this page for the library to be installed. https://github.com/atinfinity/lab/wiki/Linux%E7%92%B0%E5%A2%83%E3%81%A7%E3%81%AEOpenCV%E3%83%93%E3%83%AB%E3%83%89

I referred to CMake options. \ <8th OpenCV development for the first time-Customization of OpenCV using CMake [OpenCV 3.1.0]> https://www.atmarkit.co.jp/ait/articles/1704/10/news134.html

OpencvInstall.sh



#Package management tool update (apt-Be sure to do it when installing with get. )
sudo apt-get -y update
sudo apt-get -y upgrade

#Download the library by referring to the Github page
#Development tools
sudo apt-get -yV install build-essential
sudo apt-get -yV install cmake
#Matrix operation
sudo apt-get -yV install libeigen3-dev
#GUI framework related
sudo apt-get -yV install libgtk-3-dev
sudo apt-get -yV install qt5-default
sudo apt-get -yV install libvtk7-qt-dev
sudo apt-get -yV install freeglut3-dev
#Parallel processing related
sudo apt-get -yV install libtbb-dev
#Image format related
sudo apt-get -yV install libjpeg-dev
sudo apt-get -yV install libopenjp2-7-dev
sudo apt-get -yV install libpng++-dev
sudo apt-get -yV install libtiff-dev
sudo apt-get -yV install libopenexr-dev
sudo apt-get -yV install libwebp-dev
#Video related
sudo apt-get -yV install libavresample-dev
#Other
sudo apt-get -yV install libhdf5-dev
#Python related
sudo apt-get -yV install libpython3-dev
sudo apt-get -yV install python3-numpy python3-scipy python3-matplotlib

#Install git (used when downloading the source)
sudo apt-get -y install git

#Source download
cd /usr/local
sudo mkdir opencv4
cd /usr/local/opencv4
sudo git clone https://github.com/opencv/opencv.git
sudo git clone https://github.com/opencv/opencv_contrib.git

#Creating a directory for build (It is a good practice to create a build directory and build in it.)
cd opencv
sudo mkdir build
cd build

#Build
#Basically, I referred to the OpenCV official page.
sudo cmake \
-D CMAKE_BUILD_TYPE=Release \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=/usr/local/opencv4/opencv_contrib/modules \
PYTHON3_EXECUTABLE=/usr/lib/python3.7 \
PYTHON_INCLUDE_DIR=/usr/include/python3.7 \
PYTHON_INCLUDE_DIR2=/usr/include/arm-linux-gnueabihf/python3.7m \
PYTHON_LIBRARY=/usr/lib/arm-linux-gnueabihf/libpython3.7m.so \
PYTHON3_NUMPY_INCLUDE_DIRS =/usr/lib/python3/dist-packages/numpy/core/include \
-S /usr/local/opencv4/opencv

sudo make -j7
sudo make install

2. Check the operation of OpenCV

Check if it can be imported

Execute the following command on the terminal, and if you do not get an importError when you set ʻimport cv2`, it is OK.

#Start the interpreter
python3
#import
import cv2
#Check OpenCV version
cv2.__version__
#Exit of interpreter
exit()
Operation check by camera reading sample program

After enabling the camera (you can set it from Raspberry Pi Settings> Interface> Camera), try running this program. If OpenCV is running, you should see a window showing the camera image (if you run it using an editor, you may see a window in the back of the editor).

camera.py


import cv2
#numpy is needed to store image data.
import numpy as np


def camera():
    cap = cv2.VideoCapture(0)
    isOpened = cap.isOpened()
    if isOpened is False:
        return
    while True:
        result, frame = cap.read()
        if result is False:
            return
        #Image display
        cv2.imshow('camera', frame)
        #Key input reception
        key = cv2.waitKey(1)
        #Exit key (Exit with Enter or Esc)
        if (key == 13) or (key == 27):
            break
    #Camera end
    cap.release()
    cv2.destroyAllWindows()

camera()

3. About OpenCV license

It's open source, but there are two caveats. Please check the license for commercial use.

OpenCV is BCD license

Programs that use the main module of OpenCV can be freely used, modified, and redistributed, but the copyright information and license must be displayed at that time. \ https://opencv.org/license/

There was a very easy-to-understand article about licensing, so please take a look here. \ https://qiita.com/lovee/items/484ae3fc038314a64ee2

Some contrib modules are not commercially available

-SURF included in the contrib module cannot be used for commercial purposes.

You can check it from download on the official page. \ http://people.ee.ethz.ch/~surf/

\ <modules that use github opencv-contrib / surf> https://github.com/opencv/opencv_contrib/tree/master/modules/xfeatures2d

-SIFT seems to have expired on March 6, 2020 and moved to the main module.

4. Finally

Thank you for reading this far. If you find it helpful or have installed it, please give me a like (LGTM) m (_ _ m) Also, if you have any suggestions, we would appreciate it if you could contact us!

Recommended Posts

OpenCV installation procedure on Raspberry Pi
Install OpenCV4 on Raspberry Pi 3
pigpio on Raspberry pi
Cython on Raspberry Pi
Introduced pyenv on Raspberry Pi
Use NeoPixel on Raspberry Pi
Install TensorFlow 1.15.0 on Raspberry Pi
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Installation of Docker on Raspberry Pi and L Chika
Troubleshoot with installing OpenCV on Raspberry Pi and capturing
Testing uart communication on Raspberry Pi
MQTT on Raspberry Pi and Mac
raspberry pi 4 centos7 install on docker
Easy installation of OpenCV on RaspberryPi 3+
Install ghoto2 on Raspberry Pi (memo)
Try using ArUco on Raspberry Pi
Power on / off Raspberry pi on Arduino
Detect switch status on Raspberry Pi 3
Install OpenMedia Vault 5 on Raspberry Pi 4
L Chika on Raspberry Pi C #
Build wxPython on Ubuntu 20.04 on raspberry pi 4
"Honwaka Notification Lamp" on Raspberry Pi Part 2
Detect "brightness" using python on Raspberry Pi 3!
USB boot on Raspberry Pi 4 Model B
"Honwaka Notification Lamp" on Raspberry Pi Part 1
Enable UART + serial communication on Raspberry Pi
Adafruit Python BluefruitLE works on Raspberry Pi.
Accelerate Deep Learning on Raspberry Pi 4 CPU
Set swap space on Ubuntu on Raspberry Pi
Programming normally with Node-RED programming on Raspberry Pi 3
Use the Grove sensor on the Raspberry Pi
Install 64-bit OS (bate) on Raspberry Pi
Install docker-compose on 64-bit Raspberry Pi OS
Run servomotor on Raspberry Pi 3 using python
"Honwaka Notification Lamp" on Raspberry Pi Part 3
Working with sensors on Mathematica on Raspberry Pi
Build OpenCV-Python environment on Raspberry Pi B +
Detect temperature using python on Raspberry Pi 3!
Mount Windows shared folder on Raspberry Pi
Matrix multiplication on Raspberry Pi GPU (Part 2)
How to install NumPy on Raspberry Pi
I installed OpenCV-Python on my Raspberry Pi
Working with GPS on Raspberry Pi 3 Python
Pylearn 2 installation procedure
OpenCV3 installation battle
Raspberry Pi backup
PostgreSQL 10.0 installation procedure
blockdiag installation procedure
Why detectMultiScale () is slow on Raspberry Pi B +
Detect slide switches using python on Raspberry Pi 3!
Build a Django environment on Raspberry Pi (MySQL)
Detect magnet switches using python on Raspberry Pi 3!
Enjoy electronic work with GPIO on Raspberry Pi
Use Grove-Temperature & Humidity Sensor (DHT11) on Raspberry Pi
Make DHT11 available on Raspberry Pi + python (memo)
Beginning cross-compilation for Raspberry Pi Zero on Ubuntu
Sound the buzzer using python on Raspberry Pi 3!
Play with your Ubuntu desktop on your Raspberry Pi 4
Display CPU temperature every 5 seconds on Raspberry Pi 4
Introduced Ceph on Kubernetes on Raspberry Pi 4B (ARM64)
Connect to MySQL with Python on Raspberry Pi