OpenCV continous Speed measurement using camera

16,322

Hope you will be using a simple camera with 20 fps to 30 fps and your camera is placed perpendicular to the road but away from it...the object i.e. your cars have a max velocity of 8 ms-1 in the image plane...calculate the speed of the cars in the image plane with the help of the lens you are using...

  ( speed in object plane / distance of camera from road ) = ( speed in image plane / focal length )

you should get in pixels per second if you know how much each pixel measures...

Steps...

  1. You can use frame differentiation...that is subtract the current frame from the previous frame and take the absolute difference...threshold the difference...this segments out your moving car from the back ground...remember this segments all moving objects...so if u want a car and not a moving man you can use the shape characteristic that is the height is to width ratio...fit a rectangle to the segmented part and in each frame do the same steps. so in each frame you can keep a record of the coordinate of the leading edge of the bounding box... that way when a car enters the view till it pass out of the view you know for how long the car has persisted...use the number of frames , the frame rate and the coordinaes of the leading edge of the bounding box to calculate the speed...

  2. You can use goodfeaturestotrack and optical flow of open cv...that way you can make distinguish between fast moving and slow moving objects...but keep refreshing the points that goodfeaturestotrack gives you or else any new car coming into the camera view will not be updated...record the displacement of the set of points picked by goodfeaturestotrack in each frame..that is the displacement of the moving object...calculate speed in the same way...the basic idea to calculate speed is to record the number of frames the object has persisted in the camera field of view...if your camera is fixed so is your field of view...hence what matters is in how many frames are you able to catch the object... remember....the optical flow of opencv works for tracking slow moving objects or more theoretically the feature point (determined by goodfeatures to track..) displacement is less between 2 consecutive frames for the algorithm to work...big displacements will have some erroneous predictions by the algorithm...that is why the speed in the image plane is important..at least qualitatively you should get an idea of it...

NOTE: both the methods are for single object tracking ..for multiple object tracking you need some modifications...however you can start with either of the method...i think it will work..

Share:
16,322
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin about 2 years

    I am new to OPENCV so bear with me if there are simple things that I am missing here.

    I am trying to work out a camera based system that can continuously output the speed of a vehicle with the following assumptions: 1. The camera is horizontally placed and the vehicle passes near 3 to 5 feet of the camera lens. 2. The speed will not be more than 30KM/Hrs

    I was hoping to start with the concept of a optical mouse which detects the displacement in the surface pattern. However I am unclear as to how to handle the background when the vehicle starts to enter the frame.

    There are two methods I was interested in experiment with but am looking for further inputs.

    1. Detect the vehicle as it enters the frame and separate from background.
    2. Use cvGoodFeaturesToTrack to find points on the vehicle.
    3. Track the point across the next frame. & Calculate the horizontal velocity using Lucas_Kanade Pyramid function for optical flow.
    4. Repeat

    Please suggest corrections and amendments. Also I request more experienced members to help me code this procedure efficiently since I don't know which are the most correct functions to use here.

    Thanks in advance.