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
1 2 |
output = cv2.addWeighted(img1, weight_img1, img2, weight_img2, gamma) # gamma is the scalar added to each sum |
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
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import cv2 import numpy as np # create an overlay image. You can use any image foreground = np.ones((100,100,3),dtype='uint8')*255 # Open the camera cap = cv2.VideoCapture(0) # Set initial value of weights alpha = 0.4 while True: # read the background ret, background = cap.read() background = cv2.flip(background,1) # Select the region in the background where we want to add the image and add the images using cv2.addWeighted() added_image = cv2.addWeighted(background[150:250,150:250,:],alpha,foreground[0:100,0:100,:],1-alpha,0) # Change the region with the result background[150:250,150:250] = added_image # For displaying current value of alpha(weights) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(background,'alpha:{}'.format(alpha),(10,30), font, 1,(255,255,255),2,cv2.LINE_AA) cv2.imshow('a',background) k = cv2.waitKey(10) # Press q to break if k == ord('q'): break # press a to increase alpha by 0.1 if k == ord('a'): alpha +=0.1 if alpha >=1.0: alpha = 1.0 # press d to decrease alpha by 0.1 elif k== ord('d'): alpha -= 0.1 if alpha <=0.0: alpha = 0.0 # Release the camera and destroy all windows cap.release() cv2.destroyAllWindows() |
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.