Search code examples
c++opencvmotion-detection

Is there any C++ opencv code to compute velocity of key feature points in each frame of video?


I would like to compute velocity of key feature points and set thresholding for motion detection in a video.

I have a python code with me as follows:

def compute_vel_kf(self, fps):

   if ((len(self.features) == 0) or (self.features == None)):
        return;
   test_image = self.current_frame.copy();

    time_diff_in_sec = 1/fps;
    self.v = [];

    for i, p1 in enumerate(self.features):
        p2 = self.features_prev[i];
        # speed = dist/time
        vx, vy = [(p1[0][0] - p2[0][0]), (p1[0][1] - p2[0][1])];           
        v = sqrt(vx * vx + vy * vy)*fps;
        ang = math.atan2(vy, vx);
        self.v.append(array([v, ang]));           
        i += 1;

    return self.v;

I have to port it to cpp code. In cpp code i have used points[1] and points[2] that holds current frame & previous frame detected points respectively. I need to calculate velocity of the detected key feature points.


Solution

  • As Tobbe mentioned, you should first try to get some results with sample data, and then ask for help with what you have, about what you need next.

    To give a brief answer, you should first install an image processing library like OpenCV, and then writ some sample code to load and process frames from your video. Then you can segment objects in the first frame, track them in the coming frames, and use the stats to calculate the velocity.

    Edit: Now we can see that you already have the positions in the previous and the current frame. The usual method to get the velocity in pixels/second is to calculate the distance (Euclidian or separate axes, depending on your need) between to the two locations, and then multiply it by the frame rate. However, since the video is most likely taken at a many frames a second, you can also do a weighted averaging with the velocity from the previous frame pair.