OpenCV Python-将遮罩替换为背景色
我想从图像中删除遮罩,并用背景色替换它。在本例中,我想从原始图像中删除克里斯蒂亚诺的脸 这是原始图像OpenCV Python-将遮罩替换为背景色,python,opencv,Python,Opencv,我想从图像中删除遮罩,并用背景色替换它。在本例中,我想从原始图像中删除克里斯蒂亚诺的脸 这是原始图像 这是面具 这就是我现在使用的代码 # 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 skin
这是面具
这就是我现在使用的代码
# 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]]
这将生成一幅图像,背景值将替换遮罩值的正部分。能否在图像/遮罩中包含更多关于如何阅读的代码?它们是否可以同时删除背景和遮罩。我想要的只是这件T恤