0
点赞
收藏
分享

微信扫一扫

已知alpha通道对原图进行抠图和新背景的合成,基于python


本人自己设计过不少已知alpha通道进行原图抠图的方法,但是都不如人意,今天偶遇一篇文章描述这种问题的解决方法,以此记录下来,非常感谢该作者,

我记录该代码本意是给自己的学习做个笔记,侵删

import cv2
import numpy as np

img = cv2.imread("1.jpg")
mask = cv2.imread("2.jpg", 0) # 读取灰度图像

height, width, channel = img.shape

b, g, r = cv2.split(img)

# -----------------1.获取透明的前景图像-----------
dstt = np.zeros((4, height, width), dtype=img.dtype)

dstt[0][0:height, 0:width] = b
dstt[1][0:height, 0:width] = g
dstt[2][0:height, 0:width] = r
dstt[3][0:height, 0:width] = mask
cv2.imwrite("fore.png", cv2.merge(dstt))

# -----------------2.与新背景图像合成-----------
bg = np.zeros((3, height, width), dtype=img.dtype) # 生成背景图像
bg[2][0:height, 0:width] = 255 # 背景图像采用红色

dstt = np.zeros((3, height, width), dtype=img.dtype)

for i in range(3):
dstt[i][:, :] = bg[i][:, :] * (255.0 - mask) / 255
dstt[i][:, :] += np.array(img[:, :, i] * (mask / 255), dtype=np.uint8)
cv2.imwrite("merge.png", cv2.merge(dstt))

已知alpha通道对原图进行抠图和新背景的合成,基于python_人工智能


已知alpha通道对原图进行抠图和新背景的合成,基于python_人工智能_02


举报

相关推荐

0 条评论