Python Tutorial: N-dimensional images

Learn how to work with images beyond 2D: RGB color images, 3D volumetric data like CT scans, and 4D time-series. Stack 2D slices into 3D volumes using NumPy, or load entire DICOM series with ImageIO's volread function. Understand array shape, sampling rate, and field of view as physical properties of image data.
Chapters
Now that you can load and plot two dimensional images, let's discuss higher dimensional data sets. Images come in all shapes and sizes. This makes them versatile, but also complex. A standard grayscale image is the simplest type. It's an array that can be indexed by rows and columns. 3D images cover a volume of space rather than just a single plane. This volumetric data is useful because it can better capture the complexities of the human body. But it can be difficult to visualize because it can't be summarized in a single plot. Color images are also three dimensional. RGB images, for example, have three color channels that, when rendered by matplotlib or other image viewers, express a wide range of colors. Movies or time series data include a temporal dimension showing how each element changes over time, like the planar dimension. For 3D volumes, the temporal dimension is put first by convention. Just as a 2D image is a stack of one dimensional vectors, 3D, 4D and even higher dimensional images can be thought of as stacks of simpler ones. Let's illustrate this by creating a 3D volume from a few 2D images. First we'll load ImageIO and NumPy. Then we'll read in three slices of a chest CT scan. Each of these slices is an array with 512 row elements by 512 column elements. Now we can feed a list of these three images into NumPy's stack function to create a 3D volume. If we look at our new Vol array, we see that it contains a third dimension with three elements along it, but the row and column dimensions are the same as before. Image IO's Volum Read function is capable of reading volumes directly from disk whether your images are stored in their own folder or if the data set is already multidimensional. In this example, we have a folder named Chest data which contains 50 slices of a 3D volume. We simply have to pass the folder name to volread and it will assemble the volume for us. Since these are DICOM images, the function actually checks the available metadata to make sure that the images are placed in the correct order. Otherwise it will default to alphabetical order. Displaying the shape attribute shows us that we have 50 images stacked together. When analyzing images, keep in mind that the data is only a representation of real physical space. The information in your images is limited to the number of elements in it. This is known as the array shape in numpy and is always available as an attribute. The amount of space covered by each element is the sampling rate and it can vary along each dimension. For DICOM images. The sampling rate is usually encoded in the metadata. For other types of image formats such as JPEG and png, you may need to find it elsewhere. The field of view is the total amount of space covered along each axis. It is the product of the shape and sampling rate. Understanding the difference between these concepts is important, and we'll return to it throughout this course. Now let's start exploring some 3D volumes.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







