Python 通过OpenCV应用分段遮罩

Python 通过OpenCV应用分段遮罩,python,numpy,opencv,image-segmentation,Python,Numpy,Opencv,Image Segmentation,我目前有一个np.ndarray位掩码,其格式为掩码值为1的像素和没有掩码的像素值为0 我想将此应用于另一幅np.ndarray图像(3个通道:RGB),其中遮罩所在的区域将稍微高亮显示为高亮显示的颜色。例如,如果我想让人类面具的区域用绿色表示,我想要如下所示的东西。我想知道如何使用opencv和numpy实现这一点 让我们试试cv2.addWeighted: # sample data img = np.full((10,10,3), 128, np.uint8) # sample mask

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

我想将此应用于另一幅
np.ndarray
图像(3个通道:RGB),其中遮罩所在的区域将稍微高亮显示为高亮显示的颜色。例如,如果我想让人类面具的区域用绿色表示,我想要如下所示的东西。我想知道如何使用
opencv
numpy
实现这一点


让我们试试
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)
输出:


让我们试试
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)
输出:


这项工作非常好,而且非常直观!这项工作相当好,也相当直观!