Tune a mask
Imperfect masks can be tuned through the addition and subtraction of pixels. SciPy includes several useful methods for accomplishing these ends. These include:
binary_dilation
: Add pixels along edgesbinary_erosion
: Remove pixels along edgesbinary_opening
: Erode then dilate, "opening" areas near edgesbinary_closing
: Dilate then erode, "filling in" holes
For this exercise, create a bone mask then tune it to include additional pixels.
For the remaining exercises, we have run the following import for you:
import scipy.ndimage as ndi
This exercise is part of the course
Biomedical Image Analysis in Python
Exercise instructions
- Create a bone by selecting pixels from
im
that are greater than or equal to145
. - Use
ndi.binary_dilation()
to increase the size ofmask_bone
. Set the number ofiterations
to5
to perform the dilation multiple times. - Use
ndi.binary_closing()
to fill in holes inmask_bone
. Set the number ofiterations
to5
to holes up to 10 pixels wide. - Plot the original and tuned masks.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create and tune bone mask
mask_bone = ____
mask_dilate = ndi.binary_dilation(____, ____)
mask_closed = ____
# Plot masked images
fig, axes = plt.subplots(1,3)
axes[0].imshow(mask_bone)
axes[1].imshow(mask_dilate)
____
format_and_render_plot()