1. Spatial transformations
Now that you've seen how we can measure a single image let's turn our attention to questions that leverage many of them.
2. OASIS Database
For this chapter, we're going to look at brains from the Open Access Series of Imaging Studies. To describe the effects of aging and dementia on the brain, the researchers gathered 3D MRI images of nearly 400 adults. The participants were between the ages of 18 and 80, and many of them had mild to severe Alzheimer's disease.
3. Significant variability
With a large imaging dataset, there is going to be variability and not just the kind that's interesting. There will be differences in intensity scales, sampling ratios, object orientation, and object placement within the image window.
4. Registration
One way to address this is to register images to a pre-defined position and coordinate system.
For example, you might make all images line up with a template image or atlas. The process of aligning two images together is called "registration."
Registration requires making multiple transformations to an image, such as shifting, rotating, and scaling it.
5. Affine transformations preserve points, lines, and planes
Affine transformations modify an image while preserving all of the points, straight lines, and planes. Shown here are four examples of affine transformations.
Translation is the shifting of an image along an axis. It can be used to center an object, for example.
Rotation, on the other hand, will turn the image along a plane.
Scaling increases or decreases the size of the image, and the shearing operation shifts the ends of an axis away from each other.
6. Translation
Let's see how we can implement some of these transformations in SciPy. Here we have an off-center brain that we want to move to the center of the image.
First, we'll load the head slice using ImageIO. It has a shape of 256 by 256, so the center is at 128, 128.
Then we'll get the head's initial center of mass.
We next calculate the difference between the head's current center of mass and the target center, for both the rows and the columns.
Finally, we call SciPy's shift() function, passing in the image and the number of pixels we need to move along the first and second axes.
7. Rotation
Rotations can be performed in a similar manner using the rotate() function. The angle of rotation is specified in degrees, with positive numbers indicating upward from the horizontal, and negative numbers downward.
In two dimensions, we always rotate along the x-y plane, but in three dimensions, there are three rotational planes we could use.
8. Image rotation
One caveat with the rotate() function is that the default behavior preserves all the original data. This means that your rotated image may actually end up larger than your original.
To keep your original image shape, pass "reshape equals False" to your function call.
9. Transformation matrix
For complex registrations, it can be useful to compute a transformation matrix between the original and target space.
Essentially, the elements of a transformation matrix encode instructions for the operations we have discussed: translation, rotation, scaling, and shearing. We cannot cover methods for calculating these matrices, but let's see how they can be used to simplify the registration process.
10. Applying a transformation matrix
First, we create the transformation matrix. Let's first use the identity matrix, which has ones along the diagonal and zeros off it.
We can apply it by passing the image and matrix to the affine_transform() function.
The resulting image is identical to the original.
Next, let's manipulate the matrix values that encode shifting and rescaling.
When we apply this new matrix and plot the result, you can see that the image has been centered and made larger.
11. Let's practice!
Now, let's practice using affine transformations.