gpt4 book ai didi

python - OpenCV Python-用背景色替换 mask

转载 作者:行者123 更新时间:2023-12-02 17:37:24 26 4
gpt4 key购买 nike

我想从图像中删除蒙版,然后将其替换为背景色。在这种情况下,我想从原始图像中删除克里斯蒂亚诺的脸

这是原始图像
enter image description here

这是面具
enter image description here

这是我现在使用的代码

# load background (could be an image too)
bk = np.full(frame.shape, 255, dtype=np.uint8) # white bk


# blur the mask to help remove noise, then apply the
# mask to the frame
skinMask = cv2.GaussianBlur(skinMask, (3, 3), 0)



#skinMask = cv2.bitwise_not(skinMask)
skin = cv2.bitwise_and(frame, frame, mask = skinMask)

cv2.imshow("mask", skin)

mask = cv2.bitwise_not(skinMask)
bk_masked = cv2.bitwise_and(bk, bk, mask=mask)

# combine masked foreground and masked background
final = cv2.bitwise_or(bk_masked,skin)

最佳答案

我不确定面具的形状是什么,但是交换操作应该很简单。

# Assuming frame is of shape (531, 403, 3)
# And skinMask is of shape (527, 401, 3)
# Which is what it is when you do an cv2.imread on your posted images

# First find all the coordinates you want to swap
faceCoords = np.where(skinMask != [0,0,0]) # 0,0,0 is black

# Then swap the values from your bk image like so
frame[faceCoords[0],faceCoords[1],faceCoords[2]] = bk[faceCoords[0],faceCoords[1],faceCoords[2]]

这将生成一个图像,背景值将替换蒙版值的正部分。

关于python - OpenCV Python-用背景色替换 mask ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49099424/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com