Search code examples
pythonopencvimage-processingoverlay

How to overlay a png on a webcam feed?


I have a png image of shape(480,640,4) and webcam frame of shape(480,640,3). I would like to overlay the png entirely over the webcam feed but I'm getting the following error:

added_image = cv2.addWeighted(frame,0.4,png,0.1,0)

The error:

cv2.error: OpenCV(4.2.0) /io/opencv/modules/core/src/arithm.cpp:669: error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function 'arithm_op'

Is the issue because of the channel difference. Can someone help me solve this issue? Thanks in advance!


Solution

  • If you use addWeighted() you will get the same constant fraction of your overlay image everywhere on your background image. It seems unlikely that is what you want because your input image has 4-channels, of which one is presumably the alpha channel. So normally you would expect the background image to show through differently according to the alpha channel.

    Taking this as the background (webcam) image with shape (500,300,3):

    enter image description here

    And this as the overlay image with transparency and shape (500,300,4):

    enter image description here

    import cv2
    import numpy as np
    
    # Read background and overlay images
    bg = cv2.imread('buckinghampalace.jpg')
    ol = cv2.imread('overlay.png', cv2.IMREAD_UNCHANGED) 
    
    # Make a result image that is either the background image or the overlay image depending on the alpha channel being < 128
    res = np.where((ol[...,3]<128)[...,None], bg, ol[...,0:3])
    
    cv2.imwrite('result.jpg', res)
    

    enter image description here