1 | initial version |

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.

2 | No.2 Revision |

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.

3 | No.3 Revision |

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

Copyright OpenCV foundation, 2012-2018. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.