Measure the distance of object to the camera


Distance of circle from camera

Some days ago, I was talking to my friends and one of them asked me if I can write a program to measure the distance of the object to the camera, so I told myself why not write a post about it on my blog. I got the idea of writing this code from Adrian’s blog. You can find the code of distance of object to the camera at the end of this post

In order to determine the distance from our camera to a known object or marker, I am going to utilize triangle similarity.

The triangle similarity goes something like this: Let’s say I have a marker or object with a known width W. Then I place this marker some distance D from my camera. I take a picture of my object using our camera and then measure the apparent width in pixels P. This allows me to derive the perceived focal length F of my camera:

F = (P x D) / W

For example, I place a 21 x 29cm piece of paper (vertically; W = 21) D = 20 cm in front of my camera and take a photo. When I measure the width of the piece of paper in the image, I notice that the perceived width of the paper is P = 133 pixels.

My focal length F is then:

F = (1338px x 20cm) / 21cm = 126.35

As I continue to move my camera both closer and farther away from the object/marker, I can apply the triangle similarity to determine the distance of the object to the camera:

D’= (W x F) / P

Continue reading

Hand detection (using skin tone)

Hand detection using skin tone is the simplest way to detect hand. Problems of this method are the background should not contain skin colored, really sensitive to light and shape of background!

Continue reading

Object tracking

Tracking of the blue object (and also my glasses which are blue as well ;D)

Tracking of the blue object (and also my glasses which are blue as well ;D)

  1. Take each frame of the video
  2. Convert from BGR to HSV color-space (HSV, it is more easier to represent a color than RGB color-space)
  3. We threshold the HSV image for a range of blue color
  4. Now extract the blue object, we can do whatever on that image we want.

Continue reading