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

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

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

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))

1.jpg
2.jpg

发布了14 篇原创文章 · 获赞 7 · 访问量 595

猜你喜欢

转载自blog.csdn.net/my_name_is_learn/article/details/103869155
今日推荐