Install guide for OpenCV 3 on the Raspberry Pi

OpenCV (Open Source Computer Vision) is a library of programming functions primarily aimed at real-time computer vision. OpenCV supports the deep learning frameworks TensorFlow, Torch / PyTorch and Caffe. Especially deep learning and Caffe is important in this Raspberry Pi project.

Install OpenCV

The first thing you should do is expand your filesystem to include all available space on your micro-SD card:

sudo raspi-config

Once prompted, you should select the first option, „1. Expand File System, hit Enter on your keyboard, arrow down to the „<Finish>“ button, and then reboot your Pi:

sudo reboot

After rebooting, your file system should have all available space on your micro-SD card. You can verify that the disk has been expanded by executing df -h and examining the output:

df -h

The size of the filesystem should be almost the same size as the size of the SD card.

 

Install dependencies

The first step is to update and upgrade any existing packages:

sudo apt-get update
sudo apt-get upgrade
sudo rpi update

Install some developer tools, including CMake, which helps to configure the OpenCV build process:

sudo apt-get install build-essential cmake pkg-config

Next, we need to install some image I/O packages that allow us to load various image file formats from disk. Examples of such file formats include JPEG, PNG, TIFF, etc.:

sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev

Just as we need image I/O packages, we also need video I/O packages. These libraries allow us to read various video file formats from disk as well as work directly with video streams:

sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev

The OpenCV library comes with a sub-module named highgui  which is used to display images to our screen and build basic GUIs. In order to compile the highgui  module, we need to install the GTK development library:

sudo apt-get install libgtk2.0-dev

Many operations inside of OpenCV (namely matrix operations) can be optimized further by installing a few extra dependencies:

sudo apt-get install libatlas-base-dev gfortran

These optimization libraries are especially important for resource constrained devices such as the Raspberry Pi.

Lastly, install the Python 2.7 or Python 3 header files, depending which Pyhton version you want to use. In this project we used 2.7:

sudo apt-get install python2.7-dev

OR

sudo apt-get install python3-dev

 

Download the OpenCV source code

Now it its necessary to install the desired version archive of OpenCV from the official OpenCV repository. In this case we took the version 3.4.1. The most recent version can be seen on the OpenCV website: https://opencv.org/

cd ~
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.4.1.zip
unzip opencv.zip

We wanted the full install of OpenCV 3 so we also needed to grab the opencv_contrib repository as well:

wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.4.1.zip
unzip opencv_contrib.zip

Make sure your opencv  and opencv_contrib  versions are the same!

After that you have to install  pip, a Python package manager and NumPy, a Python package used for numerical processing:

wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
pip install numpy

Compile and Install OpenCV

First go to the directory of OpenCV then setup the build using CMake.

cd ~/opencv-3.4.2/
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.4.1/modules \
    -D BUILD_EXAMPLES=ON ..

examine the output of CMake!

Start by scrolling down the section titled Python 2  and Python 3 .

If you are compiling OpenCV 3 for Python 2.7, then make sure your Python 2  section includes valid paths to the Interpreter , Libraries , numpy  and packages path.

Finally, it is possible to compile OpenCV:

make -j4

The -j4  command controls the number of cores to leverage when compiling OpenCV 3

This step can take hours to finish.

After that OpenCv can be installed:

sudo make install
sudo ldconfig

OpenCv should be installed now on the Raspberry Pi 3. To verify this use the ls command.

For python 2.7:

ls -l /usr/local/lib/python2.7/site-packages/

In some cases, OpenCV can be installed in /usr/local/lib/python2.7/dist-packages  (note the dist-packages  rather than site-packages . If you do not find the cv2.so  bindings in site-packages , be sure to check dist-packages .

For python 3:

ls -l /usr/local/lib/python2.7/site-packages/

 

Schreibe einen Kommentar