Get startedGet started for free

Objects and labels

1. Objects and Labels

In this chapter, we'll discuss how you can measure one or more component parts of your image. We'll start by learning how to label objects.

2. Segmentation splits an image into parts

Segmentation is the process of splitting an image into separate objects. Since whole careers can be spent developing segmentation techniques, we will focus mostly on how to analyze the resulting objects.

3. Sunnybrook Cardiac Database

For this chapter, we will analyze cardiac magnetic resonance imaging data from the Sunnybrook Cardiac Database. Each Sunnybrook dataset contains a 3D time series of a person's heart over the course of a single heartbeat. The end goal is to measure the proportion of blood that's pumped out of the left ventricle, a measure known as the ejection fraction. In this image, the left ventricle is the circular cavity in the center. Abnormal ejection fractions can indicate urgent health issues.

4. Labeling image components

Our first step towards calculating the ejection fraction is to segment the left ventricle from the rest of the image. For these MRI data, fluid-filled areas have high-intensity values. So, one approach is to take the original image, filter it to reduce noise and smooth edges, then mask it to select pixels with relatively high values. This does a good job of segmenting the left ventricle, but now we need to remove the pixels that are part of other objects.

5. Labeling image components

We can do this using SciPy's label() function. First, we'll create the mask we saw in the last slide by reading in the file, applying a small Gaussian filter, then masking pixels with intensities lower than 150. Next, we "label" the mask. The labeling algorithm treats 0 values as background pixels, and then it looks for all of the objects that are separated by background. It then returns an array where each object has been indexed, as well as the number of objects detected. It seems we have 14 distinct objects in this image. Plotting the labels with the rainbow colormap shows that the circular left ventricle region in the center has been assigned a unique label value.

6. Label selection

You can now select individual objects by referencing their index value. To select pixels in the first object, you would use "where labels is 1, return the value from im, else return 0". Alternatively, you can select a number of labels meeting a condition. Calling "where labels is less than 3, return im, else 0" will select pixels from the first and second objects.

7. Object extraction

A bounding box is the range of indices along each axis which completely enclose an object. You can use the bounding box to extract objects from the larger image. The find_objects() function can create these bounding boxes for you.

8. Object extraction

When you run find_objects() on a labeled array, it will return a list of bounding boxes. Each item in the returned list is a tuple of index ranges - one slice for each dimension. Indexing the original image using one of these boxes will yield an image cropped to that object. You could then analyze each of these arrays individually.

9. Let's practice!

You've now learned the basics of segmentation and labeling. Let's practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.