Routines to work with spaces
A space is defined by coordinate axes.
A voxel space can be expressed by a shape implying an array, where the axes are the axes of the array.
A mapped voxel space (mapped voxels) is either:
slice2volume(index, axis[, shape]) | Affine expressing selection of a single slice from 3D volume |
vox2out_vox(mapped_voxels[, voxel_sizes]) | output-aligned shape, affine for input implied by mapped_voxels |
Affine expressing selection of a single slice from 3D volume
Imagine we have taken a slice from an image data array, s = data[:, :, index]. This function returns the affine to map the array coordinates of s to the array coordinates of data.
This can be useful for resampling a single slice from a volume. For example, to resample slice k in the space of img1 from the matching spatial voxel values in img2, you might do something like:
slice_shape = img1.shape[:2]
slice_aff = slice2volume(k, 2)
whole_aff = np.linalg.inv(img2.affine).dot(img1.affine.dot(slice_aff))
and then use whole_aff in scipy.ndimage.affine_transform:
rzs, trans = to_matvec(whole_aff) data = img2.get_data() new_slice = scipy.ndimage.affine_transform(data, rzs, trans, slice_shape)
Parameters: | index : int
axis : {0, 1, 2}
|
---|---|
Returns: | slice_aff : shape (4, 3) affine
|
output-aligned shape, affine for input implied by mapped_voxels
The input (voxel) space, and the affine mapping to output space, are given in mapped_voxels.
The output space is implied by the affine, we don’t need to know what that is, we just return something with the same (implied) output space.
Our job is to work out another voxel space where the voxel array axes and the output axes are aligned (top left 3 x 3 of affine is diagonal with all positive entries) and which contains all the voxels of the implied input image at their correct output space positions, once resampled into the output voxel space.
Parameters: | mapped_voxels : object or length 2 sequence
voxel_sizes : None or sequence
|
---|---|
Returns: | output_shape : sequence
output_affine : (4, 4) array
|