In this blog we will learn how to implement transition effect in OpenCV.
Let’s Code Transition Effect in OpenCV!
Steps for Transition Effect in OpenCV
- Load two images, which will take part in the transition effect.
- Image manipulation (If you want certain effect)
- Transition between the two images using addWeighted() OpenCV function.
Transition Effect Algorithm
Import Packages
For Implementing Transition effect in OpenCV Python, we will have to import Computer Vision package.
import cv2 as cv
Second package is NumPy for working with image matrix and creation of nD arrays.
For Transition Effect, NumPy is very important as it is used in the creation of the rotation matrix and also athematic and logical operations on image matrices.
import numpy as np
We also need to import time package as it will help in resting of the transition effect.
import time
Load Images
Read images in OpenCV using imread() function. Read two images that will take part in the transition effect.
img1 = cv.imread(‘./img1.jpg’)
img2 = cv.imread(‘./img2.jpg’)
Blending Logic
There is a function in OpenCV that helps blend two images on basis of percentage.
addWeighted() function in OpenCV takes five parameters:
- First Image
- Alpha value (Opacity for the first image)
- Second Image
- Beta Value (Opacity for the second image)
- Gamma Value (Weight for every pixel after blend, 0 for normal output)
The alpha value represents the opacity value of the first image.
The beta value represent the opacity value of the second image.
The gamma value represents the weight added to every pixel after the blend.
Maths behind the function
We have to make the percentage such that it follows the rule of:
alpha + beta = 1
If we choose alpha value as 0.7 I.e 70%. The beta value then should be 0.3 I.e 30%.
0.7 + 0.3 = 1.0
Creating Transition Effect
np.linspace() is a NumPy function for generating linearly spaced numbers between two numbers.
np.linspace(0,10,5) – This function will generate 5 numbers between 1 – 10 and all will be evenly spaced.
We will use linspace function in the loop to generate different values for alpha and beta for the opacity of the images.
for i in np.linspace(0,1,100):
alpha = i
beta = 1 – alpha
output = cv.addWeighted(img1,alpha,img2,beta,0)
Alpha is assigned the value of ‘i’ that will change alpha’s value in every iteration.
The beta value will also change with each iteration as beta depends on the value of alpha. Beta = 1 – alpha.
But, the alpha and beta value will always sum to 1.
The addWeighted() function then takes the two images, alpha, beta and gamma values to generate a new blend image.
This process continues till the loop ends or we forcefully end the process by an ‘ESC‘ keypress.
Simple Transition Effect Source code
import cv2 as cv
import numpy as np
import time
while True:
img1 = cv.imread('./img1.jpg')
img2 = cv.imread('./img2.jpg')
for i in np.linspace(0,1,100):
alpha = i
beta = 1 - alpha
output = cv.addWeighted(img1,alpha,img2,beta,0)
cv.imshow('Transition Effect ',output)
time.sleep(0.02)
if cv.waitKey(1) == 27:
break
cv.destroyAllWindow()
Create Trackbar for Transition Effect in OpenCV
Earlier we were dependent on the loop to see the transition effect.
We can also create a trackbar in OpenCV which will control the alpha value and on that basis, the transition will be applied.
You can change the range of the value of trackbar in order to get more smoother or fast transition.
Change sleep time as well when you change range of the trackbar.
Transition Effect OpenCV Documentation
Learn more about the transition and blending OpenCV functions from official OpenCV Documentation.
How to use np.linspace() ?
np.linspace() is a NumPy function for generating linearly spaced numbers between two numbers.
np.linspace(0,10,5) – This function will generate 5 numbers between 1 – 10 and all will be evenly spaced.
How to use addWeighted() in OpenCV?
The addWeighted() function takes the two images, alpha, beta and gamma values to generate a new blend image.
Example:-
output = cv.addWeighted(img1,alpha,img2,beta,0)
2 Responses
👍👍👍
Good👍