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).
Este exercício faz parte do curso
Biomedical Image Analysis in Python
Instruções do exercício
- 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 to0. - 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.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# Import SciPy's "ndimage" module
____
# Screen out non-bone pixels from "im"
mask_bone = ____
im_bone = np.where(____, ____, ____)
# Get the histogram of bone intensities
hist = ____
# Plot masked image and histogram
fig, axes = plt.subplots(2,1)
axes[0].imshow(im_bone)
axes[1].plot(hist)
format_and_render_plot()