2017-12-18 265 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 –