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++.
- A Raspberry Pi
- A 3gb or more SD Card with the Raspbian image
- A working Internet connection
- A way to see Rapsi’s desktop environment (for testing)
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).
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”.
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:
– 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
– Python: YES
– Python interpreter: /usr/bin/python -B (ver 2.7)
– Python numpy: YES
– Java: NO
– 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:
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:
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:
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!