How to install OpenCV on Raspberry Pi

OpenCV can use a multi-core processing, and having a GPU acceleration, enabling real-time operation.

In this tutorial, we will explain how to install OpenCV on the Raspberry Pi.

prerequisites

We assume that you installed on Raspbian Pi Raspberry .

Install OpenCV from Source

The most recommended way to install the OpenCV library is built from source code. This allows you full control over build options, OpenCV will be optimized for your system.

First, increase the swap space to avoid compiler to hang due to memory issues:

sudo nano /etc/dphys-swapfile

The CONF_SWAPSIZE value changed from the default value of 100 to 1024:

的 /etc/dphys-swapfile

CONF_SWAPSIZE=1024

Save the file and run the following command for the changes to take effect:

sudo /etc/init.d/dphys-swapfile restart

We can now begin to build OpenCV. First, the index update package and install the build tools and the required dependencies:

sudo apt update
sudo apt install build-essential cmake git pkg-config libgtk-3-dev libcanberra-gtk*
sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev
sudo apt install libjpeg-dev libpng-dev libtiff-dev gfortran openexr libatlas-base-dev opencl-headers
sudo apt install python3-dev python3-numpy libtbb2 libtbb-dev libdc1394-22-dev

Create a new directory and cloned from Github and OpenCV OpenCV contrib repository:

mkdir ~/opencv_build && cd ~/opencv_build
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

At this writing, the default version GitHub repository is version 4.1.1. If you want to install an older version of OpenCV, navigation and opencv_contrib opencv both directory and run git checkout <opencv-version>

After cloning the repository, creates a temporary build directory, then switch to the directory:

mkdir -p ~/opencv_build/opencv/build
cd ~/opencv_build/opencv/build

Use OpenCV CMake build configuration settings:

cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_C_EXAMPLES=OFF \
    -D INSTALL_PYTHON_EXAMPLES=OFF \
    -D OPENCV_GENERATE_PKGCONFIG=ON \
    -D ENABLE_NEON=ON \
    -D ENABLE_VFPV3=ON \
    -D BUILD_TESTS=OFF \
    -D OPENCV_ENABLE_NONFREE=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
    -D BUILD_EXAMPLES=OFF ..

The output is shown below:

...
-- Configuring done
-- Generating done
-- Build files have been written to: /home/pi/opencv_build/opencv/build

Run make to start the compilation process:

make -j4

This process will take some time, about 1-2 hours, depending on the Raspberry Pi model. Once complete, you will see the following:

...
[100%] Linking CXX shared module ../../lib/python3/cv2.cpython-35m-arm-linux-gnueabihf.so
[100%] Built target opencv_python3

If the compilation fails at some point, because the resources are not available, please make run the command again, the process continues from the stop position.

The final step is to install compiled OpenCV file:

sudo make install
...
-- Installing: /usr/local/bin/opencv_version
-- Set runtime path of "/usr/local/bin/opencv_version" to "/usr/local/lib"

To check OpenCV has been successfully installed, type the following command, you should see OpenCV version:

C ++ libraries:

pkg-config --modversion opencv4
4.1.1

Python library:

python3 -c "import cv2; print(cv2.__version__)"
4.1.1-pre

Clear up

If there is not enough free space on the SD card, please delete the source files:

rm -rf ~/opencv_build

Use a large number of exchange may damage your SD card. The swap space to change back to the original size:

sudo nano /etc/dphys-swapfile

The CONF_SWAPSIZE edit the value of 100:

File / etc / dphys-swapfile

CONF_SWAPSIZE=100

Save the file and activate the changes:

sudo /etc/init.d/dphys-swapfile restart

in conclusion

We've shown you how to install OpenCV in the Raspberry Pi board. OpenCV build from source to provide you with greater flexibility, it should be the preferred method to use when you install OpenCV.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159878.htm