Get startedGet started for free

Select objects

Labels are like object "handles" - they give you a way to pick up whole sets of pixels at a time. To select a particular object:

  1. Find the label value associated with the object.
  2. Create a mask of matching pixels.

For this exercise, create a labeled array from the provided mask. Then, find the label value for the centrally-located left ventricle, and create a mask for it.

This exercise is part of the course

Biomedical Image Analysis in Python

View Course

Exercise instructions

  • Use ndi.label() to assign labels to each separate object in mask.
  • Find the index value for the left ventricle label by checking the center pixel (128, 128).
  • Create a mask of pixels matching the left ventricle label. Using np.where, set pixels labeled as lv_val to 1 and other values to np.nan.
  • Use plt.imshow() to overlay the selected label on the current plot.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Label the image "mask"
labels, nlabels = ____

# Select left ventricle pixels
lv_val = ____[128, 128]
lv_mask = ____

# Overlay selected label
plt.imshow(____, cmap='rainbow')
plt.show()
Edit and Run Code