# Revision history [back]

You can get an approximate distance to an object in the image by using the pinhole camera model: from the similarity of triangles you get:

x/f=X/Z

where f - focal length of the camera (in pixels),

x - size of the observed object in the image (in pixels),

X - size of the observed object in the real world (in meters, for example),

Z - distance between the camera and the object in the real world (same unit as X).

In order to calculate the distance you are looking for, you need to know f, x, and X. You can get f by calibrating the camera with OpenCV or Matlab or whatever or you can estimate it quickly by putting an object of known size (X) at a known distance (Z) and measuring its size in the image (x).

Since you can detect cars (with a deep network, I presume) you also get their bounding rectangle. The width of this rectangle is the width of the car (x).

Since you don't know the make and model of the detected car, you do not know it's real width. You have to assume it to some average value around, say, 2 meters.

Having all these numbers, you can estimate the distance to the car you see in the image. Of course it will not be very precise, but you don't need a centimeter precision, I suppose. I would say the distance error will be around +-10% but it is a very rough estimate.

You can get an approximate distance to an object in the image by using the pinhole camera model: from the similarity of triangles you get:

x/f=X/Z

where where:

f - focal length of the camera (in pixels),

x - size of the observed object in the image (in pixels),

X - size of the observed object in the real world (in meters, for example),

Z - distance between the camera and the object in the real world (same unit as X).

In order to calculate the distance you are looking for, you need to know f, x, and X. You can get f by calibrating the camera with OpenCV or Matlab or whatever or you can estimate it quickly by putting an object of known size (X) at a known distance (Z) and measuring its size in the image (x).

Since you can detect cars (with a deep network, I presume) you also get their bounding rectangle. The width of this rectangle is the width of the car (x).

Since you don't know the make and model of the detected car, you do not know it's real width. You have to assume it to some average value around, say, 2 meters.

Having all these numbers, you can estimate the distance to the car you see in the image. Of course it will not be very precise, but you don't need a centimeter precision, I suppose. I would say the distance error will be around +-10% but it is a very rough estimate.

You can get an approximate distance to an object in the image by using the pinhole camera model: from the similarity of triangles you get:

x/f=X/Z

where:

f - focal length of the camera (in pixels),

x - size of the observed object in the image (in pixels),

X - size of the observed object in the real world (in meters, for example),

Z - distance between the camera and the object in the real world (same unit as X).

In order to calculate the distance you are looking for, you need to know f, x, and X. You can get f by calibrating the camera with OpenCV or Matlab or whatever or you can estimate it quickly by putting an object of known size (X) at a known distance (Z) and measuring its size in the image (x).

Since you can detect cars (with a deep network, I presume) you also get their bounding rectangle. The width of this rectangle is the width of the car in the image (x).

Since you don't know the make and model of the detected car, you do not know it's real width. You have to assume it to some average value around, say, 2 meters.

Having all these numbers, you can estimate the distance to the car you see in the image. Of course it will not be very precise, but you don't need a centimeter precision, I suppose. I would say the distance error will be around +-10% but it is a very rough estimate.

estimate. If you need more accuracy, the first thing you need to do is to recognize the make and model or at least the type of the vehicle you detect. The estimate of the real car size is the main contributor to error (unless you have a very wide angle lens, but with a standard webcam lens distortions will have negligible effect here).