Skip to content

Real time Video Stabilization

Abhishek Thakur edited this page Jul 19, 2019 · 44 revisions

VidGear Logo

Real-time Video Stabilization

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 class relies heavily on Threaded Queue mode for error-free, ultrafast frame handling and it is enabled by default. 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 works standalone.

 


Important:

  • Remember: This feature as of now is available with testing branch only!
  • ⚠️ The stabilizer may not perform well against High-frequency jitter in video. Use at your own risk! ⚠️

 

Usage:

Firstly, Kindly clone and install the testing branch as follows:

git clone git clone https://github.com/abhiTronix/vidgear.git
cd testing
git checkout testing
sudo pip install .
cd

 

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 as follows:


Note: 💡 Stabilizer Class Class provide access to the certain parameter which is as follows:

  • smoothing_radius:(int) to alter averaging window size. It handles the quality of stabilization at the expense of latency and sudden panning. Larger its value, less will be panning, more will be latency and vice-versa. Its default value is 25.
  • border_size: (int) to alter output border cropping. It's will crop the border to reduce the black borders from stabilization being too noticeable. Larger its value more will be cropping. Its default value is 0 (i.e. no cropping).
  • border_type:(string) to change the border mode. Valid border types are 'black', 'reflect', 'reflect_101', 'replicate' and 'wrap'. Its default value is 'black'
  • logging:(boolean) set this flag to enable/disable error logging essential for debugging. Its default value is False.

from vidgear.gears.stabilizer import Stabilizer
import cv2

stream = cv2.VideoCapture(0) #Open any video stream such as live webcam video stream on first index(i.e. 0) device

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

# infinite loop
while True:
	
	# read frames
	(grabbed, frame) = stream.read()

	# check if frame empty
	if not grabbed:
		#if True break the infinite loop
		break
		
	#send current frame to stabilizer for processing
	stabilized_frame = stab.stabilize(frame)
		
	#hack for stabilizer which is still initializing
	if stabilized_frame is None:
		#wait till initialization is not completed
		continue 

	#do something with stabilized frame here
	   
	# Show output window
	cv2.imshow("Stabilized Frame", stabilized_frame)

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

cv2.destroyAllWindows()
# close output window

stream.release()
# safely close video stream

 

3. Real-Time Stabilization with other VidGear Multi-Threaded Gears:

You can easily use the real-time Video Stabilization with Vidgear's various Gears without any extra effort as follows:

# import libraries
from vidgear.gears import VideoGear
from vidgear.gears import WriteGear
import cv2

stream = VideoGear(source=0, stabilize = True).start() # To open any valid video stream(for e.g device at 0 index)
writer = WriteGear(output_filename = 'Output.mp4') #Define writer

# infinite loop
while True:
	
	frame = stream.read()
	# read stabilized frames

	# check if frame is None
	if frame is None:
		#if True break the infinite loop
		break
	
	# do something with stabilized frame here

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

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

cv2.destroyAllWindows()
# close output window

stream.stop()
# safely close video stream
writer.close()
# safely close writer

Clone this wiki locally