'Detecting camera motion with opencv

I am working on drone stabilization close to walls using a camera. For this to work I need to extract the motion the camera makes relative to the wall. For now I used an expanded OpenCV example which uses the goodFeaturesToTrack command to find feature points in every frame. These feature points are then tracked into the next frame using calcOpticalFlowPyrLK which uses the Lucas-Kanade method. I then subtract the point locations to calculate the displacement. Adding all displacements together gets me the total displacement from the first frame. (in between I did some averaging and filtering).

The results I get do not look like the motion of the camera at all. The motion goes in any direction. Does anybody have any idea what's going wrong? Am I using the wrong algorithm for a problem like this?



Sources

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

Source: Stack Overflow

Solution Source