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).
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.
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.
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.
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.
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.
I'll post later details on the implementation.
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.
I'm following your instructions, so I'm not sure what the difference is here… any suggestions?
this is a really great tutorial for installing opencv on Debian Squeeze but a very poor tutorial for installing it on Arch Linux.
before I was able to install opencv. I was getting many error 404 messages.
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.
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.
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!
using python2 worked! i had tried python before and also tried to run it from geany; neither worked… thanks a lot!
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.
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?
I just noticed that I missed out the last step (installing libgtk2.0-dev) so I'll try again after doing that.
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.
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.
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.
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.
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.
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.
Those are great news Feej!
I get these errors after running 'make' does anyone knows what's going wrong?
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?
Thank you very much for the good instruction.
i am using debian image of raspberry pi..
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.
Mmmm, if you will not use python you can leave that out. Which version of python do you have installed?
So, I guess you are not planning to use python with Opencv. Drop the -D BUILD_PYTHON_SUPPORT=ON option.