May 22, 2012

Installing OpenCV on the Raspberry Pi

UPDATE 2:Here is my guide for Arch Linux

UPDATE: I've moved to Archlinux because it has the webcam modules included. OpenCV is really easy to install: pacman -S opencv

Installing OpenCV (2.3.1) on the Raspberry Pi is pretty easy using the base Debian Squeeze image and following these instructions for Debian/Ubuntu.You will need to have more space on the root partition so I recommend resizing it to at least 3GB (see the video tutorial using GParted or follow these instructions). Here they are with some comments on the few issues I found:

I found an error trying to install cmake for the first time, so first do

sudo apt-get update

two times before anything else. The first time I did the update an error on "duplicate sources" was shown thus the second update was necessary.

Install in the following order:

sudo apt-get install build-essential
sudo apt-get install cmake 
sudo apt-get install pkg-config 
sudo apt-get install libpng12-0 libpng12-dev libpng++-dev libpng3 
sudo apt-get install libpnglite-dev libpngwriter0-dev libpngwriter0c2 
sudo apt-get install zlib1g-dbg zlib1g zlib1g-dev 
sudo apt-get install pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools 
sudo apt-get install libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-progs 
sudo apt-get install ffmpeg libavcodec-dev libavcodec52 libavformat52 libavformat-dev 
sudo apt-get install libgstreamer0.10-0-dbg libgstreamer0.10-0  libgstreamer0.10-dev 
sudo apt-get install libxine1-ffmpeg  libxine-dev libxine1-bin 
sudo apt-get install libunicap2 libunicap2-dev 
sudo apt-get install libdc1394-22-dev libdc1394-22 libdc1394-utils 
sudo apt-get install swig 
sudo apt-get install libv4l-0 libv4l-dev 
sudo apt-get install  python-numpy 
sudo apt-get install libpython2.6 python-dev python2.6-dev 
sudo apt-get install libgtk2.0-dev pkg-config

The package build-essential was already installed on my device so maybe you don't need to install it. Note that for the eighth line the original link says "libjpeg-prog" but it must be "libjpeg-progs". Also, check the last step that is not included on the original instructions but it's needed for this platform.

Get the sources (check the version you want to install):

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

Uncompress the sources and create a directory inside it (I called it 'release'). Chdir to it. Run cmake to configure, compile and install. Compilation will take a while, about a couple of hours:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON -D BUILD_EXAMPLES=ON ..
make
sudo make install 

Now you can run the samples. In this image, the Python version of contours:

Comments

raspberry_fan said over 2 years ago
Eduardo, I live in US but I cannot find raspberry pi to purchase? I am interested in having opencv application on that too? I am desperately looking to buy one, how did you get yours? if you give us some pointers that would be helpful. Thanks.
eduardofv said over 2 years ago
raspberry_fan, I ordered mine during the launch (at midnight in Mexico) and took this long to arrive. I've seen some for sale on eBay. Element14 and RS are promising to have many more for July.
area said over 2 years ago
While trying to get opencv on my Pi, I get cc1plus: out of memory allocating 7263404 bytes after a total of 37511168 bytes I'm following your instructions, so I'm not sure what the difference is here... any suggestions?
Cathal Coffey said over 2 years ago
Hi, this is a really great tutorial for installing opencv on Debian Squeeze but a very poor tutorial for installing it on Arch Linux. I am completely new to Arch Linux and pacman. It took me awhile to figure out that I had to run packman -Syu before I was able to install opencv. I was getting many error 404 messages. It also took me awhile to locate the OpenCV samples folder. I eventually found it in /usr/share/opencv/samples I am now stuck once again. How do I get Python binding for opencv in Arch Linux? Can you please update this Tutorial and give as much care/love to Arch Linux as you did to Debian? After all, I imagine that most people who install OpenCV want to do something with a camera so a tutorial for Arch Linux makes more sense than a tutorial for Debian Squeeze. Kind Regards, Cathal
Fanjita said over 2 years ago
I've added a tutorial for getting openCV running on Arch Linux on my blog : http://www.fanjita.org/serendipity/archives/56-Running-OpenCV-headlessly-in-Arch-Linux.html There's also a post about getting webcams up and running in Arch Linux. Maybe that will be useful to you Cathal.
eduardofv said over 2 years ago
Great tutorial Fanjita. Cathat, the truth is that I'm pretty new to Archlinux so I didn't took care on what I did to get the webcam, OpenCV and Arduino working all together on my Raspberry Pi, so I can't write a simple guide. I even had to compile and update the kernel to get the USB modules for the Arduino working. Anyway, I'm sure you can find Fanjita's tutorial useful.
mbrik said over 2 years ago
First of all, thanks for the tutorials eduardo and Fanjita... However, I would like to second the request for a tutorial on ArchLinux. I followed Fanjita's instructions: pacman -S python2 pacman -S python2-numpy pacman -S opencv pacman -S opencv-samples However, when I try to run the sample app in python using 'import cv2.cv as cv', i get, 'No module named cv2.cv'. Is there something additional required after grabbing the packages to make python see the cv modules?
eduardofv said over 2 years ago
mbrik, have you tried using python2 when calling your script? I'm not sure if this can solve your problem but it seems that by default "python" is python 3.0 and to call with 2.7 you must use "python2". I'll try to do a fresh install of Archlinux on a new SD card and record each step and post a new tutorial. I just have to find the time!
eduardofv said over 2 years ago
Here is my guide for Arch Linux, hope it helps. http://eduardofv.com/read_post/192-OpencCV-on-the-Raspberry-Pi-with-Arch-Linux-ARM
mbrik said over 2 years ago
using python2 worked! i had tried python before and also tried to run it from geany; neither worked... thanks a lot!
Cathal Coffey said over 2 years ago
Hey Guys, thanks for updating your tutorials and providing lots of help for Arch Linux. I now have a new problem. I have managed to get python2 and opencv installed. I have also managed to install cheese (the webcam software) and xawtv. One of my two webcams works with both cheese and xawtv but neither of my webcams will work with OpenCV. OpenCV throws the following exception libv4l1: error allocation v4l1 buffer: Cannot allocate memory I figured that once a webcam worked in cheese and xawtv then it was going to work with OpenCV... but this is obviously not the case. Any ideas how I can get the webcam working with OpenCV in Arch Linux?
Eduardofv said over 2 years ago
Cathal, are you using the 224MB of RAM? It sounds to me that you are running out of memory. Copy the corresponding boot.elf to enable that amount of memory
Feej said over 2 years ago
Upon attempting to compile on the Pi, I get the following error: [ 37%] Building CXX object modules/core/CMakeFiles/opencv_test_core.dir/test/test_operations.o c++: Internal error: Killed (program cc1plus) Please submit a full bug report. See <file:///usr/share/doc/gcc-4.4/README.Bugs> for instructions. make[2]: *** [modules/core/CMakeFiles/opencv_test_core.dir/test/test_operations.o] Error 1 make[1]: *** [modules/core/CMakeFiles/opencv_test_core.dir/all] Error 2 make: *** [all] Error 2 Any ideas? I just noticed that I missed out the last step (installing libgtk2.0-dev) so I'll try again after doing that. Thanks!
eduardofv said over 2 years ago
Feej, I have no idea what may be wrong, but it might be that the missing dependency is causing it. Keep us informed.
Cathal Coffey said over 2 years ago
Eduardofv you were correct I had to replace start.elf with one that allowed more memory usage. Now both of my webcam's work quite well with cheese and xawtv 20fps. However one gives 1 fps with OpenCV and the other 5 fps. Neither of these are fast enough for what I want to do... I thought I needed to go and buy a super simple/cheap webcam. Someone here http://forum.openrobotino.org/showthread.php?359-Using-OpenCV-for-camera-capture recommended a Logitech Webcam Pro for Notebooks because OpenCV can grab raw images directly from this camera so the decompression step is skipped which saves lots of CPU time... but I already have an older version of this camera... its the one which gives 1 fps with OpenCV.
Feej said over 2 years ago
Just a quick update: the missing dependency did NOT fix this problem. I think I'm still using the standard amount of RAM with my boot.elf (though I used a new one when compiling UVC). I'll check this, and see if I can increase it. I'll attempt to recompile after that. Cheers.
eduardofv said over 2 years ago
Cathal, great news! I'm glad you've got it working. I'm also getting that low FPS rate, but it might be enough for my application, at least for now. I have big hopes on the CSI camera the Raspberry Pi Foundation is working on. Probably you already know this but anyway here are the links to the first pictures http://www.raspberrypi.org/archives/1254 and to the topic on the forum: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=43&t=6120 Feej, I think that the first time I installed on Debian everything worked with the standard RAM. Anyway, it's worth giving a try. Good luck.
Cathal Coffey said over 2 years ago
eduardofv, I didn't realize you were getting such a low frame rate. I imagined everyone else was getting 20 fps :-) I really need 20 fps for some of the CV that I want to do. Can't wait for the camera module to be sold... thanks for posting the links and for all your help to date. Kind Regards, Cathal
eduardofv said over 2 years ago
You're welcome, Cathal. I know it may be too much work, but maybe migrating from python to c++ may help, although I'm not sure about it. Hope you can make your project work!
Feej said over 2 years ago
Weirdly enough, increasing the RAM seems to have fixed things. It has now compiled and installed, hooray! I believe I was using non-standard boot files as I compiled a new kernel, etc., so perhaps that's what caused issues. I grabbed the latest ones from https://github.com/raspberrypi/firmware/tree/master/boot and selected the arm224.elf. Haven't actually got it working yet though :0D
eduardofv said over 2 years ago
Those are great news Feej!
Patrick said over 2 years ago
I get these errors after running 'make' does anyone knows what's going wrong? [ 8%] Built target pch_Generate_opencv_core [ 8%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/system.cpp.o In file included from /root/OpenCV-2.4.1/modules/core/src/system.cpp:460: /root/OpenCV-2.4.1/release/modules/core/version_string.inc:36:1: warning: missing terminating " character In file included from /root/OpenCV-2.4.1/modules/core/src/system.cpp:460: /root/OpenCV-2.4.1/release/modules/core/version_string.inc:36: error: missing terminating " character /root/OpenCV-2.4.1/release/modules/core/version_string.inc:37:11: error: too many decimal points in number /root/OpenCV-2.4.1/release/modules/core/version_string.inc:37:29: error: invalid suffix "st" on integer constant /root/OpenCV-2.4.1/release/modules/core/version_string.inc:41:29: warning: character constant too long for its type /root/OpenCV-2.4.1/release/modules/core/version_string.inc:58: error: stray ‘@’ in program /root/OpenCV-2.4.1/release/modules/core/version_string.inc:58: error: stray ‘@’ in program /root/OpenCV-2.4.1/release/modules/core/version_string.inc:69:10: error: #include expects "FILENAME" or <FILENAME> /root/OpenCV-2.4.1/release/modules/core/version_string.inc:72: error: stray ‘\’ in program /root/OpenCV-2.4.1/release/modules/core/version_string.inc:72:9: warning: missing terminating " character /root/OpenCV-2.4.1/release/modules/core/version_string.inc:72: error: missing terminating " character /root/OpenCV-2.4.1/release/modules/core/version_string.inc:75:23: warning: missing terminating " character /root/OpenCV-2.4.1/release/modules/core/version_string.inc:1516: error: stray ‘\’ in program /root/OpenCV-2.4.1/release/modules/core/version_string.inc:1516:4: warning: missing terminating " character /root/OpenCV-2.4.1/release/modules/core/version_string.inc:1516: error: missing terminating " character /root/OpenCV-2.4.1/release/modules/core/version_string.inc: In function ‘const std::string& cv::getBuildInformation()’: /root/OpenCV-2.4.1/release/modules/core/version_string.inc:37: error: expected ‘,’ or ‘;’ before ‘version’ /root/OpenCV-2.4.1/release/modules/core/version_string.inc:139: error: ‘z_stream’ was not declared in this scope /root/OpenCV-2.4.1/release/modules/core/version_string.inc:141: error: expected ‘;’ before ‘typedef’ /root/OpenCV-2.4.1/release/modules/core/version_string.inc:162: error: ‘gz_header’ was not declared in this scope /root/OpenCV-2.4.1/release/modules/core/version_string.inc:164: error: expected ‘;’ before ‘typedef’ /root/OpenCV-2.4.1/release/modules/core/version_string.inc:1506: error: ‘ZEXTERN’ was not declared in this scope /root/OpenCV-2.4.1/release/modules/core/version_string.inc:1506: error: expected ‘;’ before ‘const’ /root/OpenCV-2.4.1/release/modules/core/version_string.inc:1512: warning: no return statement in function returning non-void /root/OpenCV-2.4.1/release/modules/core/version_string.inc: At global scope: /root/OpenCV-2.4.1/release/modules/core/version_string.inc:1516: error: expected unqualified-id before ‘)’ token /root/OpenCV-2.4.1/modules/core/src/system.cpp:462: error: expected unqualified-id before ‘return’ /root/OpenCV-2.4.1/modules/core/src/system.cpp:465: error: ‘string’ does not name a type /root/OpenCV-2.4.1/modules/core/src/system.cpp:474: error: ‘string’ does not name a type /root/OpenCV-2.4.1/modules/core/src/system.cpp:503: error: ISO C++ forbids declaration of ‘Exception’ with no type /root/OpenCV-2.4.1/modules/core/src/system.cpp:503: error: expected ‘,’ or ‘...’ before ‘&’ token /root/OpenCV-2.4.1/modules/core/src/system.cpp: In function ‘void error(int)’: /root/OpenCV-2.4.1/modules/core/src/system.cpp:506: error: ‘exc’ was not declared in this scope /root/OpenCV-2.4.1/modules/core/src/system.cpp:510: error: ‘exc’ was not declared in this scope /root/OpenCV-2.4.1/modules/core/src/system.cpp:526: error: ‘exc’ was not declared in this scope /root/OpenCV-2.4.1/modules/core/src/system.cpp: At global scope: /root/OpenCV-2.4.1/modules/core/src/system.cpp:543: error: expected declaration before ‘}’ token make[2]: *** [modules/core/CMakeFiles/opencv_core.dir/src/system.cpp.o] Error 1 make[1]: *** [modules/core/CMakeFiles/opencv_core.dir/all] Error 2 make: *** [all] Error 2
eduardofv said over 2 years ago
Patrick, I don't know what may be happening, but I see that you are compiling the 2.4.1 version which is pretty new. I have only tested 2.3.1 on the Raspberry Pi and would suggest to try that version unless you need something specific to 2.4.1
james said over 2 years ago
Thanks for the instruction but im getting caught up on some of these later steps since I am new to linux, could you explain them in noob terms for me? "Uncompress the sources and create a directory inside it (I called it 'release'). Chdir to it." How do I uncompress? I see what appears to be a "download" file in my main folder but I dont know what to do with it...
eduardofv said over 2 years ago
James, it would be something like: tar -jxvf path_to_file.bz2 (this will create a dir with the extracted files) cd created_dir mkdir release cd release Lastly run the make commands that are refered above
james said over 2 years ago
Cool, now I have the OpenCV-2.3.1 folder in my main folder, but when I try and run the next set of commands I get: CMake Error: The source directory "/home/pi" does not appear to contain CMakeLists.txt. What am I missing?
eduardofv said over 2 years ago
You must be missing the "cd release"
BT said over 2 years ago
Thank you very much for the good instruction.
eduardofv said over 2 years ago
You are welcome BT
jack said over 2 years ago
hi feej: i have done all steps as u said to increase my cpu ram..but after rebooting when i am again starting the same process by make command this error comes: cc1plus: out of memory allocating 7350596 bytes after a total of 69115904 bytes make[2]:......error1 make[1]:........error2 help me out......
jack said over 2 years ago
i am using debian image of raspberry pi..
Me said over 2 years ago
I am new to Linux and when it try to compile cake I get the error /home does not contain cmakelists.txt Specify .. Help blablabla Please help, Matt
eduardofv said over 2 years ago
Tu run the cmake command as shown above, you must be in the 'release' directory you have just created (you seem to be on your home root). Also check the trailing .. at the end of the command.
Me said over 2 years ago
That fixed it thank you very much, Matt
eduardofv said over 2 years ago
Matt, that's great!
Me said over 2 years ago
It finish compiling in about 2 min And says Manually specified variables werenot used by the project Build_python_support Cmake_biuld_type Cmake_Unstall_prefix I see typos in the last 2 but what about the first I had caps Lock on when I typed them into terminal can I just fix the typos and re compile or do i need to uninstall
eduardofv said over 2 years ago
Go fix and retry
Me said over 2 years ago
It still doesn't like build_python_support
eduardofv said over 2 years ago
Mmmm, if you will not use python you can leave that out. Which version of python do you have installed?
Me said over 2 years ago
Im not sure There is a program called idle/idle3 that says python shell when opened
eduardofv said over 2 years ago
So, I guess you are not planning to use python with Opencv. Drop the -D BUILD_PYTHON_SUPPORT=ON option

Add Comment

Name 
Your comment 
Enter the verification code
simple_captcha.jpg