'How to determine travel distance of an object between frames, using a moving object and camera

If you have a video file with a moving object, as well as a moving camera, is it possible to track the distance that the object moved between 10 or 20 frames? I'm using OpenCV to track the object and have no problem finding the distance it travels when the camera is stationary, but I can't seem to wrap my head around a non-stationary camera.

The feed is only a single 2D camera feed and no other tracking is being done.

The only thing I could think of is to grey out the frame horizontally from the lowest to the highest coordinate of the object within the frame, attempt to layer the frames together, then measure the distance the object traveled already knowing the x,y coordinate of the object in each frame. This doesn't seem like a very clean solution so I'm curious to know if there is anything else out there to solve this problem.



Solution 1:[1]

Assuming the object to be tracked is not covering the whole image and there is enough background visible, you could try to track the camera movement with visual odometry on the static background.

You can then track the relative motion of the object with respect to the camera as if it where static and then transform the motion back to world coordinates with the known camera movement.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 a crazy Minion