In this blog, we will learn how to implement the transition effect in OpenCV.

Let’s Code Transition Effect in OpenCV!

Steps for Transition Effect in OpenCV

  1. Load two images, which will take part in the transition effect.
  2. Image manipulation (If you want a certain effect)
  3. Transition between the two images using addWeighted() OpenCV function.

Transition effect OpenCV python
Source | Divyanshu Shekhar-

Transition Effect Algorithm

Import Packages

For Implementing the Transition effect in OpenCV Python, we will have to import the Computer Vision package.

import cv2 as cv

The 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 the time package as it will help in resting 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:

  1. First Image
  2. Alpha value (Opacity for the first image)
  3. Second Image
  4. Beta Value (Opacity for the second image)
  5. 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 represents 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 the 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 values 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 with 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)
        if cv.waitKey(1) == 27:

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 that will control the alpha value and on that basis, the transition will be applied.

You can change the range of the value of the trackbar in order to get a smoother or fast transition.

Change sleep time as well when you change the range of the trackbar.

Transition Effect OpenCV Documentation

Learn more about the transition and blending of OpenCV functions from official OpenCV Documentation.

About Author
0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Scroll to Top