如何在网络摄像头feed上覆盖png?

问题描述 投票:1回答:3

我有一个形状为(480,640,4)的png图像和形状为(480,640,3)的网络摄像头框架。我想将png完全覆盖在网络摄像头Feed上,但是出现以下错误:

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

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'

是由于渠道差异而引起的问题。有人可以帮我解决这个问题吗?预先感谢!

python opencv image-processing overlay
3个回答
0
投票

这是因为您的图像具有不同数量的通道(一个是4通道,另一个是3通道)。如果查看有关addWeighted()的文档。它说到第一个源图像(src1)和第二个源图像(src2):

src1:第一个输入数组。

src2:第二个输入数组,其大小和通道号与src1相同。


0
投票

这是因为图像的尺寸不同。您必须将PNG图像从BGRA转换为BGR。然后错误应该消失了。

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

0
投票

如果使用addWeighted(),您将在背景图像的所有位置上获得相同比例的重叠图像。这似乎不太可能是您想要的,因为您的输入图像具有4通道,其中一个可能是alpha通道。因此,通常您希望背景图像根据alpha通道的显示方式有所不同。

将其作为形状为(500,300,3)的背景(网络摄像头)图像:

enter image description here

这是具有透明度和形状(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

© www.soinside.com 2019 - 2024. All rights reserved.