Article

# Computer Vision: Laser Range Finder

, 7 Jul 2010
 Rate this:
Finding the distance of an object using a laser pen and an ordinary web-cam !

## Introduction

These days, there are many range finder devices which are used almost everywhere from robotics to country's defence system. All these devices work well, but there are portability and cost concerns for general purpose robotics applications. From my previous articles, I've been creating cool programs related to webcam and now I've come up with a laser-web cam range finder. It could tell how far an object is by just using a very cheap laser pen and an ordinary web cam.

## How It Works

First, we need to arrange the laser pen exactly below the web cam so that it's parallel to the optical axis of the camera's lens. Practically, it's not possible even a minor error would give a huge error at greater distances, but this technique works good at short distances (~2 meters). Let's see why we arranged it in this way, check out the ray diagram below:

When the laser beam is projected at some surface, it makes a laser dot on it and then image of that laser dot is formed at focal plane of web cam. Assuming that angle of view of camera is parallel, then by applying laws of similar triangles, we get:

h' / h = f' / D
h': height of laser dot(image) from center(image height)
h: height of laser beam below camera(actual height)
f': focus of camera, distance of lens to focal plane
D: we want this distance(range)
so D = (f' * h) / h'

Since focus of the camera doesn't change much at small distances, laser is fixed and it's height won't change so everything is constant here except h'(image height). We get our relation that D is inversely proportional to h'.

We know that a pixel is a unit of measurement so we want to re-define h' then we could say that h' is directly proportional to number pixels of laser dot from center of image or mathematically:

h' = k * (pixels of laser dot from center)

where k is some constant depends on type of web cam and computer used. Now our final equation to find distance has reduced to a simple equation below:

D = K / (pixels of laser dot from center)

To find constant K we could choose experimental approach. We use known values to calculate constant of proportionality. It's just needed once and I've included this option in my program. All you need is to hold a piece of paper at a known distance, let's say 10cm from webcam and simply press a button and the computer will calculate the constant of proportionality.

## Image Processing

Before reading further, please take a look at my previous article for "how to include webcam within your C# application".

Now we know that distance is inversely proportional to pixels of laser dot from the center of image. But first of all, we need to extract the laser dot from image. Well it's fairly easy using a computer vision library called AForge. In my case, laser dot is a highly red blob, so it could be extracted using color filter:

//making clone of frame received from webcam
Bitmap image = (Bitmap)eventArgs.Frame.Clone();

//creating instance of color filtering for detection of laser light
ColorFiltering colr_filter = new ColorFiltering();

//setting the color filter properties
colr_filter.Red.Min = color_data.RedDown;
colr_filter.Red.Max = color_data.RedUp;

colr_filter.Blue.Min = color_data.BlueDown;
colr_filter.Blue.Max = color_data.BlueUp;

colr_filter.Green.Min = color_data.GreenDown;
colr_filter.Green.Max = color_data.GreenUp;

//apply filter and storing filtered image in another bitmap
Bitmap gray_image = colr_filter.Apply(image);

Note: These RedDown, RedUp, BlueDown, etc. are pre-defined values within range of 0-255. Consider the following color filter code:

// create filter
ColorFiltering colorFilter = new ColorFiltering( );
// configure the filter
colorFilter.Red   = new IntRange( 0, 100 );
colorFilter.Green = new IntRange( 0, 200 );
colorFilter.Blue  = new IntRange( 150, 255 );
// apply the filter
Bitmap objectImage = colorFilter.Apply( image );

And when this code is applied on the image above, the resultant would be:

Similarly, once we isolate the laser dot, we convert image to 8bpp gray scale for using other filters.

//filter to convert RGB image to 8bpp gray scale for image processing
IFilter gray_filter = new GrayscaleBT709();
gray_image = gray_filter.Apply(gray_image);

Further, we apply threshold filter to retain only bright objects such as laser dot.

//thresholding a image
Threshold th_filter = new Threshold(color_data.threshold);
th_filter.ApplyInPlace(gray_image);

Then we apply erosion filter to remove unwanted and small pixels and then finally apply dilation filter to make laser blob more prominent.

//erosion filter to filter out small unwanted pixels
Erosion3x3 err_filter = new Erosion3x3();
err_filter.ApplyInPlace(gray_image);
//dilation filter
Dilatation3x3 dil_filter = new Dilatation3x3();
dil_filter.ApplyInPlace(gray_image);

I've included a color tune form where you could play around with filters and visually see how changing values of certain filters could change the original image. You may find this option in Filters>>Edit.

Once we have a white blob on laser dot, we use blob counter class to detect its location and geometric center. See the code below:

//initialize a blob counting object to count blobs in image
BlobCounter bc = new BlobCounter();
//arrange blobs by area
bc.ObjectsOrder = ObjectsOrder.Area;
bc.MinHeight = 15;
bc.MaxWidth = 15;

//process image for blobs
bc.ProcessImage(gray_image);
gray_image.Dispose();

To find the geometric center of the blob, we use the following code:

//get all the blobs from image
Blob[] b = bc.GetObjectsInformation();

//finding center of gravity of blob detected(b[0] is biggest blob by area)
IntPoint center_blob = b[0].CenterOfGravity;

Once we have the location of laser dot, then we could easily calculate the number of pixels from center of image by simple code:

Math.Abs((float)(previous_image.Height/2) - y_cord)

where y_cord is y coordinate of laser dot.

Now we are left with one more thing that is constant of calibration, we find this value experimentally. We choose a surface which is at a known distance from the camera and then we flash laser. Computer detects that laser light, finds the pixels from center and multiplies it with known distance and we get a constant. I've included this feature in the program, you may find it in Tools>>calibrate.

That's it! We are done building our laser range finder.

## Using the Program

You may first need to adjust the color filter to detect the laser light. When you are adjusting the settings of filter, remember that the white region is considered as a blob and black is rejected. Also you need to calibrate, it's pretty simple just go to Tools>>Calibrate and after that, the program will instruct you accordingly.

You could also change the source code to detect the edges of moving object. Basically, edges mean sudden change of range and we could also build some kind of robots that would follow some object by detecting its edges. It may also be used in autonomous robotics. For now, have fun with it. If you like it or found any error or need to give any suggestion, then share it with me though comments.

## Update

Just updated the program, I found that sometimes it's not able to detect laser dot or at least not with full precision. So I added a manual mode to define laser dot on its own. You can download it from the link at the top of this article.

## References

cat me.txt

 First PrevNext
 My vote of 5 Amir Mohammad Nasrollahi 11-Aug-13 20:14
 Good Job OmarGW 12-Mar-13 12:59
 My vote of 5 haji1245 19-Jan-13 22:49
 My vote of 5 umair ehsan 17-Dec-12 0:03
 My vote of 5 Zaid Pirwani 14-Dec-12 6:45
 My vote of 5 Shine Jacob (Enot) 13-Oct-12 10:24
 i need the coordinations for laser point (X,y) values?? esaaco 27-Sep-12 3:09
 project gokul8983 31-Aug-12 4:07
 excellent work sario100 16-Dec-11 20:58
 Source for manual mode? Diagram 8-Nov-11 4:58
 How track more than one blob Member 7921268 14-May-11 12:58
 Re: How track more than one blob Shivamkalra 14-May-11 14:29
 Re: How track more than one blob Member 7921268 14-May-11 23:42
 My vote of 3 GreenShoes 16-Aug-10 17:48
 Good Yves 12-Jul-10 14:07
 My vote of 3 Corey Fournier 7-Jul-10 10:00
 Re: My vote of 3 Shivamkalra 7-Jul-10 12:18
 Re: My vote of 3 Corey Fournier 7-Jul-10 12:58
 Re: My vote of 3 Shivamkalra 7-Jul-10 13:04
 Check out the references section, above licenses and below updates you will see I referred to original article though I just copied one image from there. And I've stated valid reason for voting 1 on your article.
 Re: My vote of 3 Corey Fournier 7-Jul-10 13:07
 Re: My vote of 3 Shivamkalra 7-Jul-10 13:17
 Re: My vote of 3 Corey Fournier 7-Jul-10 13:38
 Very cool as always, Shivam Marcelo Ricardo de Oliveira 6-Jul-10 7:15
 Re: Very cool as always, Shivam Shivamkalra 6-Jul-10 7:22
 My vote of 5 Khaniya 5-Jul-10 22:48
 Re: My vote of 5 M i s t e r L i s t e r 6-Jul-10 14:13
 Re: My vote of 5 Khaniya 13-Jul-10 19:16
 Good Job Khaniya 5-Jul-10 22:47
 Re: Good Job Shivamkalra 6-Jul-10 7:44
 Re: Good Job Philippe Mori 7-Jul-10 2:31
 Re: Good Job Shivamkalra 7-Jul-10 8:19
 My vote of 5 Arun Jacob 5-Jul-10 22:38
 Re: My vote of 5 Shivamkalra 6-Jul-10 7:43
 Re: My vote of 5 nguyenminhkhoi 13-Sep-12 17:43
 My vote of 5 Henry Minute 5-Jul-10 9:55
 Re: My vote of 5 Shivamkalra 5-Jul-10 9:58
 Please ignore this post Henry Minute 5-Jul-10 9:53
 working on a similar idea c0ax_lx 5-Jul-10 3:30
 Re: working on a similar idea Shivamkalra 5-Jul-10 9:38
 Re: working on a similar idea Ross Brigoli 16-Aug-10 18:25
 Re: working on a similar idea Shivamkalra 17-Aug-10 2:20
 Possible Enhancements Tim Craig 3-Jul-10 17:51
 Re: Possible Enhancements Shivamkalra 5-Jul-10 9:37
 Re: Possible Enhancements Tim Craig 5-Jul-10 18:10
 Re: Possible Enhancements Shivamkalra 5-Jul-10 18:23
 Re: Possible Enhancements M i s t e r L i s t e r 6-Jul-10 14:11
 Re: Possible Enhancements Shivamkalra 6-Jul-10 14:13
 Re: Possible Enhancements M i s t e r L i s t e r 6-Jul-10 14:16
 Re: Possible Enhancements Shivamkalra 6-Jul-10 14:18
 Re: Possible Enhancements Tim Craig 6-Jul-10 18:46
 Last Visit: 31-Dec-99 18:00     Last Update: 22-Jul-14 6:49 Refresh 12 Next »

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140718.1 | Last Updated 7 Jul 2010
The provided text is a technical article that describes a method for creating a laser range finder using a webcam and a laser pen. The article explains the principles behind the device, the image processing techniques used to detect the laser dot, and the calibration process. It also includes code snippets and a brief guide on how to use the program.
no_question

the text does not contain any explicit mathematical questions or provide answers to such questions. it is primarily a technical explanation and guide.
Given a text passage, determine:
    1. If it contains a mathematical question (including word problems or calculations)
    2. If an answer to that mathematical question is provided in the text

First, describe the content structure you found in 1-2 sentences.

Then provide your analysis in the following format:
TYPE: <one of the following values>
- NO_QUESTION
- SINGLE_QUESTION
- SINGLE_QUESTION_WITH_ANSWER
- MULTIPLE_QUESTIONS
- MULTIPLE_QUESTIONS_WITH_ANSWERS
system
Article

# Computer Vision: Laser Range Finder

, 7 Jul 2010
 Rate this:
Finding the distance of an object using a laser pen and an ordinary web-cam !

## Introduction

These days, there are many range finder devices which are used almost everywhere from robotics to country's defence system. All these devices work well, but there are portability and cost concerns for general purpose robotics applications. From my previous articles, I've been creating cool programs related to webcam and now I've come up with a laser-web cam range finder. It could tell how far an object is by just using a very cheap laser pen and an ordinary web cam.

## How It Works

First, we need to arrange the laser pen exactly below the web cam so that it's parallel to the optical axis of the camera's lens. Practically, it's not possible even a minor error would give a huge error at greater distances, but this technique works good at short distances (~2 meters). Let's see why we arranged it in this way, check out the ray diagram below:

When the laser beam is projected at some surface, it makes a laser dot on it and then image of that laser dot is formed at focal plane of web cam. Assuming that angle of view of camera is parallel, then by applying laws of similar triangles, we get:

h' / h = f' / D
h': height of laser dot(image) from center(image height)
h: height of laser beam below camera(actual height)
f': focus of camera, distance of lens to focal plane
D: we want this distance(range)
so D = (f' * h) / h'

Since focus of the camera doesn't change much at small distances, laser is fixed and it's height won't change so everything is constant here except h'(image height). We get our relation that D is inversely proportional to h'.

We know that a pixel is a unit of measurement so we want to re-define h' then we could say that h' is directly proportional to number pixels of laser dot from center of image or mathematically:

h' = k * (pixels of laser dot from center)

where k is some constant depends on type of web cam and computer used. Now our final equation to find distance has reduced to a simple equation below:

D = K / (pixels of laser dot from center)

To find constant K we could choose experimental approach. We use known values to calculate constant of proportionality. It's just needed once and I've included this option in my program. All you need is to hold a piece of paper at a known distance, let's say 10cm from webcam and simply press a button and the computer will calculate the constant of proportionality.

## Image Processing

Before reading further, please take a look at my previous article for "how to include webcam within your C# application".

Now we know that distance is inversely proportional to pixels of laser dot from the center of image. But first of all, we need to extract the laser dot from image. Well it's fairly easy using a computer vision library called AForge. In my case, laser dot is a highly red blob, so it could be extracted using color filter:

//making clone of frame received from webcam
Bitmap image = (Bitmap)eventArgs.Frame.Clone();

//creating instance of color filtering for detection of laser light
ColorFiltering colr_filter = new ColorFiltering();

//setting the color filter properties
colr_filter.Red.Min = color_data.RedDown;
colr_filter.Red.Max = color_data.RedUp;

colr_filter.Blue.Min = color_data.BlueDown;
colr_filter.Blue.Max = color_data.BlueUp;

colr_filter.Green.Min = color_data.GreenDown;
colr_filter.Green.Max = color_data.GreenUp;

//apply filter and storing filtered image in another bitmap
Bitmap gray_image = colr_filter.Apply(image);

Note: These RedDown, RedUp, BlueDown, etc. are pre-defined values within range of 0-255. Consider the following color filter code:

// create filter
ColorFiltering colorFilter = new ColorFiltering( );
// configure the filter
colorFilter.Red   = new IntRange( 0, 100 );
colorFilter.Green = new IntRange( 0, 200 );
colorFilter.Blue  = new IntRange( 150, 255 );
// apply the filter
Bitmap objectImage = colorFilter.Apply( image );

And when this code is applied on the image above, the resultant would be:

Similarly, once we isolate the laser dot, we convert image to 8bpp gray scale for using other filters.

//filter to convert RGB image to 8bpp gray scale for image processing
IFilter gray_filter = new GrayscaleBT709();
gray_image = gray_filter.Apply(gray_image);

Further, we apply threshold filter to retain only bright objects such as laser dot.

//thresholding a image
Threshold th_filter = new Threshold(color_data.threshold);
th_filter.ApplyInPlace(gray_image);

Then we apply erosion filter to remove unwanted and small pixels and then finally apply dilation filter to make laser blob more prominent.

//erosion filter to filter out small unwanted pixels
Erosion3x3 err_filter = new Erosion3x3();
err_filter.ApplyInPlace(gray_image);
//dilation filter
Dilatation3x3 dil_filter = new Dilatation3x3();
dil_filter.ApplyInPlace(gray_image);

I've included a color tune form where you could play around with filters and visually see how changing values of certain filters could change the original image. You may find this option in Filters>>Edit.

Once we have a white blob on laser dot, we use blob counter class to detect its location and geometric center. See the code below:

//initialize a blob counting object to count blobs in image
BlobCounter bc = new BlobCounter();
//arrange blobs by area
bc.ObjectsOrder = ObjectsOrder.Area;
bc.MinHeight = 15;
bc.MaxWidth = 15;

//process image for blobs
bc.ProcessImage(gray_image);
gray_image.Dispose();

To find the geometric center of the blob, we use the following code:

//get all the blobs from image
Blob[] b = bc.GetObjectsInformation();

//finding center of gravity of blob detected(b[0] is biggest blob by area)
IntPoint center_blob = b[0].CenterOfGravity;

Once we have the location of laser dot, then we could easily calculate the number of pixels from center of image by simple code:

Math.Abs((float)(previous_image.Height/2) - y_cord)

where y_cord is y coordinate of laser dot.

Now we are left with one more thing that is constant of calibration, we find this value experimentally. We choose a surface which is at a known distance from the camera and then we flash laser. Computer detects that laser light, finds the pixels from center and multiplies it with known distance and we get a constant. I've included this feature in the program, you may find it in Tools>>calibrate.

That's it! We are done building our laser range finder.

## Using the Program

You may first need to adjust the color filter to detect the laser light. When you are adjusting the settings of filter, remember that the white region is considered as a blob and black is rejected. Also you need to calibrate, it's pretty simple just go to Tools>>Calibrate and after that, the program will instruct you accordingly.

You could also change the source code to detect the edges of moving object. Basically, edges mean sudden change of range and we could also build some kind of robots that would follow some object by detecting its edges. It may also be used in autonomous robotics. For now, have fun with it. If you like it or found any error or need to give any suggestion, then share it with me though comments.

## Update

Just updated the program, I found that sometimes it's not able to detect laser dot or at least not with full precision. So I added a manual mode to define laser dot on its own. You can download it from the link at the top of this article.

## References

cat me.txt

 First PrevNext
 My vote of 5 Amir Mohammad Nasrollahi 11-Aug-13 20:14
 Good Job OmarGW 12-Mar-13 12:59
 My vote of 5 haji1245 19-Jan-13 22:49
 My vote of 5 umair ehsan 17-Dec-12 0:03
 My vote of 5 Zaid Pirwani 14-Dec-12 6:45
 My vote of 5 Shine Jacob (Enot) 13-Oct-12 10:24
 i need the coordinations for laser point (X,y) values?? esaaco 27-Sep-12 3:09
 project gokul8983 31-Aug-12 4:07
 excellent work sario100 16-Dec-11 20:58
 Source for manual mode? Diagram 8-Nov-11 4:58
 How track more than one blob Member 7921268 14-May-11 12:58
 Re: How track more than one blob Shivamkalra 14-May-11 14:29
 Re: How track more than one blob Member 7921268 14-May-11 23:42
 My vote of 3 GreenShoes 16-Aug-10 17:48
 Good Yves 12-Jul-10 14:07
 My vote of 3 Corey Fournier 7-Jul-10 10:00
 Re: My vote of 3 Shivamkalra 7-Jul-10 12:18
 Re: My vote of 3 Corey Fournier 7-Jul-10 12:58
 Re: My vote of 3 Shivamkalra 7-Jul-10 13:04
 Check out the references section, above licenses and below updates you will see I referred to original article though I just copied one image from there. And I've stated valid reason for voting 1 on your article.
 Re: My vote of 3 Corey Fournier 7-Jul-10 13:07
 Re: My vote of 3 Shivamkalra 7-Jul-10 13:17
 Re: My vote of 3 Corey Fournier 7-Jul-10 13:38
 Very cool as always, Shivam Marcelo Ricardo de Oliveira 6-Jul-10 7:15
 Re: Very cool as always, Shivam Shivamkalra 6-Jul-10 7:22
 My vote of 5 Khaniya 5-Jul-10 22:48
 Re: My vote of 5 M i s t e r L i s t e r 6-Jul-10 14:13
 Re: My vote of 5 Khaniya 13-Jul-10 19:16
 Good Job Khaniya 5-Jul-10 22:47
 Re: Good Job Shivamkalra 6-Jul-10 7:44
 Re: Good Job Philippe Mori 7-Jul-10 2:31
 Re: Good Job Shivamkalra 7-Jul-10 8:19
 My vote of 5 Arun Jacob 5-Jul-10 22:38
 Re: My vote of 5 Shivamkalra 6-Jul-10 7:43
 Re: My vote of 5 nguyenminhkhoi 13-Sep-12 17:43
 My vote of 5 Henry Minute 5-Jul-10 9:55
 Re: My vote of 5 Shivamkalra 5-Jul-10 9:58
 Please ignore this post Henry Minute 5-Jul-10 9:53
 working on a similar idea c0ax_lx 5-Jul-10 3:30
 Re: working on a similar idea Shivamkalra 5-Jul-10 9:38
 Re: working on a similar idea Ross Brigoli 16-Aug-10 18:25
 Re: working on a similar idea Shivamkalra 17-Aug-10 2:20
 Possible Enhancements Tim Craig 3-Jul-10 17:51
 Re: Possible Enhancements Shivamkalra 5-Jul-10 9:37
 Re: Possible Enhancements Tim Craig 5-Jul-10 18:10
 Re: Possible Enhancements Shivamkalra 5-Jul-10 18:23
 Re: Possible Enhancements M i s t e r L i s t e r 6-Jul-10 14:11
 Re: Possible Enhancements Shivamkalra 6-Jul-10 14:13
 Re: Possible Enhancements M i s t e r L i s t e r 6-Jul-10 14:16
 Re: Possible Enhancements Shivamkalra 6-Jul-10 14:18
 Re: Possible Enhancements Tim Craig 6-Jul-10 18:46
 Last Visit: 31-Dec-99 18:00     Last Update: 22-Jul-14 6:49 Refresh 12 Next »

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140718.1 | Last Updated 7 Jul 2010
user
The provided text is a technical article that describes a method for creating a laser range finder using a webcam and a laser pen. The article explains the principles behind the device, the image processing techniques used to detect the laser dot, and the calibration process. It also includes code snippets and a brief guide on how to use the program.

TYPE: NO_QUESTION

The text does not contain any explicit mathematical questions or provide answers to such questions. It is primarily a technical explanation and guide.