I’ve been working on a project to build a Laser Range Finder using a Raspberry Pi, an Arduino and OpenCV using a webcam. I hope that eventually this project may be used on a mobile robot using the algorithms taught on Udacity CS373 specially SLAM (Synchronous Location and Mapping).
The First Prototype
This first protoype is more a proof of concept than a usable device. Anyway, it’s working pretty well except for it being quite slow.
- Raspberry Pi Model B running:
- Archlinux ARM with a modified kernel to support the Arduino and the Webcam
- OpenCV 2.4.1
- Python 2.7
- The LRF software
- Arduino UNO connected via USB to the Raspberry Pi. Runs a controller that receives a message to turn on and off the laser. I hope it will also control some servos later.
- A Logitech c270 webcam, disassembled, so it can be installed on the casing
- Sparkfun TTL Controlled Laser Module
- A targus mini USB hub
- My Powered USB cable to provide the extra current that the Raspberry Pi can’t provide to the USB devices
- A couple of USB power sources, one for the RPi and the other for the USB devices
- A lousy acrylic casing, the first thing I’ve done with acrylic
Also check the video for an overview on it’s parts and how it works.
This prototype is very slow (one measurement takes about 10 seconds) but I’m optimistic that it may become more functional on a couple of iterations, specially with the upcoming Raspberry Pi Foundation CSI camera. The device is pretty accurate and precise on short distances but, as expected, both decrease at large distances. I would estimate that from a distance up to 35cms it’s very accurate, from 35 to about 60cms has pretty good and up to 2m it may be good enough for a small robot.Later I’ll post more details on the measured precision and accuracy and some tricks to enhance them.
As you can see on the video, it has a simple web interface to trigger the measurement process. It can also be done command line by SSHing to the Raspberry Pi. I’ll also post how OpenCV detects the laser in the image, and the next steps I’ll take to improve. For now you can get most of the working code from Github. The details of the mathematical model appears below.
All comments are welcome here (comments section at the bottom) or via Twitter.
The Model
This diagram shows the basic idea for the project. The laser is shot to a target at a known angle and the image is captured on the webcam. The angle at which the laser appears on the image corresponds to the incidence angle of the laser at the target, and thus, to the distance to the target.
If the target is a little farther, so that the laser crosses the focus line of the camera, the model is a bit different:
Here we are considering that both the camera-to-laser angle (β) and the distance from the camera to the laser (L) are fixed. We also know the focal distance (f) and the horizontal resolution (CAMERA_WIDTH) that are parameters of the camera. With OpenCV we can process the image and calculate the horizontal distance (vc) from the camera’s Y axis to the point where the laser appears on the image. Given those values we can use simple trigonometry to calculate the angle at which the laser appears on the image (δ) and the distance from the camera to the target (Dc). Note that we are looking Dc and not D which is the perpendicular distance from the camera to the target. By the way, for the purposes of this model the webcam is considered a pinhole camera. Later on we will correct the physical camera to adjust the model.
vx = CAMERA_WIDTH – vc
δ = atan( f / vx )
λ = π – β – δ
Dc = L * sin( β / λ )
I’ll post later details on the implementation.
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.
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.
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?
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
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.
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.
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?
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!
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
using python2 worked! i had tried python before and also tried to run it from geany; neither worked… thanks a lot!
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?
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
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 for instructions.
c++: Internal error: Killed (program cc1plus)
Please submit a full bug report.
See
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!
Feej, I have no idea what may be wrong, but it might be that the missing dependency is causing it. Keep us informed.
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.
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.
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.
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
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!
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
Those are great news Feej!
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
/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
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
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…
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
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?
You must be missing the “cd release”
Thank you very much for the good instruction.
You are welcome BT
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……
i am using debian image of raspberry pi..
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
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.
That fixed it
thank you very much,
Matt
Matt, that’s great!
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
Go fix and retry
It still doesn’t like build_python_support
Mmmm, if you will not use python you can leave that out. Which version of python do you have installed?
Im not sure
There is a program called idle/idle3
that says python shell when opened
So, I guess you are not planning to use python with Opencv. Drop the -D BUILD_PYTHON_SUPPORT=ON option.