Prerequisite: Addition and Blending of images using OpenCV in Python
In this article, we will be showing you how to effectively blend two videos together. But for that, we will first see what is Alpha Blending.
Alpha blending is a curved mix of two tones taking into consideration transparency impacts in computer illustrations. The estimation of alpha in the color code goes from 0.0 to 1.0, where 0.0 addresses a completely transparent tone, and 1.0 addresses a completely dark tone. The estimation of the subsequent color when color Value1 with an alpha value of Alpha is drawn over a foundation of color Value0 is given by:
Value = Value0(1.0 - Alpha) + Value1(Alpha)
The alpha part might be utilized to mix to red, green and blue segments similarly, as in 32-bit RGBA, or, then again, there might be three alpha qualities determined relating to every one of the essential tones for spectral tone shifting.
Now, the second thing we will be using is OpenCV which is a library of programming functions that focuses on real-time computer vision. But, before moving forward we will keep a note of a few things.
In order for our program to work perfectly, you’ve to make sure:
- The resolution and frame rate of the two input videos must be exactly same (in code it’s using 1920×1080 format so use that only, otherwise you’ve to set the ‘h’, ‘w’ values accordingly with a resolution of your input videos which we will see later in the code)
- The code is written, taking into consideration that the background video’s duration is slightly greater than or equal to the foreground video’s duration, so it’s preferable for you as well-to-do the same with your background and foreground input videos otherwise you’ve to assign the “ret” value to the background instead of foreground which we will see later in the code
- Rename your video files with some unique name because sometimes it might cause an error if there are two video files of the same name and the path of the two input video files must be provided accurately as well.
- The foreground video must be a combination of some solid color background (preferably black color) and with some movement of the subject on it.
We will be taking two videos as input, one will be our background video and the second one will be our foreground video. The main work is to remove the solid color background of this foreground video so that we’re left with the subject only (of foreground video) with its transparent background. After making this change, the foreground video will then be put on the background video, which will give an illusion of a single video with some effect on it but actually, there will be two videos blended together. This magic is done with the help of OpenCV and Python.
- Import required modules
- Add paths and capture both the input videos.
- Iterate through each frame of both the videos and blend them together using Alpha Blending.
- Play the output video generated.
Notice that the foreground video contains only the particles with a solid black background, thus for better results, it is preferable to take foreground videos and background videos like these.
Notice that in this output video we can see that the sand is moving in the air in a hot desert area, which in reality is the blended output of the particle video(used as foreground) and desert video(used as background). Also, you can always press “q” from your keyboard to break the loop and quit the program.
So in this way, you can blend two videos and if done correctly, this method can also provide a professional edit look as well.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course