Ask Your Question

finding distance between car on real-time

asked 2019-08-29 16:15:16 -0500

Ayeshayounis gravatar image

updated 2019-09-10 03:52:19 -0500

i am trying to make a project with Opencv python on real-time finding distance from my dash cam ,which can tell me how much other car is far from me.i already make a code in python opencv which can detect objects like 98% car 88% person ,i need your help how i can find distance ?some other sites they showed me to use calibration method as i am very new in this filed i dont know where and how i can use it . thank you

edit retag flag offensive close merge delete


helo, it's unclear, what youre doing, and where the problem is.

berak gravatar imageberak ( 2019-08-30 02:07:26 -0500 )edit

you want to find the distance to cars in front of your car from a dash cam?

Witek gravatar imageWitek ( 2019-08-31 05:20:44 -0500 )edit

yes witek i want find distance in real time can you please help me i am very new in this field i have already one program which can just detect things but no distance

Ayeshayounis gravatar imageAyeshayounis ( 2019-09-10 03:45:10 -0500 )edit

I couldn't find link for you. Also I'm unable attempting to find my file that was about 4 yrs. ago

supra56 gravatar imagesupra56 ( 2019-09-11 22:23:21 -0500 )edit

so you worked on same project may be you remember somehow ,how to do it?

Ayeshayounis gravatar imageAyeshayounis ( 2019-09-12 00:22:05 -0500 )edit this is the link which is the code which i am using help me how i can edit it to find distance in real time

Ayeshayounis gravatar imageAyeshayounis ( 2019-09-12 00:27:23 -0500 )edit

No. it is not pyimagesearch. There is university teacher posting on blog dating back to 2010. I was using raspberry pi 2B+. I was going to do raspberry pi 4b .Firstly, I was going to install OpenCV 4.1.2 by this weeken end.

supra56 gravatar imagesupra56 ( 2019-09-12 09:33:18 -0500 )edit

Is calculating the distance in pixels an acceptable unit of measure? If yes, then it is a simple Euclidean distance calculation from detected box's edges or centroid to the next one

eshirima gravatar imageeshirima ( 2019-09-12 10:59:22 -0500 )edit

@eshirima. The answer is NO!

supra56 gravatar imagesupra56 ( 2019-09-12 12:53:04 -0500 )edit

@Ayeshayounis. I found link and github. It was dating back to 2016. I am away from home. I be back soon.

supra56 gravatar imagesupra56 ( 2019-09-12 12:54:38 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-09-12 15:38:49 -0500

Witek gravatar image

updated 2019-09-12 15:45:43 -0500

You can get an approximate distance to an object in the image by using the pinhole camera model: image description

from the similarity of triangles you get:



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

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2019-08-29 16:15:16 -0500

Seen: 1,495 times

Last updated: Sep 12 '19