Exercise

Apply a mask

Although masks are binary, they can be applied to images to filter out pixels where the mask is False.

NumPy's where() function is a flexible way of applying masks. It takes three arguments:

np.where(condition, x, y)

condition, x and y can be either arrays or single values. This allows you to pass through original image values while setting masked values to 0.

Let's practice applying masks by selecting the bone-like pixels from the hand x-ray (im).

Instructions

100 XP
  • Create a Boolean bone mask by selecting pixels greater than or equal to 145.
  • Apply the mask to your image using np.where(). Values not in the mask should be set to 0.
  • Create a histogram of the masked image. Use the following arguments to select only non-zero pixels: min=1, max=255, bins=255.
  • Plot the masked image and the histogram. This has been done for you.