I have a video feed which is taken with a moving camera and contains moving objects. I would like to stabilize the video, so that all stationary objects will remain stationary in the video feed. How can I do this with OpenCV?
i.e. For example, if I have two images prev_frame and next_frame, how do I transform next_frame so the video camera appears stationary?
I can suggest one of the following solutions:
EDIT Three remarks I should better mention explicitly, just in case:
OpenCV has the functions estimateRigidTransform() and warpAffine() which handle this sort of problem really well.
Its pretty much as simple as this:
Mat M = estimateRigidTransform(frame1,frame2,0)
warpAffine(frame2,output,M,Size(640,480),INTER_NEAREST|WARP_INVERSE_MAP)
Now output
contains the contents of frame2
that is best aligned to fit to frame1
.
For large shifts, M will be a zero Matrix or it might not be a Matrix at all, depending on the version of OpenCV, so you'd have to filter those and not apply them. I'm not sure how large that is; maybe half the frame width, maybe more.
The third parameter to estimateRigidTransform is a boolean that tells it whether to also apply an arbitrary affine matrix or restrict it to translation/rotation/scaling. For the purposes of stabilizing an image from a camera you probably just want the latter. In fact, for camera image stabilization you might also want to remove any scaling from the returned matrix by normalizing it for only rotation and translation.
Also, for a moving camera, you'd probably want to sample M through time and calculate a mean.
Here are links to more info on estimateRigidTransform(), and warpAffine()
openCV now has a video stabilization class: http://docs.opencv.org/trunk/d5/d50/group__videostab.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With