python使用opencv显示图片不被覆盖_使用openCV将透明图像覆盖到另一个imag上

下面的代码将使用覆盖图像的alpha通道将其正确地混合到背景图像中,使用x和y设置覆盖图像的左上角。import cv2

import numpy as np

def overlay_transparent(background, overlay, x, y):

background_width = background.shape[1]

background_height = background.shape[0]

if x >= background_width or y >= background_height:

return background

h, w = overlay.shape[0], overlay.shape[1]

if x + w > background_width:

w = background_width - x

overlay = overlay[:, :w]

if y + h > background_height:

h = background_height - y

overlay = overlay[:h]

if overlay.shape[2] < 4:

overlay = np.concatenate(

[

overlay,

np.ones((overlay.shape[0], overlay.shape[1], 1), dtype = overlay.dtype) * 255

],

axis = 2,

)

overlay_image = overlay[..., :3]

mask = overlay[..., 3:] / 255.0

background[y:y+h, x:x+w] = (1.0 - mask) * background[y:y+h, x:x+w] + mask * overlay_image

return background

这段代码将改变背景,因此如果您希望保留原始背景图像,请创建一个副本。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值