[Python] Calculate velocity of a point between frames

Discussão em 'Python' iniciado por Stack, Junho 9, 2025.

  1. Stack

    Stack Membro Participativo

    I am working on a project in which I need to calculate object velocity across frames of a video. I am using opencv to open video, extract frames, perform some threshold and contour operations to find object centroids. Now, for velocity calculation of this centroids over frames, is my function correct?

    def calculate_velocity(curr_centroid, prev_centroid, time_step, scale_pixels_to_cm=1, origin=(0, 0)):

    if prev_centroid is None:
    return 0

    prev_vector = np.array(prev_centroid) - np.array(origin)
    curr_vector = np.array(curr_centroid) - np.array(origin)

    displacement_vector = curr_vector - prev_vector
    velocity_vector = displacement_vector / time_step
    velocity_vector_cm = velocity_vector * scale_pixels_to_cm

    speed = np.linalg.norm(velocity_vector_cm)

    return round(speed, 2)


    Main idea here is to get current and previous centroids, and get their vectors relative to the origin, then perform vector subtraction to get displacement vector.

    Speed here is regarded as the magnitude of resultant velocity vector, and it is the most important element to quantify, direction of movement is not relevant

    Thanks

    Continue reading...

Compartilhe esta Página