[英]numpy: Setting values to zero based on multiple conditions
我有一个RGB图像,我试图使用这样的东西为它执行一个简单的阈值:
from skimage import filter
def threshold(image):
r = image[:, :, 0]
g = image[:, :, 1]
b = image[:, :, 2]
rt = filter.threshold_otsu(r)
gt = filter.threshold_otsu(g)
bt = filter.threshold_otsu(b)
我想做的是现在制作二进制掩码,其中原始图像中小于这些阈值的RGB值应设置为0。
mask = np.ones(r.shape)
我无法弄清楚怎么做的是如何将掩码索引(x,y)设置为零
image[x, y, 0] < rt and image[x, y, 1] < gt and image [x, y, 2] < bt
不知何故,我需要从这个符合此标准的原始图像中获取(x,y)像素索引,但我不知道如何做到这一点。
NumPy的公司&
进行bit-wise and
。 应用于数组时,按bit-wise and
元素方式应用。 由于比较,例如r < rt
,返回布尔数组,按bit-wise and
的结果bit-wise and
此处的logical and
结果相同。 需要括号,因为NumPy的公司&
具有更高的优先级比<
。
mask = (r < rt) & (g < gt) & (b < bt)
image[mask] = 0
小心 - OpenCV使用“BGR”,而不是RGB。 所以使用
r = image[:, :, 0]
产生蓝色通道,而不是红色通道!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.