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.
__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
-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.
<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/
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.
(Update the list of package management tools and update installed packages)
sudo apt update
sudo apt upgrade
sudo pip install --upgrade pip
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
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
__ <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.
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
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()
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()
It's open source, but there are two caveats. Please check the license for commercial use.
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.
\
There was a very easy-to-understand article about licensing, so please take a look here.
\
You can check it from download on the official page.
\
\ <modules that use github opencv-contrib / surf> https://github.com/opencv/opencv_contrib/tree/master/modules/xfeatures2d
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