Thu Dec 2 08:06:02 CST 2010
On Thu, Dec 2, 2010 at 11:14 AM, Zachary Pincus <email@example.com> 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 <=
> And this a 0/255 mask:
> mask = 255*numpy.logical_and( medical_image >= lower, medical_image <=
> 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.
Thanks, Zach! I stayed with the last one.
More information about the NumPy-Discussion