Saturday, November 04, 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. :)


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. :)


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:

Then, I moved on to motion detection:
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! :)


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

Avik said...

Hey aashish!
this is avik...
i really liked your project...
can you please send me the compiled project?
My email:

Anonymous said...

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

I really want to try that:)

Anonymous said...

Hello Ashish i have a project with an robot that must avoid obstacles. Could you sent your code to 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:

I really want to try that:)

theboss said...

please send to me this project
because very very nice .

please send to >




Vivan said...

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


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....


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!!


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

Anonymous said...

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

Thanks a lot

davidsherwood said...

Thanks Ashish. You can e-mail me the source code at

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

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

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

Dave said...

Hi Ashish
Very interesting blog. Could you send me as much or as little information on the obstacle detector as possible to, 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.


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 or

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


jason said...

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


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

Charles said...

Hi Ashish!

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

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 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

saurav said...

hi asish
this is saurav
can u please send me the compiled project
my id is:-

Anonymous said...

hi ashish,
You can e-mail me the source code
Thanks Ashish.
my e-mail

Nitin said...

Hi Ashish
Its a nice project can u mail me the program on
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?

Thank you

Anonymous said...

hey ashish derhgawen,

i'm studing about this your suject. Can you please send me the program to

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, I'd be happy to add you on the credits page.


anand said...

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

anand said...

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

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 i need the code....and very well done on all ur projects.....hope to c a reply asap..this is ma id
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:

thx so much

Albert Cheng said...

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

My email is

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 ^^


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

SAKSHI said...

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..

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

Atomic Chimp said...

Hi Ashish,

Could you also send the source code to me.


Michael Girard

Riaan said...

please send me the source for this awesome app to

Mohammed said...

Hey Ashish!

this project is so fantastic , could you send me the code @


Anonymous said...

Thats what im just searching for my similar project, can u please send me the files.

Anonymous said...

You need to correct for the lens. In particular with such a simple thing as a webcam, which has a crap lens, after all. This correction will then eliminate a significant part of the calibration hassles, and extend the useful spacing between.

It's a good start, though.

Next to do the same thing for a line laser source, so you can get several points per frame. Pulsing the laser while the shutter is open will also give a shorter effective shutter time, and you can simply increase the pulse duration to increase the light accumulated in a frame when the distances are very large. Using an interference filter will also give significant improvements, and the bulk of CCD chips without built in NIR filtering are more sensitive to 900nm or so, outside the visible range (but still an eye hazard at high powers, so be sure to check the power you use).

The addition of a second camera is useful in seeing the beam when it's been obscured by an object, and gives visual information in stereoscopy.

Finally, if you project a grid and not a line, with a displacement that is diagonal, then you will be able to collect near field spatial information with greater resolution (the far field will be obscured as the lines become too small to distinguish from each other, unless you stick the lens on a voice coil to "scope" for the far field).

Just some thoughts.

Anonymous said...

Hey Ashish!

I like this fantastic project, nice work Ashish.
Could you send me the code @

Anonymous said...

hi ,
This is really nice ......
and i need to use this project in my final year project so can u send it for me please .........
my email id is
thank you.

eatabean said...

Great idea Asish. What is the resulution of this typ of measuring device? Can I measure parts of a millimeter from close distance? I measure the application of silicone gaskets, and need a no-contact method which will not damage the gasket before it has hardened... this may be adaptable!

Thank you for sharing.
Jim in Sweden

Anonymous said...

Hi Ashish,

i'm doing the same project and i just completed the project(contolling CAR using Keyboard).

Now, i lik to use wireless Cam and IR for my project.

But i dnt kno , how to send IR signals frm car to Port...

and also i lik to use sum sensors lik temp,Fire..

if it is possible..hw i can send the datas(temp) to the port...

am using for the development....

Hope .. u will help me....

thanks in advance

Anonymous said...

hey ashish,can u provide me the code for obstacle detection
my email

Anonymous said...

Hi Ashish,
I'm doing a similar kind of project. I'm stuck with a problem, can you help me please?Can you detail how the detection of spot is made possible in your software?Did you process on RGB image? Is it by setting a threshold for any one of intensity/saturation/hue or for all the three?

Anybody doing laser spot processing,
please give me an answer.
Pls answer to

Riaan Deyzel said...

Please send me the source for the Laser range finder project, You have previously told me to modify another projects code to be able to do this, but I am a begginer at Coding, & only wish I had your knowlege.
Please help me out with this source code. thank you.

itesh said...

hey ashish ...pls send me obstacle detector using webcam and laser pointer in vb8...pls yar madad kar de.....muche BE ka project submit karna hai.......pls yar....

akshay said...

hey ashish
can u send me detail abt this project...
on my id

Alex Lee said...

I have read your documentation, could you sent me the pure source code to examine it???My mail address ,s

It is quite emergency :( I have to examine your project and modify it according to my project... Please touch in connect as soon as possible...


Anonymous said...

Hey Ashish,

This is way cool! could you please send me the app? I would really appreciate it.



Anonymous said...

hey ashish,

i am planning to work on something similar can you please send me the code and the design details
my id is

thank you

leni said...

hiiiiii ashish can u please send me the full info about designing the obstacle detector,hope to see from u soon

Anonymous said...

Hi Ashish,
Can u plz send me the project code on

Blasphemous Aesthete said...

hey Ashish, could you help me with the calibration part.
I don't quite get the working theory behind this.
In physical sense it uses the law of similar triangles. The formula is pretty straightforward. But what then.
How is the relation between the pixel distance from center of image and actual distance related. How is the central pixel pointing to the farthest distance.

I am searching for detailed explanation for this for quite sometime now and have also mailed the authors of related papers like Todd Danko. But they don't seem to take much interest in answering me.

Do reply me at

hitesh ahuja said...

hello ashish,
really it seems now that i was searching for you only..
actually i want to access my webcam
remotely .like many websites ask us to open ur webcam and when we click tha button ,immediately webcam get on and the one sitting anywhere can peep into my room..
i m having a basic knowledge of c#,now plz tell me from where should i start.

hitesh ahuja said...

hello ashish,
really it seems now that i was searching for you only..
actually i want to access my webcam
remotely .like many websites ask us to open ur webcam and when we click tha button ,immediately webcam get on and the one sitting anywhere can peep into my room..
i m having a basic knowledge of c#,now plz tell me from where should i start.

Anonymous said...

Hey Ashish,

I'm Andrew from Chile. I'm a student of mathematical engineer and i'm working on a project to obtain points of a surface, and i have to implement sth to read distance. So, could you give the program please!?!?!?
I'll realy thank you


Anonymous said...

hey ashish could you pls send me the code to,
i'd really like to try it out for my robot

J said...

Hey, Ashish

actually im on a project on creating a laser pointed gun turret using image processing,
the thing is i'm interested on your laser code,
i'm having a trouble on processing the laser-pointer dot,

my questions are
How you process the image?
Did u you an openCv?
can you share me your code?

any help will be very helpful
thanks in advance
my address is;

Anonymous said...

why the fcuk evry bdy asking for code?... cant see its clearly written logic.!!!!

Andres said...


could you give it to me????

dalton said...

hey its really a wonderfull project...m really interested in learning hw it works.......can u plz send me the source code....
thanks in advance...

santosh said...

Hey Ashish

wonderful job
can u plz mail me the design and program at

Thanks in advance!!!

adeem said...

hi ashish
i liked your concept but i am looking for a different approach wherein there is an obstacle detection mechanism by the laser through a buzzer sound.Do you have any knowledge on this?..kindly give me the details if you do.thankyou.
email id-

Anonymous said...

Hey Ashish,

hey its really a wonderfull project...n really interested in learning hw it works for use this concept for final year project.......can u plz send me the source code n details about this project.... at email -->
hope u cam help me... tq:)

Anonymous said...

Hi Ashish,
This is a very useful stuff... Could you please email me how did you make this work? I'm doing a robot project which uses webcam to capture video to connect wireless to my laptop.. I'm pretty confuse what should I do... This might be helpful for me. Please email me when you see this.. Thanks alot !

Anonymous said...


Khairul said...

Hi Ashish,
This is nice. can i have the source code. send to my email

Anonymous said...

Hi Ashish,
We are working on a robot which climbs starirs. So we plan to use a similar method for staircase detection. Can you please mail this code on Thanx a lot.. :)

nanni said...

can you please mail me the code

Anonymous said...

really nice work can u plz send me the source code..(
and which languages are preferred to do same.

Anonymous said...

Nice project can you e-mail me details

thank you so much

Anonymous said...

Hi Ashish,

Good work on your project. Can you send me the source coed, as i'm trying to do it. Thanks.

Email :

Shiahb Shihab said...

Good work on your project. Can you send me the source coed, as i'm trying to do it.
if you need?

Shiahb Shihab said...

I am sorry
this my Email

Amalia Puspita said...

Hey Ashish!

I like this project, nice work Ashish.
Could you send me the code please..

Anonymous said...

Hey Ashish ,i lykd ur project a lot ,can u please give me an idea on how to make it a real time application n can u please e-mail me this project details on

Ruben Alvarez said...

can i have the code that you used on this proyect? i'd love try it out, i love your post btw.

Kumod Singh said...

hey ashish... i really liked ur project... could u please send me the code and other details at my

Unknown said...

could you please the SW to

Houssem Khouja said...

Great job there, I am really impressed by the work you're doing.
I am starting a project with similar problematic. The problem is that I am not sure of the way I can detect the brightest point detected by the camera. Can you help with that?

Here is my email:

Muhammad Qasim said...

i like your project idea.Please send me Project code at

jorge Ricardez said...

I really like your project can you send me the project pls

nano said...

hi Ashish,,
could u please send me the coding to my e mail

Anonymous said...

HI Ashish,

This is really a great project. I am wondering if I can get the code from you and I will try to modify it to work on something that I could put into my raspberry attached to a robot and see how will it do.

Thanks in advance

alvaro fabian altovez chacon said...

Hello I would like to know if you could collaborate with your project code this is my email :

رضا رمضان said...

شركة كشف تسربات المياه بالدمام
شركة كشف تسربات بالدمام
شركة كشف تسربات المياه بالخبر
شركة كشف تسربات المياه بالجبيل
شركة كشف تسربات المياه بالاحساء
شركة كشف تسربات المياه بالقطيف
شركة كشف تسربات بالرياض
شركة كشف تسربات المياه بالرياض
كشف تسربات المياه

Masa Jaddah said...

شركات الماسة تقدم لكم شركة جلى رخام جدة
فنحن نساعدك على عمل جلى رخام جدة
حيث أن شركتنا شركة جلى بلاط جدة
لديها أفضل ماكينات جلى بلاط جدة
كما اننا فى شركة شركة جلى سيراميك جدة
لدينا احدث أدوات جلى سيراميك جدة
كما ان شركتنا تعتبر افضل شركة جلى رخام جدة وبشهادة العملاء نعتبر افضل شركة جلى بلاط جدة ونعتبر أيضا افضل شركة جلى سيراميك جدة كما أن لدينا افضل اسعار جلي بلاط جدة

princess Totta said...

عطيه السيد said...

شركة الضحى
شركة تنظيف خزانات بالرياض
شركة تنظيف شقق بالرياض
شركة تنظيف مجالس بالرياض
شركة تنظيف سجاد بالرياض
شركة تنظيف كنب بالرياض
شركة تنظيف موكيت بالرياض
شركة رش مبيدات بالرياض