Search code examples
pythonopencvcomputer-visioncomputer-science

Lucas Kanade: How to calculate distance between tracked points


I'm using lucas-kanade opencv implementation to track objects between frames. I want to be able to do the following two things:

  • Calculate the distance moved by each point between frames
  • Track bounding boxes for each object across frames

I have obtained the features to track using cv2.goodFeaturesToTrack(). I also add the bounding boxes of objects to the features to be tracked. Right now I am using the following to calculate distance between the points np.sqrt(np.square(new_pts - old_pts).sum(axis=1).sum(axis=1)). I am not quite sure if this is the correct way to do this because the indices of the points might be different in the new_pts. Is the assumption that every index in old_pts corresponds to the same feature in new_pts array correct?

Secondly, is there a way to track bounding boxes across frames using lucas kanade?


Solution

  • In new_pts points have the same index. But they can be not founded - see to the status array: if status[i] == 1 then new_pts[i] contains a new coordinates of the old_pts[i].

    For the more robustness it can to search direct flow (goodFeaturesToTrack(frame1) -> LK flow), backward flow (goodFeaturesToTrack(frame2) -> LK flow) and leave the points whose coordinates are equal in both directions.