代码之家  ›  专栏  ›  技术社区  ›  SDG

通过OpenCV应用遮罩

  •  0
  • SDG  · 技术社区  · 3 年前

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

    我想把这个“应用”到另一个 np.ndarray 图像(3个通道:RGB),其中存在遮罩的区域会稍微突出显示为突出显示的颜色。例如,如果我想用绿色表示人体面具的区域,我会想要下图所示的东西。我想知道我该怎么做 opencv numpy .

    enter image description here

    1 回复  |  直到 3 年前
        1
  •  4
  •   Quang Hoang    3 年前

    我们试试吧 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)
    

    输出:

    enter image description here