Raspberry Pi / Setup / Installing OpenCV

Raspberry Installing OpenCV

By Marcelo Fernandes Jul 30, 2017

Installing OpenCV on raspberry

Installing OpenCV might be a little bit tricky, so I made this guide to make things more straight forward.

Prerequisites:

# Update the raspberry pi.
sudo apt-get update
sudo apt-get upgrade
sudo rpi-update

# Install developer tools required to build OpenCV from source.
sudo apt-get install build-essential git cmake pkg-config

# Install packages used to load various image formats from disk:
sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev

# Video I/O packages
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev

# Install GTK which handles OpenCV's GUI operations:
sudo apt-get install libgtk2.0-dev

# We can optimize various functions (such as matrix operations), by installing the following packages:
sudo apt-get install libatlas-base-dev gfortran

# Python 2.7 and python 3 header files
sudo apt-get install python2.7-dev python3-dev

# Get pip if you don't have it already.
wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py


Getting the repository and other utilities.


# OpenCV repository.
cd
mkdir opencv_installation
cd opencv_installation
git clone https://github.com/opencv/opencv.git
cd opencv
git checkout 3.0.0

# For the full, complete install of OpenCV 3.0, grab the opencv_contrib repo as well:
cd ..
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
# make sure to get the version that matches.
git checkout 3.0.0


Make the virtual environment


# Get virtualenv
pip install virtualenv

# make a virtualenv with python3
cd
virtualenv -p /usr/bin/python3 env_opencv

# activate the virtual env
source env_opencv/bin/activate

# install numpy (might take some time)
pip install numpy


Compile and install OpenCV


cd
cd opencv_installation/opencv
mkdir build
cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE \
	-D CMAKE_INSTALL_PREFIX=/usr/local \
	-D INSTALL_C_EXAMPLES=OFF \
	-D INSTALL_PYTHON_EXAMPLES=ON \
	-D OPENCV_EXTRA_MODULES_PATH=/home/pi/opencv_installation/opencv_contrib/modules \
	-D BUILD_EXAMPLES=ON ..

# in the end of the above command, check if the python3 interpreter/libraries/numpy path is correct.

# use the 4 cores to make the installation:
make -j4

# if you run into errors run it with only 1 core (will take much longer):
make clean
make

# finally:
sudo make install
sudo ldconfig

Finishing:


# Open cv must be installed at:
ls /usr/local/lib/python3.4/site-packages/

# you should see the following line:
# cv2.cpython-34m.so

# Rename it to a more convinient name
cd /usr/local/lib/python3.4/site-packages/
mv cv2.cpython-34m.so cv2.so

# The last step here is to sym-link the OpenCV bindings into the virtual environment:
cd
cd env_opencv/lib/python3.4/site-packages/
ln -s /usr/local/lib/python3.4/site-packages/cv2.so cv2.so

# Verify the install:
python
>>> import cv2
>>> cv2.__version__
3.0.0


Basic python snippet to test it:


import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # capture frame-by-frame
    ret, frame = cap.read()

    if ret: # check ! (some webcam's need a "warmup")
        # our operation on frame come here
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # Display the resulting frame
        cv2.imshow('frame', gray)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# When everything is done release the capture
cap.release()
cv2.destroyAllWindows()



Notes


References:


Link 1