Raspberry Pi Rasbian + OpenCV


First of all, building OpenCV on Raspbian (Raspberry Pi) will take at least 4 hours. So consider doing this before you sleep so you can leave it compiling overnight.

And also if you’re doing all this through SSH, linux’s “screen” program will definitely very useful. It’s not installed by default, so do this:

sudo apt-get install screen

Click here for short and quick tutorial of how to use screen.

Now let’s cook!

  1. Prepare fresh installed Rasbian on Raspberry Pi.
  2. Run sudo apt-get update, then sudo apt-get upgrade, to make sure everything is updated.
  3. Copy and paste this into the terminal to install all the dependencies (NOTE: Some package names are altered, e.g. libavcodec52 is not available anymore, replaced by libavcodec53):

    sudo apt-get -y install build-essential
    sudo apt-get -y install cmake
    sudo apt-get -y install pkg-config
    sudo apt-get -y install libpng12-0 libpng12-dev libpng++-dev libpng3
    sudo apt-get -y install libpnglite-dev libpngwriter0-dev libpngwriter0c2
    sudo apt-get -y install zlib1g-dbg zlib1g zlib1g-dev
    sudo apt-get -y install libjasper-dev libjasper-runtime libjasper1
    sudo apt-get -y install pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools
    sudo apt-get -y install libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-prog
    sudo apt-get -y install libavcodec53 libavcodec-dev libavformat53 libavformat-dev libavutil51 libavutil-dev libswscale2 libswscale-dev
    sudo apt-get -y install libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev
    sudo apt-get -y install libxine1-ffmpeg libxine-dev libxine1-bin
    sudo apt-get -y install libunicap2 libunicap2-dev
    sudo apt-get -y install libdc1394-22-dev libdc1394-22 libdc1394-utils
    sudo apt-get -y install swig
    sudo apt-get -y install python-numpy
    sudo apt-get -y install libpython2.6 python-dev python2.6-dev
    sudo apt-get -y install libjpeg-progs libjpeg-dev
    sudo apt-get -y install libgstreamer-plugins-base0.10-dev
    sudo apt-get -y install libqt4-dev libgtk2.0-dev
  4. We need to install other dependencies (x264, ffmpeg, and v4l) manually:
    sudo apt-get remove ffmpeg x264 x264-dev
    wget ftp://ftp.videolan.org/pub/videolan/x264/snapshots/x264-snapshot-20120528-2245-stable.tar.bz2
    tar -xvf x264-snapshot-20120528-2245-stable.tar.bz2
    cd x264-snapshot-20120528-2245-stable/
    ./configure --enable-shared --enable-pic
    sudo make install
    cd ..
    wget http://ffmpeg.org/releases/ffmpeg-0.11.1.tar.bz2
    echo "Installing ffmpeg"
    tar -xvf ffmpeg-0.11.1.tar.bz2
    cd ffmpeg-0.11.1/
    ./configure --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-nonfree --enable-postproc --enable-version3 --enable-x11grab --enable-shared --enable-pic
    sudo make install
    cd ..
    wget http://www.linuxtv.org/downloads/v4l-utils/v4l-utils-0.8.8.tar.bz2
    tar -xvf v4l-utils-0.8.8.tar.bz2
    cd v4l-utils-0.8.8/
    sudo make install
    cd ..
  5. Download desired version of OpenCV, in this example we’re using version 2.4.3. Unpack it anywhere you like.
    tar -xjvf  OpenCV-2.4.3.tar.bz2
    rm OpenCV-2.4.3.tar.bz2
    cd OpenCV-2.4.3/
    mkdir build
    cd build/
  6. Then to create standard configuration just follow this command:

    The part | sudo tee ./CMAKE.log is used for logging purpose. Normally I used redirection (>>) but it won’t work if you are trying to write inside a folder with no permission.

    You can use cmake-gui if it’s more comfortable for you, but it has to be run from a desktop environment (X Server is running). I did this over SSH so I don’t have the luxury of X.

  7. Then a configuration will be generated, make sure everything you need has no problem. For example see the first lines, it should be able to found the libraries needed, such as gtk+2.0, libavcodec, libavformat, etc.
    In my case cmake couldn’t find linux/videodev.h. According to this source, apparently there’s a change of structure for libv4l but OpenCV didn’t take that into account. In my installation I found the file libv4l1-videodev.h inside /usr/local/include, so we need to make a symlink to the location searched by OpenCV installation:

    sudo ln -s /usr/local/include/libv4l1-videodev.h /usr/include/linux/videodev.h

    TODO: there’s also one missing library, ffmpeg/avformat.h, but I’m not sure whether it’s necessary to fix or not.

  8. Now we’re ready to start the build. This process will take a long time to finish. So as said before, consider doing this before you sleep overnight. I do all this through SSH (I only have 1 keyboard and 1 mouse and I don’t want to back and forth plugging them between my laptop and the Raspberry Pi), so here’s where “screen” program is very useful. You can ignore this if you don’t use SSH:
    screen -S OpenCV_Installation

    Seems like nothing happened, but what actually happens is “screen” starts a new TTY/session, which you can build OpenCV, then you can “detach” it so it will run on background. After that you can quit the SSH session and turn off your laptop, without even bothering the building process.

    Now change user into root

    sudo su -

    then execute this as root

    make && make install

    or if you want some logging

    make | tee make.log && make install | tee make_install.log
  9. Now “detach” the compilation “screen” by pressing Ctrl+A then Ctrl+D consecutively. The building process will run in background. To check the process you have to get back to the “screen”, simply call in any session – either SSH or local
    screen -r

    If you use the logging with tee make.log and tee make_install.log as mentioned above, to check the status you can use tail

    tail -f make.log
  10. Sit back, watch some movie, or sleep. See you in 5 hours!


Now that you have energy from your sleep, let’s continue setting up the system to complete the installation.

  1. We have to tell the system that there are libraries available. Create a file named opencv.conf located in /etc/ld.so.conf.d/
    sudo touch /etc/ld.so.conf.d/opencv.conf

    Then using your favorite editor add this line into the file:


    After that:

    sudo ldconfig /etc/ld.so.conf.d
  2. Then we have to add this 2 lines into the file /etc/bash.bashrc
    export PKG_CONFIG_PATH


NOTE: Seems like my Raspberry Pi is unable to detect my cheap Logitech C170 yet. This is weird.

OpenCV comes with lots of pre-compiled sample programs, find it in build/bin folder. Some of them are drawing and kmeans. And if you have USB webcam you can check out lkdemo


Press r then it will start tracking objects. Cool huh?

I also found this over here, to test camera capture using python.

import cv2

vc = cv2.VideoCapture(0)

if vc.isOpened(): # try to get the first frame
    rval, frame = vc.read()
    rval = False

while rval:
    cv2.imshow("preview", frame)
    rval, frame = vc.read()
    key = cv2.waitKey(20)
    if key == 27: # exit on ESC

Save it and run it with python:

python opencv_cam_test.py

For obvious reasons you can’t do this over SSH.

Leave a comment


  1. Amazing….this is a helpful web site.

  2. Hi,

    I’m having problems installing ffmpeg. When I try to configure I get: “ERROR: libfaac not found”. Can you help-me?

  3. Greg Bryant

     /  April 6, 2014

    I must be missing something obvious. I am unable to open a file using the Opencv VideoCapture function in python. Here is what always happens with any video file:

    ~ $ python
    Python 2.7.3 (default, Mar 18 2014, 05:13:23)
    [GCC 4.6.3] on linux2
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import numpy
    >>> import cv2
    >>> cap = cv2.VideoCapture(“~/pirates.mpg”)
    >>> print cap.isOpened()
    >>> print cap.open(“~/pirates.mpg”)
    >>> quit()



  4. You can run the X11 tests if you ssh to the pi using the -X flag

  1. Getting Raspberry Pi, OpenNI2, and Asus Xtion Pro Live To Work Together | ariandyblog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: