# OpenCv calculate or convert the speed of a moving Object

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???

edit retag close merge delete

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

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

Sort by » oldest newest most voted

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.

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

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, 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 sounds simple... but it could work just for linear movements... like car passing. Do you have any idea for movements without any specific paths?

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

then:

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

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 https://www.translatorscafe.com/unit-converter/EN/typography/4-8/%D0%BC%D0%B8%D0%BB%D0%BB%D0%B8%D0%BC%D0%B5%D1%82%D1%80-%D0%BF%D0%B8%D0%BA%D1%81%D0%B5%D0%BB%D1%8C%20(X)/ (PixelToMm)

http://www.jatit.org/volumes/Vol84No2/9Vol84No2.pdf (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??

1

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

@ Berak did you Open this link (http://www.jatit.org/volumes/Vol84No2/9Vol84No2.pdf (http://www.jatit.org/volumes/Vol84No2...) ) and try to see if you could understand my problem?

Official site

GitHub

Wiki

Documentation