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!

131 Comments

  1. Thank you a lot for giving everyone an exceptionally marvellous chance to check tips from here. It’s always so useful and as well , full of a lot of fun for me and my office mates to visit your site at minimum three times a week to read the fresh guidance you have got. Of course, we are certainly amazed for the astounding opinions served by you. Certain 4 ideas in this article are definitely the simplest we’ve had.

  2. Great post. I was checking continuously this blog and I’m impressed! Extremely helpful information particularly the last part :) I care for such info much. I was looking for this certain information for a long time. Thank you and best of luck.

  3. I and also my friends have already been viewing the good hints found on the website and so the sudden I had a terrible suspicion I never thanked you for those secrets. Those people were totally thrilled to study all of them and have pretty much been enjoying those things. I appreciate you for turning out to be well accommodating and for picking certain high-quality resources millions of individuals are really eager to discover. My personal honest apologies for not expressing appreciation to earlier.

  4. Very good written information. It will be useful to everyone who employess it, as well as myself. Keep up the good work – can’r wait to read more posts.

  5. I simply wished to thank you so much all over again. I’m not certain the things that I might have worked on without the actual aspects shown by you on such area. It seemed to be a real fearsome issue for me personally, however , spending time with the expert manner you resolved it forced me to weep for delight. I’m happier for your support and even trust you really know what an amazing job your are providing teaching men and women by way of your websites. Most likely you have never come across all of us.

  6. I¡¦ve read some excellent stuff here. Certainly worth bookmarking for revisiting. I wonder how a lot attempt you put to make one of these great informative site.

  7. I have not checked in here for a while as I thought it was getting boring, but the last several posts are good quality so I guess I will add you back to my daily bloglist. You deserve it my friend :)

  8. Thanks for each of your effort on this blog. My mother takes pleasure in carrying out investigations and it’s obvious why. A lot of people notice all of the compelling mode you create rewarding ideas through your web blog and therefore invigorate response from website visitors on the topic and our girl is truly understanding a lot. Enjoy the rest of the new year. You are doing a splendid job.

  9. I’m also writing to make you know what a extraordinary experience our child went through going through your blog. She noticed a good number of pieces, with the inclusion of what it’s like to possess an excellent coaching style to get the others without hassle learn about chosen multifaceted subject areas. You actually exceeded people’s desires. I appreciate you for giving the good, healthy, explanatory and even fun tips about this topic to Evelyn.

  10. My spouse and i felt quite lucky Emmanuel could complete his basic research through the precious recommendations he had through the web pages. It is now and again perplexing just to always be handing out tips many people could have been trying to sell. And now we take into account we have got the blog owner to thank because of that. Most of the illustrations you’ve made, the straightforward blog menu, the relationships you will give support to instill – it’s many astonishing, and it is letting our son in addition to the family recognize that this concept is fun, which is certainly extremely vital. Many thanks for the whole lot!

  11. Hiya, I am really glad I have found this information. Nowadays bloggers publish just about gossips and internet and this is actually annoying. A good website with interesting content, that is what I need. Thank you for keeping this web-site, I will be visiting it. Do you do newsletters? Can not find it.

  12. I would like to thnkx for the efforts you’ve put in writing this site. I’m hoping the same high-grade site post from you in the upcoming also. In fact your creative writing abilities has encouraged me to get my own blog now. Really the blogging is spreading its wings rapidly. Your write up is a good example of it.

  13. you’re really a excellent webmaster. The web site loading pace is amazing. It sort of feels that you are doing any unique trick. Also, The contents are masterpiece. you have done a great job on this topic!

  14. you’re actually a good webmaster. The site loading pace is amazing. It sort of feels that you are doing any distinctive trick. In addition, The contents are masterpiece. you’ve performed a great activity in this topic!

  15. I have been absent for a while, but now I remember why I used to love this web site. Thank you, I¡¦ll try and check back more frequently. How frequently you update your web site?

  16. I would like to thank you for the efforts you’ve put in writing this web site. I am hoping the same high-grade web site post from you in the upcoming as well. Actually your creative writing skills has inspired me to get my own web site now. Really the blogging is spreading its wings fast. Your write up is a good example of it.

  17. I have not checked in here for some time since I thought it was getting boring, but the last several posts are great quality so I guess I¡¦ll add you back to my daily bloglist. You deserve it my friend :)

Leave a Comment.