通过OpenCV应用分割掩模。

3

我目前有一个np.ndarray掩码,格式为掩码值为1的像素和没有掩码的像素值为0。

我想要将其应用于另一个np.ndarray图像(3个通道:RGB),其中存在掩码的区域会变得更加突出并呈现高亮颜色。例如,如果我想要人脸掩码的区域以绿色表示,我希望如下所示。我想知道如何使用opencvnumpy实现这一点。

enter image description here

1个回答

4

让我们尝试 cv2.addWeighted

# sample data
img = np.full((10,10,3), 128, np.uint8)

# sample mask
mask = np.zeros((10,10), np.uint8)
mask[3:6, 3:6] = 1

# color to fill
color = np.array([0,255,0], dtype='uint8')

# equal color where mask, else image
# this would paint your object silhouette entirely with `color`
masked_img = np.where(mask[...,None], color, img)

# use `addWeighted` to blend the two images
# the object will be tinted toward `color`
out = cv2.addWeighted(img, 0.8, masked_img, 0.2,0)

输出:

在这里输入图片描述


这个工作效果相当好,而且也非常直观! - SDG

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接