Get Started

Stack images

Image "stacks" are a useful metaphor for understanding multi-dimensional data. Each higher dimension is a stack of lower dimensional arrays.

In this exercise, we will use NumPy's stack() function to combine several 2D arrays into a 3D volume. By convention, volumetric data should be stacked along the first dimension: vol[plane, row, col].

Note: performing any operations on an ImageIO Image object will convert it to a numpy.ndarray, stripping its metadata.

This is a part of the course

“Biomedical Image Analysis in Python”

View Course

Exercise instructions

  • Import imageio and numpy (as np).
  • Load "chest-220.dcm", "chest-221.dcm", and "chest-222.dcm".
  • Create a 3D volume using np.stack(). Set the stacking axis to 0.
  • Print the shape attribute of vol.

Hands-on interactive exercise

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

# Import ImageIO and NumPy
____
import ____ as ____

# Read in each 2D image
im1 = imageio.imread('chest-220.dcm')
im2 = ____
im3 = ____

# Stack images into a volume
vol = np.stack(____)
print('Volume dimensions:', ____)

This exercise is part of the course

Biomedical Image Analysis in Python

IntermediateSkill Level
4.6+
12 reviews

Learn the fundamentals of exploring, manipulating, and measuring biomedical image data.

Prepare to conquer the Nth dimension! To begin the course, you'll learn how to load, build and navigate N-dimensional images using a CT image of the human chest. You'll also leverage the useful ImageIO package and hone your NumPy and matplotlib skills.

Exercise 1: Image dataExercise 2: Load imagesExercise 3: MetadataExercise 4: Plot imagesExercise 5: N-dimensional imagesExercise 6: Stack images
Exercise 7: Load volumesExercise 8: Field of viewExercise 9: Advanced plottingExercise 10: Generate subplotsExercise 11: Slice 3D imagesExercise 12: Plot other views

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free