Get startedGet started for free

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 edges
  • binary_erosion: Remove pixels along edges
  • binary_opening: Erode then dilate, "opening" areas near edges
  • binary_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

View Course

Exercise instructions

  • Create a bone by selecting pixels from im that are greater than or equal to 145.
  • Use ndi.binary_dilation() to increase the size of mask_bone. Set the number of iterations to 5 to perform the dilation multiple times.
  • Use ndi.binary_closing() to fill in holes in mask_bone. Set the number of iterations to 5 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()
Edit and Run Code