2017-12-18 286 views
0

我想制作一个具有与图像中某些单元格对应的蒙版。这些单元格的RGB颜色值中的至少一个应该大于阈值。这里是我的代码无法正常工作:NumPy在哪里获取单元格的最大(R,G,B)>阈值的条件

B = image[0:h,0:w,0].astype(int) 
G = image[0:h,0:w,1].astype(int) 
R = image[0:h,0:w,2].astype(int) 
mask = np.zeros((h,w)) 

mask[np.where(max(R,G,B) > threshold)] = 1 

这给出了一个错误:

ValueError occurred Message=The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

回答

2

由于您的图像是三维阵列(h, w, 3),你可以通过简单地取最大值最后的得到max(R, G, B)轴:

np.max(image, axis=-1) 

比较threshold返回值,你会得到一个bool阵列。将其转换为int以获得零和一个蒙版:

mask = (np.max(image, axis=-1) > threshold).astype(int) 
+0

它工作!谢谢:D –

+0

@AhmedMaher https://meta.stackexchange.com/help/someone-answers –