On Thu, Dec 2, 2010 at 11:14 AM, Zachary Pincus <zachary.pin...@yale.edu> wrote: >> mask = numpy.zeros(medical_image.shape, dtype="uint16") >> mask[ numpy.logical_and( medical_image >= lower, medical_image <= >> upper)] = 255 >> >> Where lower and upper are the threshold bounds. Here I' m marking the >> array positions where medical_image is between the threshold bounds >> with 255, where isn' t with 0. The question is: Is there a better >> way to do that? > > This will give you a True/False boolean mask: > mask = numpy.logical_and( medical_image >= lower, medical_image <= > upper) > > And this a 0/255 mask: > mask = 255*numpy.logical_and( medical_image >= lower, medical_image <= > upper) > > You can make the code a bit more terse/idiomatic by using the bitwise > operators, which do logical operations on boolean arrays: > mask = 255*((medical_image >= lower) & (medical_image <= upper)) > > Though this is a bit annoying as the bitwise ops (& | ^ ~) have higher > precedence than the comparison ops (< <= > >=), so you need to > parenthesize carefully, as above. > > Zach
Thanks, Zach! I stayed with the last one. _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion