Skip to content

Real time Video Stabilization

Abhishek Thakur edited this page Apr 24, 2020 · 44 revisions

VidGear Logo

Real-time Video Stabilization

VideoGear Stabilizer in action!
Original Video Courtesy @SIGGRAPH2013

VidGear now provides real-time Video Stabilization with minimalistic latency and at the expense of little to no additional computational power requirement with Stabilizer Class. The basic idea behind it is to tracks and save the salient feature array for the given number of frames and then uses these anchor point to cancel out all perturbations relative to it for the incoming frames in the queue. This stabilization feature in VideoGear can be activated with stabilize boolean flag during initialization.

 

Features :

  • Real-time stabilization with low latency and no extra resources.

  • Works exceptionally well with low-frequency jitter.

  • Integrated with VideoGear API, therefore, can be applied to any incoming stream.

  • Also seamlessly works standalone.

 


⚠️ Important Information:

  • The stabilizer may not perform well against High-frequency jitter in video. Use at your own risk!

  • It is advised to enable logging (logging = True) on the first run, to easily identify any runtime errors.


 

 

Table of Contents:

  • Importing
  • Usage
    • Using VideoGear Class with stabilization activated
    • Using Stabilizer Class Directly with OpenCV
    • Using Stabilizer Class with other Gears

 

 

Usage 🔨

1. Using VideoGear Class with stabilization activated: (Recommended)

 

2. Using Stabilizer Class Directly with OpenCV:

The VidGear's stabilizer class can also work standalone easily with any Computer Vision library such as OpenCV itself.

The suitable example is as follows:

💡 Stabilizer Class provides exclusive Attributes and Parameters which can be found here ➶.

# import required libraries
from vidgear.gears.stabilizer import Stabilizer
import cv2

# Open suitable video stream, such as webcam on first index(i.e. 0)
stream = cv2.VideoCapture(0) 

#initiate stabilizer object with default parameters
stab = Stabilizer()

# loop over
while True:

    # read frames from stream
    (grabbed, frame) = stream.read()

    # check for frame if not grabbed
    if not grabbed:
      break

    # send current frame to stabilizer for processing
    stabilized_frame = stab.stabilize(frame)
    
    # wait for stabilizer which still be initializing
    if stabilized_frame is None:
      continue 


    # {do something with the frame here}
     

    # Show output window
    cv2.imshow("Stabilized Frame", stabilized_frame)

    # check for 'q' key if pressed
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
      break

# close output window
cv2.destroyAllWindows()

#clear stabilizer resources
stab.clean()

# safely close video stream
stream.release()

 

3. Using Stabilizer Class with other Gears:

VideoGear's stabilizer can be used in conjunction with WriteGear API directly without any compatibility issues.

The suitable example is as follows:

# import required libraries
from vidgear.gears.stabilizer import Stabilizer
from vidgear.gears import WriteGear
import cv2

# Open suitable video stream
stream = cv2.VideoCapture("unstabilized_stream.mp4") 

#initiate stabilizer object with default parameters
stab = Stabilizer()

# Define writer with default parameters and suitable output filename for e.g. `Output.mp4`
writer = WriteGear(output_filename = 'Output.mp4') 

# loop over
while True:

    # read frames from stream
    (grabbed, frame) = stream.read()

    # check for frame if not grabbed
    if not grabbed:
      break

    # send current frame to stabilizer for processing
    stabilized_frame = stab.stabilize(frame)
    
    # wait for stabilizer which still be initializing
    if stabilized_frame is None:
      continue 


    # {do something with the frame here}


    # write stabilized frame to writer
    writer.write(stabilized_frame)
     
    # Show output window
    cv2.imshow("Stabilized Frame", stabilized_frame)

    # check for 'q' key if pressed
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
      break

# close output window
cv2.destroyAllWindows()

#clear stabilizer resources
stab.clean()

# safely close video stream
stream.release()

# safely close writer
writer.close()

 

Clone this wiki locally