【问题标题】:Add 2 images together based on a mask基于蒙版将 2 个图像加在一起
【发布时间】:2019-01-11 18:36:57
【问题描述】:

假设我有图像 A、B 和 M。目标是将图像 A 作为背景,然后将图像 B 与图像 A 混合,但仅在蒙版 M 的区域中。

因此,如果一个像素位于遮罩的黑色区域,则该像素与 A 中的同一像素相同。但如果一个像素位于遮罩的白色区域,则生成的像素应该是这些像素的混合在 A 和 B 中。

OpenCV 有办法实现这个吗?

【问题讨论】:

  • 从 B 的掩码中,您应该在掩码边界处创建一个渐变并根据 maskBB + (1-maskBA) 进行混合

标签: python opencv image-processing computer-vision


【解决方案1】:

试试这个。您有 2 个 numpy 数组形式的图像(形状 = 宽度、高度、3)。您只会找到那些不是黑色的像素 ([0,0,0]) - 您会得到索引(带有索引的数组)并只弯曲那些。

#bcg - background
#mask - mask
#indice - indexes of pixels that are not black

indice =np.where(np.any(mask!=0, axis=2))
bcg.setflags(write=True) # maybe not needed, but sometimes array is write-blocked
bcg[indice]=mask[indice]+bcg[indice] # or (mask[indice]+bcg[indice])/2 - depends on your bending algorithm

【讨论】:

    猜你喜欢
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 2012-05-15
    相关资源
    最近更新 更多