OpenCV Installation - Rasbian Buster, Jessie, DietPi Buster

In this post, we will install OpenCV on Raspberry Pi . Raspberry Pie doesn't have a GPU, so you don't need to set up acceleration with CUDA. Therefore, it is easiest to install a package that has already been created. But old version of Rasbian(Jessie) doesn't have python package. So you have to build the source code to install the OpenCV. And I'm going to install OpenCV 4.1.1 to DietPi. DietPi is a lightweight version of Debian.


Prepare Rasbian Buster headless sd card image

Prepare the image using Etcher. I used 2019-09-26-raspbian-buster-lite.zip image. Do not boot the Raspberry Pi immediately after creating the image, but create a size 0 ssh file on the / boot partition. The reason for this file is to make it a headless image. This allows remote ssh operations without having to connect a monitor to the Raspberry Pi. However, to get an IP address using dhcp, the network cable must be connected.

<Create ssh file in the boot partition>

Then run the raspi-config for localisation setup.

sudo raspi-config
sudo apt-get update
sudo apt-get -y upgrade


OpenCV installation for Rasbian Buster

Install packages


sudo apt-get update
sudo apt-get install -y build-essential cmake pkg-config python3-dev python3-pip python-dev
sudo apt-get install -y libatlas-base-dev gfortran  openexr libqtgui4 libqt4-test   
sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libjasper-dev libilmbase-dev libopenexr-dev
sudo apt-get install -y libgraphicsmagick++-dev libwebp-dev  python3-pil
pip3 install opencv-python 


Edit environment variable (Not needed for OpenCV 4.5)

add this line (export LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1) to ~/.bash_profile


nano ~/.bashrc
 
#"insert this line" at the end
export LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1
 
#then reload the environment
source ~/.bashrc  

Test


pi@raspberrypi:~ $ python3
Python 3.7.3 (default, Dec 20 2019, 18:57:59)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> import numpy
>>> cv2.__version__
'4.1.1'
>>>

If you installed without error, you can see that OpenCV 4.1.1 version is installed as above.



OpenCV installation for Rasbian Jessie

Rasbian jessie does not need to create an ssh file on the / boot partition because ssh is enabled in the initial image. After creating the image, you can boot directly and connect directly to ssh from the remote PC. I used  "2016-02-26-raspbian-jessie-lite.zip" image.

First you should run raspi-config and expand the filesystem.  Older versions of Raspbian must extend the file system manually. If you do not do this, an error will occur during the operation due to insufficient SD card space. And setup the localisation(menu 5).

Then reboot the system.

Install packages

Jessie does not support installing packages using pip3. Therefore, you must build the source code yourself. Therefore, there are many packages to install compared to Buster.

sudo apt-get install -y python-dev  python3-dev python-pip  python3-pip  python-tk  python3-tk  python-numpy  python3-numpy
sudo apt-get install -y build-essential cmake pkg-config libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev  x264 v4l-utils openexr libqtgui4 libqt4-test 
sudo apt-get install -y libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev  libgtk2.0-dev libgtk-3-dev  libatlas-base-dev gfortran  python-qt4
sudo apt-get install -y libcanberra-gtk*  libdc1394-22-dev  libxine2-dev  libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev  libqt4-opengl-dev qt5-default
sudo apt-get -y install libprotobuf-dev protobuf-compiler libgoogle-glog-dev libgflags-dev libgphoto2-dev libeigen3-dev libhdf5-dev doxygen

Download the source codes and build them

I downloaded the OpenCV 3.1 version source code here. If you want a higher version of the code, give it a try.

cd /usr/local/src
sudo wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip
sudo unzip opencv.zip
sudo wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip
sudo unzip opencv_contrib.zip
cd /usr/local/src/opencv-3.1.0/



Modify the common.cmake file

Before run cmake command, modify the "/usr/local/src/opencv-3.1.0/modules/python/common.cmake" file.
Just add the following two lines to the beginning of the file.

find_package(HDF5)
include_directories(${HDF5_INCLUDE_DIRS})




# This file is included from a subdirectory
set(PYTHON_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../")
 
#include these 2 lines at the beginning 
find_package(HDF5)
include_directories(${HDF5_INCLUDE_DIRS})

ocv_add_module(${MODULE_NAME} BINDINGS)

Run cmake



cd /usr/local/src/opencv-3.1.0/
sudo mkdir build
cd build
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=../..//opencv_contrib-3.1.0/modules \
    -D BUILD_TESTS=OFF \
    -D OPENCV_ENABLE_NONFREE=ON \
    -D ENABLE_PRECOMPILED_HEADERS=OFF \
    -D BUILD_opencv_python2=ON \
    -D BUILD_opencv_python3=ON \
    -D BUILD_EXAMPLES=OFF ..


If cmake is finished and the following screen is displayed, you are finished.


Build and install.



sudo make -j4
sudo make install
ldconfig

Test

If the installation was successful, you can test it with:


pi@raspberrypi:~ $ python3
Python 3.4.2 (default, Sep 16 2019, 19:58:00)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.1.0'
>>>




OpenCV installation for DietPi Buster

DietPi is a very lightweight Debian image for Raspberry Pi. Raspberry Pi 4 is only available for Raspbian OS Buster. However, when using the RGB LED matrix in Raspbian Buster, the screen becomes unstable. This can cause the Rasberry Pi4 to fail. To use the Raspberry Pi 4 with the RGB LED Matrix, you need a version of DietPi Buster that can be installed on the Raspberry Pi 4. My tests show that unlike the Rasbian Buster, the RGB LED Matrix works fine on DietPi Buster. I explained this problem on my other blog(https://iot-for-maker.blogspot.com/2020/02/led-5-lets-make-large-led-display-part.html).

Prepare DietPi Buster image

Download the image at https://dietpi.com/#download. Then make a SD card image with Etcher tool. Log in account is "root/dietpi".
Like the Raspbian Buster, create a file of size 0 "ssh" on the boot partition. On first boot, dietpi's initial configuration screen will appear automatically.

<Diet Pi initial setup screen>

Most of the time, you can proceed with the default values, but you can change the configuration of the ssh server to OpenSSH. You might encounter some sftp problems when using the default Dropbear ssh server.
Be Careful : Then you should go to Install menu and press"OK" to install OpenSSH.


You can run the program later. The name is "dietpi-software".
After finishing the initial setup, reboot the system.

Be Careful: Dietpi automatically configures the root user to access ssh. Therefore, there is no need to modify "PermitRootLogin yes" in /etc/ssh/ sshd_config file.

Edit environment variable

add this line (export LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1) to ~/.bash_profile


nano ~/.bashrc
 
#"insert this line" at the end
export LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1
 
#then reload the environment
source ~/.bashrc  

Install packages

The installation process is the same as for Raspbian Buster. Both products are based on Debian Buster.

sudo apt-get update
sudo apt-get install -y build-essential cmake pkg-config python3-dev python-dev python3-pip
sudo apt-get install -y libatlas-base-dev gfortran  openexr libqtgui4 libqt4-test libgtk-3-0  
sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libjasper-dev libilmbase-dev libopenexr-dev
pip3 install opencv-python 
 

Test


root@DietPi:~# python3
Python 3.7.3 (default, Dec 20 2019, 18:57:59)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> import numpy
>>> cv2.__version__
'4.1.1'



Wrapping up

I have installed OpenCV on three operating systems. Rasbian Buster and DietPi Buster go through the same installation process. However, since the initial part of the image is different, pay attention to the initial part after creating the first image. Rasbian Jessie doesn't use much at the moment, but personally I use it sometimes with RGB LED Matrix or with older hardware. Rasbian Jessie is a bit tricky to install because it doesn't provide OpenCV Python packages. After installing the libraries required to build the package, download the source code and install it after the build process.
For reference, Jetson Nano's latest JetPack 4.3 (December 2019) has OpenCV 4.1 installed and does not need to be installed.





댓글

이 블로그의 인기 게시물

Image Processing #7 - OpenCV Text

Playing YouTube videos using OpenCV