I have ndarray with the shape of (3,794,1255).
I want to create mask based on the 2 first bands of the ndarray, so my output will be one array with x-y coordinates, with 0 for pixels that do not fullfill the 2 conditions and 1 for pixels that do fullfill the conditions.
For example, the conditions can be:
array.shape
>>>(3,794,1255)
array[0]>0.65
0.2<array[1]<0.4
mask = (array[0] > 0.65) & (0.2<array[1]<0.4)
array[mask]
but that returns the error:
IndexError: boolean index did not match indexed array along dimension 0; dimension is 3 but corresponding boolean dimension is 794
My question is how can I create one mask layer based on two first dimensions of ndarray ? is there any better way to do this?
CodePudding user response:
this is what I tend to do:
mask = np.array(condition 1) * np.array(condition 2) np.array(condition n)
using * for 'and', for 'or'
example:
r = np.random.random((3,5,5))
mask = np.array(0.2<r[1]) * np.array(r[1]<0.4) * np.array(r[0] < .65)
r is:
array([[[0.2373137 , 0.75311162, 0.00749418, 0.62770494, 0.6802736 ],
[0.99861914, 0.98375702, 0.48055185, 0.76576586, 0.57430756],
[0.56211162, 0.29463516, 0.96651997, 0.17392071, 0.85070297],
[0.39914012, 0.20810329, 0.18085806, 0.02747008, 0.54901285],
[0.66871882, 0.37093185, 0.14755093, 0.17983568, 0.75469553]],
[[0.81590629, 0.61742905, 0.34190211, 0.73226403, 0.88913768],
[0.74056323, 0.13472895, 0.3629095 , 0.44750391, 0.37093239],
[0.93072263, 0.55193092, 0.93684829, 0.17397018, 0.54124493],
[0.29852027, 0.93821551, 0.46921668, 0.61645803, 0.4749333 ],
[0.94431342, 0.13278848, 0.71384213, 0.33611594, 0.81344182]],
[[0.41933789, 0.654538 , 0.37429377, 0.57694553, 0.43628154],
[0.87547837, 0.45714451, 0.84946798, 0.46364122, 0.0405608 ],
[0.19172952, 0.96078271, 0.78402289, 0.34496085, 0.01560104],
[0.1903755 , 0.66774343, 0.79225036, 0.41254314, 0.79447361],
[0.32102159, 0.55022489, 0.77361031, 0.73757623, 0.73835877]]])
mask is:
array([[False, False, True, False, False],
[False, False, True, False, True],
[False, False, False, False, False],
[ True, False, False, False, False],
[False, False, False, True, False]])