liveBook · Manning

Computed tomography

As explained in Deep Learning with PyTorch, Second Edition

Computed tomography imaging data consisting of cross-sectional body slices.

A computed tomography (CT) scan is a 3D image built from cross-sectional slices of the body. Think of it as a stack of grayscale images: each slice shows a different position in space. A CT scan differs from a conventional X-ray, which projects 3D structures onto a 2D plane. section 11.3 section 4.2

Three details matter when working with the data:

  • Each 3D grid cell is a voxel. Its value roughly reflects the density of the matter inside it: air and lung tissue appear dark, while bone appears bright. section 11.3
  • A single scan is usually a 3D array because CT has one intensity channel. With batch and channel dimensions added, volumetric image data has shape N × C × D × H × W. section 4.2
  • Voxel dimensions represent physical distances and need not be equal. Slice spacing matters when relating array positions to anatomy, even if a 2D model does not use the exact thickness directly. section 11.3 section 15.6.1

From scan file to nodule slice

For a lung-nodule task, load MetaIO .mhd and .raw data with SimpleITK, then convert the image to a NumPy voxel array. A candidate’s patient coordinates identify a position in that array; indexing along one axis extracts a 2D slice. A dataset can return that slice as a PyTorch tensor with its label and position. This also makes it possible to feed individual slices to SAM, which cannot directly process the 3D volume. section 12.3 section 15.6.2 section 15.6.1

Listing 15.3 Extracting a single 2D slice from 3D CT scan data
center_irc = xyz2irc(
  center_xyz,           #1
  self.origin_xyz,
  self.vxSize_xyz,
  self.direction_a,
)
center_val = int(round(center_irc[axis]))     #2

if axis == 0:
    ct_slice = self.hu_a[center_val, :, :]    #3
elif axis == 1:
    ct_slice = self.hu_a[:, center_val, :]
elif axis == 2:
    ct_slice = self.hu_a[:, :, center_val]
else:
    raise ValueError("Invalid axis value. Must be 0, 1, or 2.")

return ct_slice, center_irc

The slice-extraction listing converts patient coordinates to array indices, then selects a plane along one of the volume’s three axes. section 15.6.2

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Defines voxels and tensor shape and demonstrates loading and slicing, while leaving reconstruction mathematics and raw-format parsing to other tools.
Examples
Shows stacked head slices, SimpleITK scan loading, and a lung slice with a marked nodule.
What it assumes
Builds on 2D image and tensor representations while defining CT scans and voxels before using them.
What you can do after
Apply it via SimpleITK and PyTorch to load a CT volume and extract labeled 2D slices for a segmentation dataset.
How it connects
Connects volumetric tensors, voxels, patient coordinates, dataset construction, and 2D image segmentation.

Also explained in

Compare how 2 books explain computed tomography →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.