【发布时间】:2020-06-09 00:04:56
【问题描述】:
我有两个图像和一个掩码,所有尺寸都与 Numpy 数组相同:
期望的输出
我想以这样的方式合并它们,输出将是这样的:
当前代码
def merge(lena, rocket, mask):
'''Mask init and cropping'''
mask = np.zeros(lena.shape[:2], dtype='uint8')
cv2.fillConvexPoly(mask, circle, 255) # might be polygon
'''Bitwise operations'''
lena = cv2.bitwise_or(lena, lena, mask=mask)
mask_inv = cv2.bitwise_not(mask) # mask inverting
rocket = cv2.bitwise_or(rocket, rocket, mask=mask_inv)
output = cv2.bitwise_or(rocket, lena)
return output
当前结果
这段代码给了我这个结果:
应用cv2.GaussianBlur(mask, (51,51), 0) 会以不同方式扭曲叠加图像的颜色。
其他 SO 问题与类似问题有关,但不能完全解决这种类型的模糊合成。
更新:这给出了与当前结果相同的结果
mask = np.zeros(lena.shape[:2], dtype='uint8')
mask = cv2.GaussianBlur(mask, (51,51), 0)
mask = mask[..., np.newaxis]
cv2.fillConvexPoly(mask, circle, 1)
output = mask * lena + (1 - mask) * rocket
临时解
由于转化次数较多,这可能不是最优的,请告知
mask = np.zeros(generated.shape[:2])
polygon = np.array(polygon, np.int32) # 2d array of x,y coords
cv2.fillConvexPoly(mask, polygon, 1)
mask = cv2.GaussianBlur(mask, (51, 51), 0)
mask = mask.astype('float32')
mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
foreground = cv2.multiply(lena, mask, dtype=cv2.CV_8U)
background = cv2.multiply(rocket, (1 - mask), dtype=cv2.CV_8U)
output = cv2.add(foreground, background)
请告知如何模糊蒙版,将其与前景正确合并,然后叠加在背景图像上?
【问题讨论】:
-
您在寻找什么样的混合物?乘法?添加剂?
alpha * x + (1-alpha) * y?在您选择的图像编辑软件中尝试几个(Photoshop/GIMP/Paint.NET/...)。 -
@MateenUlhaq 只需在背景顶部应用模糊前景就足够了,所有这些都是为了达到帖子中所示的所需输出,感谢您的澄清
-
@MateenUlhaq 我想这与 RGB 通道有关,但不确定;我在某处读到它可以使用
PIL.Image.composite(lena, rocket, mask)来实现,但还不知道如何正确地来回转换数组 -
你试过我提到的简单的alpha混合方法吗?
output = mask * lena + (255 - mask) * rocket,其中mask已根据需要进行模糊处理。 -
@MateenUlhaq 是的,它给了
ValueError: operands could not be broadcast together with shape (225, 400) with (225, 400, 3),可能是因为掩码
标签: python numpy opencv python-imaging-library bitwise-or