Setting everything up for OpenCV – Raspberry Pi

Setting OpenCV up on the Raspberry Pi took me two attempts and about 20 hours. I’m writing this guide so others don’t have to go through all the problems. I will mainly use OpenCV with Python; I haven’t tested it with C/C++.

You will need:
  1. A Raspberry Pi
  2. A 3gb or more SD Card with the Raspbian image
  3. A working Internet connection
  4. A way to see Rapsi’s desktop environment (for testing)
  5. Patience
Optional:
  1. PuTTY

Attempt #1 – Following other guides

Googling about OpenCV and Raspberry Pi leads to a few guides how to set it up (this and this). They helped a lot, but they didn’t fully work for me. However, without them I wouldn’t be writing this right now.

First of all, you need to install all dependencies. You can get most of them by running the following commands:

sudo apt-get -y install build-essential cmake cmake-qt-gui pkg-config libpng12-0 libpng12-dev libpng++-dev libpng3 libpnglite-dev zlib1g-dbg zlib1g zlib1g-dev pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools

sudo apt-get -y install libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-progs ffmpeg libavcodec-dev libavcodec53 libavformat53 libavformat-dev libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev libxine1-ffmpeg libxine-dev libxine1-bin libunicap2 libunicap2-dev libdc1394-22-dev libdc1394-22 libdc1394-utils swig libv4l-0 libv4l-dev python-numpy libpython2.6 python-dev python2.6-dev libgtk2.0-dev pkg-config

And that’s about it from the other guides that we can use. I’m not sure if we really need Python 2.6, as 2.7 comes preinstalled. I wasn’t in the mood for testing at that time. Either way, I followed the guides, downloaded the version they recommended, run CMake, started building, and around 4.5 hours later I had build something. Sadly, it didn’t work. As it later turned out, it had something to to with the version of ffmpeg (it was too new).

Attempt #2 – Figuring it out by myself

So.. what now? First, let’s download the latest version of OpenCV (v2.4.2 as of writing this).

wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.2/OpenCV-2.4.2.tar.bz2/download

It’s quite heavy; it will take some time to download. It downloaded for me in a file named “download”. I’m not quite sure why it wasn’t named properly, but whatever. Let’s unpack it:

tar -xvjpf download

(or whatever name it has on your machine)

We go inside the new directory that we got with unpacking, and make a new one within it. I named my “build”.

cd OpenCV-2.4.2/
mkdir build
cd build

And, now we are ready to build! Or not. Yeah, we aren’t ready at all. We need to run CMake and see what is says. Here is now a difference to the other guides I found – we need to replace BUILD_PYTHON_SUPPORT with BUILD_NEW_PYTHON_SUPPORT. Run:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_NEW_PYTHON_SUPPORT=ON -D BUILD_EXAMPLES=ON ..

As I wasn’t planning on writing this, I haven’t backed my first CMake output up. There will be some tests run, and some will fail. But that’s ok! Watch carefully and note the names of the missing libs. Install everything that’s missing. For example, if it says that it can’t find gtk+-2.0, install it (sudo apt-get install gtk+-2.0). I had to install a few things, including some Python modules. If you can’t figure out the package name of a module, just Google it. My CMake output looked somewhat like this before building:

GUI:
– GTK+ 2.x: YES
– GThread: YES

– Media I/O:
– ZLib: YES
– JPEG: TRUE
– PNG: TRUE
– TIFF: TRUE
– JPEG 2000: TRUE
– OpenEXR: NO
– OpenNI: NO
– OpenNI PrimeSensor Modules: NO
– XIMEA: NO

– Video I/O:
– DC1394 1.x: NO
– DC1394 2.x: YES
– FFMPEG: YES
– codec: YES
– format: YES
– util: YES
– swscale: YES
– gentoo-style: YES
– GStreamer: YES
– UniCap: NO
– PvAPI: NO
– V4L/V4L2: Using libv4l
– Xine: NO

– Other third-party libraries:
– Use IPP: NO
– Use TBB: NO
– Use ThreadingFramework: NO
– Use Cuda: NO
– Use Eigen: NO

– Interfaces:
– Python: YES
– Python interpreter: /usr/bin/python -B (ver 2.7)
– Python numpy: YES
– Java: NO

– Documentation:
– Sphinx: /usr/bin/sphinx-build (ver 1.1.3)
– PdfLaTeX compiler: NO
– Build Documentation: YES (only HTML without math formulas)

As I already said, just read carefully and install the missing pieces.

When you install everything you need, it’s time to build. Simply run:

make

This will take a really, really long time. Long as in 10 hours and 4 minutes. Just a tip: don’t think it froze. It will look sometimes as if it froze and there is no hope. Just wait. When it finishes, run the last command:

make install

Let it do it’s magic. Congratulations, you just compiled OpenCV on your Raspberry Pi.

Try it out

Start your desktop environment. Open up the Terminal and go to OpenCV-2.4.2/samples/python. Run a sample:

python drawing.py

And it should work. I went ahead and tried to access the webcam stream, and:

I hope this guide will help you. If you have any question, feel free to ask! Happy coding!

16 Comments

  1. May I know where I can get the package ‘gstreamer-base-0.10′ because it is not available in debian package online repository “http://packages.debian.org/” ?

  2. Use sudo apt-get update before installing new packages to make sure that everything is up to date. This article is a little older now and I haven’t since then tried to compile it again, so I don’t know if gstreamer-base-0.10 is even available anymore.

  3. Initially i tried your guide, but it did not work because too much RAM was allocated to the GPU… I got some internal error message. But after reducing the RAM allocated to the GPU your guide worked fine… Kudos to you !!!

  4. When I first saw this title Setting everything up for OpenCV – Raspberry Pi – D's Lab Log on google I just whent and bookmark it. Whats up. Very cool blog!! Guy .. Beautiful .. Superb .. I’ll bookmark your website and take the feeds also…I am satisfied to find numerous helpful information here within the article. Thank you for sharing..

  5. Hi, I tried to do the install by your advice and it seemes to go ok. But when I try to run examples, I get error that there is no cv2. I’m also getting error from trying to import cv. I’m so newbie with this, I would be very grateful for any help…

  6. That’s exactly what I got on my first try. Have you made sure that all the dependencies are installed? Check your CMake log and make sure it looks like mine did. And be aware that it takes a really long time to compile, so if it finished in a few minutes or just 1 or 2 hours, something went bad. Good luck!

  7. Hi…
    A very cool post.. seriously… but… i am stuck at a point..
    i am unable to pass away from 61% after executing “make”.. i have been striking my head all around from last 2 weeks… but as last hope i am posting it to you.. hope u would help
    [ 61%] Building CXX object modules/gpu/CMakeFiles/opencv_test_gpu.dir/test/test_core.cpp.o
    c++: internal compiler error: Killed (program cc1plus)
    Please submit a full bug report,
    with preprocessed source if appropriate.
    See for instructions.
    make[2]: *** [modules/gpu/CMakeFiles/opencv_test_gpu.dir/test/test_core.cpp.o] Error 4
    make[1]: *** [modules/gpu/CMakeFiles/opencv_test_gpu.dir/all] Error 2
    make: *** [all] Error 2

    An immidiate reply is required…

  8. It could really be many things. One that comes to mind is the memory limit you have on a Raspberry Pi. Try giving less memory to the GPU, as you don’t really the GPU for compiling.

    But you probably don’t have to compile it anymore. It seems like there is a package in synaptic, so you can just download it and start working right away! (http://hackaday.com/2013/03/04/using-opencv-with-the-raspberry-pi/)
    Sadly, my Pi is broken so I can’t check :/

  9. Hi, My make also stops at 61%. How much ram should I keep for the GPU? Please see below for a snapshot of the error message I got when running make.
    [ 67%] Building CXX object modules/gpu/CMakeFiles/opencv_gpu.dir/src/denoising.cpp.o
    [ 67%] Building CXX object modules/gpu/CMakeFiles/opencv_gpu.dir/src/bilateral_filter.cpp.
    o
    Linking CXX shared library ../../lib/libopencv_gpu.so
    [ 67%] Built target opencv_gpu
    [ 67%] Generating opencv_perf_gpu_pch_dephelp.cxx
    [ 68%] Building CXX object modules/gpu/CMakeFiles/opencv_perf_gpu_pch_dephelp.dir/opencv_p
    erf_gpu_pch_dephelp.cxx.o
    Linking CXX static library ../../lib/libopencv_perf_gpu_pch_dephelp.a
    [ 68%] Built target opencv_perf_gpu_pch_dephelp
    [ 68%] Generating perf_precomp.hpp
    [ 68%] Generating perf_precomp.hpp.gch/opencv_perf_gpu_RELEASE.gch
    [ 68%] Built target pch_Generate_opencv_perf_gpu
    [ 68%] Building CXX object modules/gpu/CMakeFiles/opencv_perf_gpu.dir/perf/perf_precomp.cp
    p.o
    [ 68%] Building CXX object modules/gpu/CMakeFiles/opencv_perf_gpu.dir/perf/perf_video.cpp.
    o
    [ 69%] Building CXX object modules/gpu/CMakeFiles/opencv_perf_gpu.dir/perf/perf_features2d
    .cpp.o
    [ 69%] Building CXX object modules/gpu/CMakeFiles/opencv_perf_gpu.dir/perf/perf_calib3d.cp
    p.o
    [ 69%] Building CXX object modules/gpu/CMakeFiles/opencv_perf_gpu.dir/perf/perf_labeling.c
    pp.o
    [ 69%] Building CXX object modules/gpu/CMakeFiles/opencv_perf_gpu.dir/perf/perf_denoising.
    cpp.o
    [ 69%] Building CXX object modules/gpu/CMakeFiles/opencv_perf_gpu.dir/perf/perf_main.cpp.o
    [ 69%] Building CXX object modules/gpu/CMakeFiles/opencv_perf_gpu.dir/perf/perf_core.cpp.o
    c++: internal compiler error: Killed (program cc1plus)
    Please submit a full bug report,
    with preprocessed source if appropriate.
    See for instructions.
    make[2]: *** [modules/gpu/CMakeFiles/opencv_perf_gpu.dir/perf/perf_core.cpp.o] Error 4
    make[1]: *** [modules/gpu/CMakeFiles/opencv_perf_gpu.dir/all] Error 2
    make: *** [all] Error 2

  10. Last command “make install” is better if is “sudo make install” because i notice that without admin privileges installation it gives an error when try to write in usr/local/include/opencv2

  11. It would be wonderfull if there was a linux distribution developed for Computing Vision, so we should only install it.
    I come back to dream

Leave a Comment.