Sunday, November 05, 2006

Obstacle detector using webcam and laser pointer



Hi everyone! I’m back with another webcam project, as usual. :) I’ve been having a lot of fun making cool things using my webcam over the last couple of months. This time, I’ve created an obstacle detector which uses a cheap laser pointer and a webcam to determine how far an object is from the camera. I worked with my cousins, Anuj Karpatne and Prateek Raj on this project.

There are all sorts of range finding devices available in the market like ultrasonic and infrared range finders. All these devices work well, but where portability and cost are of primary concern, this technique could be more desirable.

So, here’s how it works. A laser pointer is fixed below the camera at a known distance (as you can see in the picture above). The pointer is kept parallel to the optical axis of the camera. Then, a laser-beam is projected on an obstacle which is supposedly in the camera’s field of vision. The scene, along with laser dot is captured by the camera. Then, the image is scanned for the brightest pixels (we are assuming that the laser dot is the brightest point in the scene). Then, we calculate how far along the y-axis the laser dot is from the center of the image. The farther from the center, the closer we are to the obstacle. So, the distance from the object (D) is inversely proportional to the number of pixels from the center of the image (y):

D = hY/2y * cot (A/2)
Where the constants used are:
h = the actual distance between the laser pointer and the optical axis of the camera.
Y = total number of pixels along the y-axis
A = angle of view of the camera

As these values are constants, we obtain,
D α 1/y
Or, D = k/y (where k is the proportionality constant for all the constants used above)

We used an experimental approach to determine the value of k. We wrote a simple program in C# to do the entire image processing work and for finding the position of the laser dot in the scene. We calibrated the apparatus by placing an obstacle at known distances from the center of the camera and noting down the number of pixels the laser dot is from the center of the image along the y-axis. After taking several readings, we obtained the value of k. By using this value, we were actually able to determine how far an obstacle is, from the camera.
Well, we had a lot of fun working on this project! I hope you find it interesting. I’d love to hear your comments and ideas. :)

59 comments:

Paul said...

Hey Ashish,

Question on the derivation of the distance equation. Was the idea behind that using tan(A/2) = opposite/adjacent and solving for inverse of adjacent (being the distance)? Where X/2 is equal to opposite?

Ashish Derhgawen said...

Hey Paul,

I made a silly mistake while writing the equation on my blog. I've edited my post..check it out..'X' and 'x' have been changed to 'Y' and 'y' because we were not measuring the number of pixels along the x-axis..we were measuring them along the y-axis.

Here's the derivation:

tan(A/2) = H/D (where H = half of the actual distance, along the y-axis, on the obstacle, in the camera's field of view.

Since the optical axis of the camera is parallel the laser beam, the distance between the laser dot and the optical axis of the camera on the obstacle is also h which can be obtained from the pixels counted on the y-axis as h = 2yH/Y. Solving these equations, we get the working formula.

Thanks for the message. If you still have questions...let me know. :)

Ashish

Matthew said...

Hi! I'm trying to make programs using a webcam right now, but I have no idea where to start. I'm using VB Express 2005, but I can switch and learn a new language if I need to.
What language do you use for your laser programs, and where can I find info on programming using webcams?

Ashish Derhgawen said...

Hey Mathew,

I entered the world of webcams after reading a Coding4Fun article by Scott Hanselman: http://msdn.microsoft.com/coding4fun/hardware/video/article.aspx?articleid=912546

Then, I moved on to motion detection: http://ashishrd.blogspot.com/2006/10/motion-detection.html
I really enjoyed creating the motion detector..It unlocked a new world of machine vision for me :)

I usually use C# 2005 Express..but you don't have to learn a new language at this point. You can do awesome webcam projects with VB 2005 Express! :)

Regards,
Ashish

Bernardo Kyotoku said...

Wow!! that was really a nice application done with so little!!! Really Fascinating

Alex said...

can you email me the program to johndear101@gmail.com

Avik said...

Hey aashish!
this is avik...
i really liked your project...
can you please send me the compiled project?
My email:
a_d777@yahoo.co.in
www.techjugaad.com

Anonymous said...

Hey aashish!
i'm andy...
i really liked your project...
can you please send me the compiled project?
My email:
kurniawan_andi@yahoo.com

I really want to try that:)
thx

Anonymous said...

Hello Ashish i have a project with an robot that must avoid obstacles. Could you sent your code to zlog2005@gmail.com to not reinventing the wheel. :) and also to test your code :P ?
Thank you ! And Keep going on because is nice what you do.

Vladimir said...

Hey Ashish!
i'm Vladimir....
i have a project with a robot that must avoid obstacles. can you send me your code to not reinvent the wheel and also test your code :P
My email:
zlog2005@gmail.com

I really want to try that:)

theboss said...

please send to me this project
because very very nice .

please send to >
\/

\/


majde_issa@yahoo.com

Or

alaa_mcp@yahoo.com

Or

majde_underground@yahoo.com

Vivan said...

Hey ashish,
Could u tel me how to disassemble a laser pointer to mount it on my custom built RC car?

Vivan

vivangkumar@gmail.com

Anonymous said...

i'm doing a project to detect obstacle using stereo images from 2 webcams. Processing stereo image to derive distance is tricky since image correspondance is a big problem. I was planning to use a Laser beam of type in the robot, so that the spot beam in the 2 images can be used to derive distance. U have any idea about Laser LEDs.. How much it will cost. wheather the differnt color led s can be distinguished as differnt color pixels..., I'm doing the project in VC++. Could u also send the source code of u'r project using webcam for obstacle detection??? Or can u suggest any links were related information will be available....

regards
***************
ranjithknmbr@yahoo.com
***********

Kristof said...

Hi ashish,

First of all this is amazing what you've accomplished! My upmost respect for your work!
Currently I'm working on a pan/tilt construction made from old HP-printer stepper motors.
I've made it with a wireless control.
So far so good, now I'm trying to boost it up a little bit.
I could really use your project you've made here for the distance measurement. Could you please email me the project?


Kind regards and keep up the good work!!

Kristof

davidsherwood said...

Wow. Pretty cool. I assume this is a wireless camera, and all the calculations are performed on a "base station" pc? I am working on a robot using a OV6620 color cmos camera sensor hooked up to a Philips LPC 2106 micro with 128k flash and 64k ram. I am communicating via wireless RS232 link at 115.2kbaud back to a PC, where I am trying to get some useful code running in C#. I've played with a few sensors, and have landed on the laser pointer/ov6620 (filtering out all but red, finding brightest spot). Can you e-mail me your code? I will share in return when I am finished. Thanks again, and very impressive work!

Ashish Derhgawen said...

@davidsherwood: Hi David,
Yes..all calculations are performed on a base statjion PC. How can I send you the code? Send me an email and I'll reply back with all the details.

Anonymous said...

hi Ashish,

Im Ashish Nair. I am doing a project with robots which requires image processing.wil u kindly sen me the program codes to ashishnair@hotmail.com
or
kutts1986@yahoo.com.
thanks

Anonymous said...

Hi Ashish,
amazing project. Is it possible, that you can send me your code,please?

Thanks a lot
bamba
newsletttersucks[-AT-]gmx.de

davidsherwood said...

Thanks Ashish. You can e-mail me the source code at davidsherwood@rogers.com

Anonymous said...

I wonder if placing the laser further from the camera would increase the long range accuracy.

EF Mechanic said...

Nice Project.
Please email me the compiled project.
My email is airman00@gmail.com

Thank You

kris said...

hey ashis do you have any minituare camera circuit pls send me ok ............i love your project very much ..........
ok bye.....and keep doing...............................................from nepal

Anonymous said...

If you are willing email source code. thanks
marc1234lv@aol.com

krishn music collections said...

hi guy, it is an excellent project, really really i appreciate ur efforts, me too in the hardware side, if u dont mine , would u pls send me the code
my email krishnkrishna@gmail.com

Dave said...

Hi Ashish
Very interesting blog. Could you send me as much or as little information on the obstacle detector as possible to davidablake@hotmail.com, BTW which software did you use to scan the picture for the brightest point and to find where this pixel is. Cheers Dave

saranya said...

hi ashish,
tats really great yar!!!!!! such a innovative idea,never&no chance.can i know wat is ur next project.

saranya said...

hi ashish,
tats really great yar!!!!!! such a innovative idea,never&no chance.can i know wat is ur next project.

Anonymous said...

hi ashish,
Tats really great yar.UR PROJECT IS WONDERFUL.can i know wat is ur next project.


cheers,
saran

shreesh said...

hey i am able to control LEDs via parallel port now,.. i started this all after seeing your blog.. now i ve setup webcam, i know just C.. can i do this with C ? reply soon..

omer said...

Hi again Ashish-thanks for sending me your codes...This project is extremely marvellous; can u mail me it's source code...
Thanks alot
You can reach me at m_omer_iqbal@yahoo.co.uk or momeriqbal2@gmail.com

José Jorge (Geo) said...

I was just working on the same project and found your site, however, I just can't figure out your equation.

At the moment I'm working with two approaches (I'll try to use your notation):
D = h / tan( y * k ), where k is a constant derived from experimentation, it's a relation between pixels from the center of focus and the angles for every known distance.

The second is D = r / y, where r is a relation between pixels from the center and the distance, it's calculated through experimentation/calibration. This one is just the same as yours D = k/y.

But, I tried your first version, and I can't make it work since it gives wrong results, also, I can't figure out how you got the first one and how you got to the last from there.

Could you please give a little more detail on your math process? Thanks a lot, you have a great site with lots of useful/interesting information :).

Anonymous said...

Hi Ashish

Great Project can you please also email any coded or help information in regards to this Project tony811@hotmail.com

Cheers

jason said...

hi Ashish ,
can i have ur email add?
i am interested in this project u did
anyway my email add

jasonlimwk@gmail.com

thanks

Anonymous said...

hi...m working on a project how to detect a object using webcam...m frm mech bckgrnd..can u help m to proceed further..!!
my id is harmansinghgrover@hotmail.com

Charles said...

Hi Ashish!

Would you be able to send me the source code for your Obstacle Detector to me at: charleslo77 at gmail.com

Hester said...

hei mr paul,

im hester.and i am orking on a project that is so related with your new discovery so i hope you can really help me.i am thinking of a obstacle detector for blind people.so i thought of 'using' your idea then adding some alarm/sound whenever the detector is x-units away from an obstacle so that the blind person will be able to be alarmed about the said obstacle.

Hester said...

o yeah, can you email me. i am really looking forward for your reply.umayam_hestermana@yahoo.com

saurav said...

hi asish
this is saurav
can u please send me the compiled project
my id is:- f22raptor_commache@yahoo.com

Anonymous said...

hi ashish,
You can e-mail me the source code
Thanks Ashish.
my e-mail
mina.alfy@yahoo.com
eng_mina.alfy@hotmail.com

Nitin said...

Hi Ashish
Its a nice project can u mail me the program on nitin_1700@yahoo.com
and i would like to know which port you used to connect the remote to the PC. It would be really nice if u can explain the whole method of connecting the remote to Pc..
Thank you.

Anonymous said...

Hi Ashish,

Great work, could you email me your code? noormuhyi@gmail.com

Thank you

Anonymous said...

hey ashish derhgawen,

i'm studing about this your suject. Can you please send me the program to mail:bocautrang2583@yahoo.com.
Thanks!

jai said...

hi ,sir
i am working on robotics.i want to make a robot base fire extinguisher to put off a candle.for this i need a web cam for image processing.please help me by sending the program codes as i don't know how to program a web cam

vasanth said...

hey .. can u help me by answering a few ques... how can u find which pixel is having max intensity ???

mayurisama said...

hello ashish,

Nice project. I'm really interested in integrating your project with my group's project proposal. I'm actually planning to propose a security system that uses webcams and stuff that needs to track some moving objects. Can I have a copy of your program? Please send it to mayurisama@yahoo.com.ph, I'd be happy to add you on the credits page.

thanks

anand said...

hey ashish can u please give me the code for ur project. i really need it. my mail id is anandv_20022yahoo.com

anand said...

hey ashish can u please give me the code for ur project. i really need it. my mail id is anandv_2002@yahoo.com

mayoush said...

i want the stages of this project because i want to practice this one...and i want the detailed programm for this project...and plz facilitate the distances(physic)

Martin said...

Hello Ashis!!
I loking for something about lasertracking and I found your project: "Obstacle detector using webcam and laser pointer". It´s very interesting and I would like to try this project. Because I am a biginner in C# and programming at all. I ask for help me, please send me this project.
very very thanks Martin

Prabhasa K L said...

hey ashish,
well hope u can send me ur compiled project including the code...because i am not a progrmmer..so i need the code....and very well done on all ur projects.....hope to c a reply asap..this is ma id prabhasakl@yahoo.com
thanx and regards.

jroyal said...

hey ashish,
can u plz send my a copy of the source code or giv me a link to somewhere i can get it. my email is: joshroyal@hotmail.com

thx so much

Albert Cheng said...

Hi ashish, can you send me this program to my email?

My email is cheng_alb@hotmail.com

I find that this is a very simple program that runs a very complex form. It is very admirable.

Please help me out...I am doing a project for my school and I need some help. Thank you ^^

Regards
Albert

Chris said...

Hi Ashish, Can you send me the code to this project as i am currently bulding a robot of my own and range finding would be perfect. thanks
Chris
Chris@Malyon.co.uk

SAKSHI said...

HI ASHISH , I'M SAKSHI ,I AM MAKING A PROJECT " FIRE EXTINGUISHER USING IMAGE PROCESSING".
CAN YOU HELP ME WITH ITS PROGRAMMING AND ITS CIRCUIT DAIGRAM.
Since I have to submit project report on this project by 4th of Sept. 2009 so will it be possible for u to send me the details as soon as possible..
SAKSHIDHINGRA15@GMAIL.COM

anwar said...

hello ashish ,

can i have the code for the same please i am trying to do the same excercise but with more then 1 laser .

Anwar said...

Hello Ashish, can you please send me the code for Obstacle detector using webcam and laser pointer my email id is vanekar01@hotmail.com.

Atomic Chimp said...

Hi Ashish,

Could you also send the source code to me.

thanks!

Michael Girard
atomicchimp@hotmail.com

Riaan said...

please send me the source for this awesome app to riaan.deyzel@gmail.com

Mohammed said...

Hey Ashish!

this project is so fantastic , could you send me the code @
ms_soft89@hotmail.com


thanks