# Calculating the distance between a camera and a target using camera output

I have a 640x480 camera that recognizes a rectangle that is 1ftx2ft. Is it possible to calculate the distance between the camera and the rectangle?

Edit:

The horizontal angle of view is 54°.

• You will need to know the field of view of the camera and how many pixels wide the rectangle appears in order to calculate the distance away. Commented Jan 12, 2013 at 23:24
• I have these measurements, I don't know what to do next. Commented Jan 12, 2013 at 23:29
• Knowing the field of view you can calculate how far away the rectangle would have to be to completely fill the view with trigonometry. Commented Jan 12, 2013 at 23:42

## 1 Answer

$$D=\frac{Lw}{2x\tan\frac\alpha2}$$ where $D$ = Distance from Camera, $L$ = length of object (2ft), $w$=width of screen in pixels (640), $x$=width of object on camera in pixels, and $\alpha$ = camera field of view.

• Is it assumed that the rectangle is in the center of the screen or it doesn't matter? Commented Jan 13, 2013 at 0:31
• With a narrow field of view it shouldn't matter where the rectangle is. But it is important that the rectangle is facing flat on to the camera. If you had been using a wide angle lens things might be trickier. Commented Jan 13, 2013 at 9:04