通过 OpenCV 应用分割掩模

Sha*_*ala 3 python opencv numpy image-segmentation

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

我想将其“应用”到另一个np.ndarray图像(3 个通道:RGB),其中遮罩存在的区域稍微突出显示为突出显示的颜色。例如,如果我希望人类面具的区域由绿色表示,我会想要如下所示的内容。我想知道如何使用opencv和 来做到这一点numpy

在此输入图像描述

Qua*_*ang 5

咱们试试吧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)
Run Code Online (Sandbox Code Playgroud)

输出:

在此输入图像描述