Ask Your Question

OpenCv calculate or convert the speed of a moving Object

asked 2017-03-18 06:55:51 -0500

bincos gravatar image

I would like to get the real speed of a moving object from my camera or webCam

I used OpenCv **BackgroundSubtractorMOG2** then it return me the Foreground image( the moving object) then I draw two line for eg:

    line( resize_blur_Img, Point(10,0),Point(10,60), Scalar( 255, 0, 10 ),2, 8 );
 line( resize_blur_Img, Point(100,60),Point(100,0), Scalar( 0, 0, 0 ),2, 8 );

as you can see I can find the distance between the two line by a simple way can be ==> 90

and every time we the Object cross my line i save the time in this way

 time_t now = time(0);
  dt = ctime(&now);

dt will return the current time is second for eg dt return to me 1489755120 wich is the result from timeCrossedForLine 2 - timeCrossedForLine2

the when I use a simple formula to get the speed -->> Speed= Distance/Time

Applying to my situation I will get Speed=(90/1489755120)= 6.041261

-->> My Question is this 6.041261 will be in Km/h ?? or in m/s or other else

if i did not well calculate the speed could you please show the the other way???

Please I need you guy!!!!!

edit retag flag offensive close merge delete


please do not post answers here, if you have a question or comment. thank you.

berak gravatar imageberak ( 2017-10-25 07:30:31 -0500 )edit

Hi @bincos! I'd like to use this to measure the speed of bulls crossing the lines. Could you share your code with me? Thanks

denissato gravatar imagedenissato ( 2018-08-29 03:11:21 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-10-25 07:24:35 -0500

sama_z gravatar image

Hi @bincos I have the same problem. Please let me know if you have found a solution for the problem and share your experiences on it.

many thanks in advance

edit flag offensive delete link more


Hi @sama_z, you first have to know the real distance between the two line then you could get the real speed for eg if you distance was 10 metter the the speed will be in metter/second

bincos gravatar imagebincos ( 2017-10-26 18:13:18 -0500 )edit

Thanks @bincon for the reply. Exactly, agree with you, but the main problem is here! I don't know how to calculate the real distance in meters between two line! because information from a normal camera are in pixels not meters. How did you manage that problem?

sama_z gravatar imagesama_z ( 2017-10-31 09:13:14 -0500 )edit

@sama_z, sorry for the delay.....before recording or getting the video on the road you have to measure you self physically the real distance between two point and then when you are making your code you will draw your two line at those two point on the road... this is the easy way to do that

bincos gravatar imagebincos ( 2017-11-05 17:11:42 -0500 )edit

@bincos sounds simple... but it could work just for linear movements... like car passing. Do you have any idea for movements without any specific paths?

sama_z gravatar imagesama_z ( 2017-11-06 09:49:12 -0500 )edit

answered 2017-03-18 07:16:04 -0500

berak gravatar image

ctime returns a string not a double, so your measurement is bs.

then, time() returns time in seconds, imho, you need a much finer grained measurement here

also, (90/1489755120)= 6.041261e-8 (so, next to nothing) and this would be pixels per second, not very useful..

do the following:

measure out, how much distance in the real world is covered between your 2 lines in the image


// measure at 1st line crossed
int64 t0 = cv::getTickCount();

// measure at 2nd line crossed
int64 t1 = cv::getTickCount();
double seconds = (t1 - t0) / cv::getTickCount();

double speed = real_distance / speed; // in meters per second.
edit flag offensive delete link more


Hi @berak thank your for your quick response i would like to know if there is a relationship or a convertion between Pixel and Millimeter or Meter I need Your Answer try to look here (PixelToMm)

bincos gravatar imagebincos ( 2017-03-18 07:35:56 -0500 )edit

(you would need to calibrate your camera for this, to get f)

berak gravatar imageberak ( 2017-03-18 07:47:21 -0500 )edit

Thank @berak try to look on this link what this guy made on this Article (VehicleSpeedMesurement) I do not understand on page 4 : where hi specified how to calculate the Speed of the vehicle:

speed=[ ( distanaceInPixel * OriginalTrack) / (Time * TrackImage) ] * Kfactor

I do not understand the two parameters -->1 OriginaTrack (Original track = actual path length (meters)) -->2 TrackImage (Track image = path length in the image (pixels) ) ???

how To find those Two parameters Please Show me the way?? Do you have any idea of understanding those para meter??

bincos gravatar imagebincos ( 2017-03-18 08:25:42 -0500 )edit
LBerger gravatar imageLBerger ( 2017-03-18 09:05:44 -0500 )edit

Hi @LBerger Thanks for your reply but I'm so confused and i do note see how to apply your answer in my situation case

bincos gravatar imagebincos ( 2017-03-18 09:25:35 -0500 )edit

@ Berak did you Open this link ( ( ) and try to see if you could understand my problem?

bincos gravatar imagebincos ( 2017-03-22 04:35:01 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-03-18 06:55:51 -0500

Seen: 5,234 times

Last updated: Mar 18 '17