Ask Your Question

# finding speed of vehicle in video [closed]

Can you please assist me with getting the speed of vehicles in a video. I have googled a lot on this but I could not find a solution. My task was to count cars and get the speed of every car and save to a stack/array. I have been able to count the cars but I an not sure how to calculate the speed. I had imagined I would do it in this manner.

1. T= Total number of frames in the video
2. t(enter) = the time at which one vehicle enters the scene
3. t(leave) = the time at which the same vehicle leaves the scene
4. T(frame) = the total time consumed by one frame
5. T(total) = the total number of frames that were consumed by the vehicle to enter and leave the scene
6. d = the total distance(in meters) of the road; the distance of the road that is on interest: the real distance.

I was then hoping that speed in KM/h may be calculated as follows speed = d/[t(left)-t(enter)]

I was then hoping to take each of the speeds and save then to a stack/array for further processing.

I am not sure if this is the correct way of processing speed from a video.

Thank you in advance.

edit retag reopen merge delete

## 1 answer

Sort by » oldest newest most voted

If you are able to detect and track cars and you know the real distance travelled, then should be an easy task.

If you want velocity in km/h, you need to transform the time T from total frames to hours.

For that you have to calculate the framerate of your videostream, for which I point you to this answer of mine where I give an example of how to do that

http://answers.opencv.org/question/93...

So, if you divide the total number of frames the car was present in the scene by the current framerate, you'll get the number of seconds the car was present in the scene.

Time [seconds] = Total [Frames] / [Frames / Second] .

Edit:

If you know the distance, the framerate, the total number of frames the car is present in the scene you don't need anything else to calculate the speed.

For each car you detect you need to create an ID, and for each frame that ID is present on the scene you need to increment a counter. This means you'll have to match each car detection in between frames to keep counting.

Imagine that the distance of the road in the scene is 200 meters, the car goes from begining to end in 120 frames and the framerate is 30 fps. The time the car was present in the video was 120 / 30 = 4 seconds. So, the car travelled 200 meters in 4 seconds. That means the speed is 50 m/s which then you can convert to 180 km/h.

more

## Comments

Note: This will give you the average speed of the car on that particular path.

May you please assist me with a code snippet. I have been able to get the frame rate total number of frames with the blow code

   void video_details(VideoCapture& cap)
{

cout << "The video has  " << cap.get(CAP_PROP_FRAME_COUNT) << "  frames" << endl;
cout << "The video is playing at  " << cap.get(CV_CAP_PROP_FPS) << "   frames per second." << endl;
}


I do not know how to get the remaining variables in order to get the speed. Thank you for the response

from a mathematical point of view I understand everything you are saying I should do. I just dnt know how to code it. I got this far by following tutorials and examples. the code I have draws boxes around each car after running background subtraction. as each box is drawn a counter counts the number of boxes drawn and returns the total number of boxes hence the number of cars. am very new to opencv and learning as I go. How would I find the time and the frame at which a car is observed and leaves the scene? I understand what I need to do but have struggling with the code.

1

The only thing I can do is point you in the right direction here. If you can draw a bounding box around the cars it means at each frame you know the coordinates of such rectangles. If you keep the data that describes each rectangle (x, y, width, height) for each frame and compare them to the detections on the next frame, you can verify area of overlap between bounding boxes. If a detection in one frame has a great overlap with a detection on the next frame it probably means that the object is the same, so you can increment a "life-time" counter for that particular object. If there is no overlap on an object it means that it is new. This requires some skills in general programming, more than in OpenCV.. good luck.

If a car is detected as soon as it enters the frame and stops being detected only when it leaves the frame, you only need to count how much frames it is on the scene to calculate its speed.

Official site

GitHub

Wiki

Documentation

## Stats

Asked: 2016-07-20 08:50:56 -0500

Seen: 3,011 times

Last updated: Jul 20 '16