Add image to a live camera feed using OpenCV-Python

In this blog, we will learn how to add an image to a live camera feed using OpenCV-Python. Also known as Image Blending. In this we take the weighted sum of two images. These weights give a feeling of blending or transparency.

Images are added as per the equation below:

Since an image is a matrix so for the above equation to satisfy, both img1 and img2 must be of equal size.

OpenCV has a built-in function that does the exact same thing as shown below

The idea is that first, we will select which image we want to overlay (another image will serve as the background). Then we need to select the region in the background image where we want to put the overlay image. Add this selected region with the overlay image using the above equation. At last change the region in the background image with the result obtained in the previous line.

I hope you understand the idea. Now, let’s get started

Task:

Overlay a white square image on the live webcam feed according to different weights. Instead of manually giving weights, set two keys which on pressing increase or decrease the weights.

Steps:

  • Take an image which you want to overlay. Here, I have used a small white square created using numpy. You can use any.
  • Open the camera using cv2.VideoCapture()
  • Initialize the weights (alpha).
  • Until the camera is opened
    • Read the frame using cap.read()
    • Select the region in the frame where we want to add the image and add the images using cv2.addWeighted()
    • Change the region in the frame with the result obtained
    • Display the current value of weights using cv2.putText()
    • Display the image using cv2.imshow()
    • On pressing ‘a’ increase the value of alpha by 0.1 and decrease by the same amount on pressing ‘d’
    • Press ‘q’ to break

Code:

See the change in transparency by pressing keys ‘a’ and ‘d’. The output looks like this

You might encounter wrong values of alpha being displayed. This is because of Python’s floating point limitations.

Hope you enjoy reading. In the next blog, we will learn how to do the same for the non-rectangular region of interest.

If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.

5 thoughts on “Add image to a live camera feed using OpenCV-Python

  1. Akhil P Jacob

    wow that was useful. But i tried to put my image as background variable. but it didn’t worked for me.
    I am new to the image processing. like below

    background = cv2.imread(‘../dresses/deepak.jpg’)

    Reply
      1. Pablo Quichimbo

        Hola! tengo el mismo problema, no se muestra la imagen y sigue apareciendo el cuadrado inicial

        Reply
  2. Sridhar Ethiraj

    Hi all, i tried but the example, but the problem is while pressing a and d buttons its not working. Video is coming, intensity not changing.

    Reply
  3. Jagdish

    Hey
    Is it possible to make the camera feed transparent while using it and then add images to it accordingly?

    Reply

Leave a Reply