Jul 16, 2015

Install OpenCV and Python to Raspberry Pi



New instructions!!!

Step 0:

Again, I’m going to assume that you have just unboxed your Raspberry Pi 2/B+. Open up a terminal and we’ll start by updating and upgrading installed packages, followed by updating the Raspberry Pi firmware:

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

Step 1:

Install the required developer tools and packages:

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

Both build-essential  and pkg-config  are likely already installed, but just in case they are not, be sure to include them in your apt-get command.

Step 2:

Install the necessary image I/O packages. These packages allow you to load various image file formats such as JPEG, PNG, TIFF, etc.

sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev

Step 3:

Install the GTK development library. This library is used to build Graphical User Interfaces (GUIs) and is required for the highgui  library of OpenCV which allows you to view images on your screen:

sudo apt-get install libgtk2.0-dev

Step 4:

Install the necessary video I/O packages. These packages are used to load video files using OpenCV:

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

Step 5:

Install libraries that are used to optimize various operations within OpenCV:

sudo apt-get install libatlas-base-dev gfortran

Step 6:

Install pip :

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

Step 7:

Install  virtualenv  and virtualenvwrapper :

sudo pip install virtualenv virtualenvwrapper
sudo rm -rf ~/.cache/pip

Then, update your ~/.profile  file to include the following lines:

sudo nano ~/.profile

# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

Reload your .profile  file:

source ~/.profile

Create your computer vision virtual environment:

mkvirtualenv cv

Step 8:

Now we can install the Python 2.7 development tools:

sudo apt-get install python2.7-dev

Note: Yes, we are going to use Python 2.7. OpenCV 2.4.X does not yet support Python 3 and OpenCV 3.0 is still in beta. It’s also unclear when the Python bindings for OpenCV 3.0 will be complete so I advise to stick with OpenCV 2.4.X for the time being.

We also need to install NumPy since the OpenCV Python bindings represent images as multi-dimensional NumPy arrays:

pip install numpy

Step 9:

Download OpenCV and unpack it:

wget -O opencv-3.0.0.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/3.0.0/opencv-3.0.0.zip/download
unzip opencv-3.0.0.zip
cd opencv-3.0.0

Setup the build:

mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_NEW_PYTHON_SUPPORT=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON  -D BUILD_EXAMPLES=ON ..

Compile OpenCV:

make

Important: Make sure you’re in the  cv  virtual environment so OpenCV is compiled against the virtual environment Python and NumPy. Otherwise, OpenCV will be compiled against the system Python and NumPy which can lead to problems down the line.

Finally, we can install OpenCV:

sudo make install
sudo ldconfig

More info...