【发布时间】:2018-08-22 04:33:59
【问题描述】:
问题比较简单。
我有一张图片,并且我知道图片的背景颜色。鉴于我想创建图像的前景蒙版。
我试过这个:
background_color = np.array([255, 255, 255], dtype=np.uint8)
foreground = image != background_color
我期望的是一个布尔矩阵,其形状为 HxWx1,颜色匹配时为 True,颜色不匹配时为 False。
但是此操作不是比较颜色,而是比较所有三个颜色分量,我得到 HxWx3 大小的矩阵,其中颜色分量匹配时为 True,不匹配时为 False。
我用 for 循环创建了一个临时解决方案:
foreground = np.zeros(shape=(height, width, 1), dtype=np.bool)
for y in range(height):
for x in range(width):
if not np.array_equal(image[y, x], background_color):
foreground[y, x] = True
但是,当然,这工作缓慢。我的问题如下:在 numpy 或 OpenCV 方法的帮助下,是否有合适的方法进行此类比较?
【问题讨论】:
-
CV - Extract differences between two images: stackoverflow.com/questions/27035672/…
标签: python numpy opencv image-processing