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

1 answer

Sort by ยป oldest newest most voted

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: 427 times

Last updated: Mar 18