Affine transforms using clesperanto#

This notebook demonstrates how to apply affine transforms to 3D images.

import pyclesperanto_prototype as cle

cle.select_device('TX')
<NVIDIA GeForce GTX 1650 with Max-Q Design on Platform: NVIDIA CUDA (1 refs)>
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from skimage.io import imread
# Laod example data
np_array = imread('../../data/Haase_MRT_tfl3d1.tif')
np_array.shape
(192, 256, 256)
# push it to GPU memory
input_image = cle.push_zyx(np_array)

cle.imshow(input_image)
../_images/281e43ff45adbeef88ee10bfd05ebeb81f145c67d81c4a8e48939ea501006835.png

Rotation#

For rotating an image, you need to provide angles corresponding to axes.

rotated = cle.rotate(input_image, angle_around_z_in_degrees=45)
cle.imshow(rotated)
../_images/119e50d78e215eda2bc974f4a5a42d203ae3e21e1ed683d75f9678659d029c17.png

Images are rotated around their center by default. You can change this by providing an additional parameter. The image will then be rotated around the origin.

rotated = cle.rotate(input_image, angle_around_z_in_degrees=15, rotate_around_center=False)
cle.imshow(rotated)
../_images/3791ad32a5b98b994cde602e851ce872b1d3b109313a83c8b314b24d1f41ae56.png

Translation#

Images can be translate by providing translation distances along axes:

translated = cle.translate(input_image, translate_x=50, translate_y=-50)
cle.imshow(translated)
../_images/78833893e8baf0bdd071981d85eaefa4453bfe02a3c630a0538db90e5b8d28ff.png

Scaling#

You can scale the image by providing scaling factors.

scaled = cle.scale(input_image, factor_x=0.5, factor_y=2)
cle.imshow(scaled)
../_images/c79d880c574fbf1179e783693fc82fcfc3b9591fe4702c3dc7dacacb83e9913e.png

In this context, the auto_size parameter might be useful:

scaled_auto_size = cle.scale(input_image, factor_x=0.5, factor_y=2, auto_size=True)
cle.imshow(scaled_auto_size)
../_images/a598f11f91d0c966c5afe2ca9ba429969964148876319206692b60db6fc33973.png

Rigid transform#

Rigid transforms allow to do translations and rotations in one shot

rigid_transformed = cle.rigid_transform(input_image, translate_x=50, angle_around_z_in_degrees=45)
cle.imshow(rigid_transformed)
../_images/32cb5d2696f9ba0ce95cc0e669330529cf2f1faf97bbe0c37583a2ac57911ea1.png

Affine transforms#

To do translation, rotation, scaling and shearing in one shot, use affine transforms.

To setup an affine transform, you can do this using a 4x4 transform matrix:

transform_matrix = np.asarray([
    [1, 0, 0, 50],
    [0, 2, 0, 0],
    [0, 0, 0.5, 0],
    [0, 0, 0, 1]
])
transformed_image = cle.affine_transform(input_image, transform=transform_matrix)
cle.imshow(transformed_image)
../_images/e1b51b7631cf6154c54ca8f5e6f8390b44a694f4798c67a7765194fdd11ff93e.png

Alternatively, you can configure a transform object and pass it:

transform = cle.AffineTransform3D()
transform.translate(50)
transform.scale(1, 2, 0.5)

transformed_image = cle.affine_transform(input_image, transform=transform)
cle.imshow(transformed_image)
../_images/e1b51b7631cf6154c54ca8f5e6f8390b44a694f4798c67a7765194fdd11ff93e.png

Shearing#

Providing the shear angle will shear the image in the desired plane

The shear is calculated using the shear angle by the following equation:

1.0/ tan(shear_angle_in_degrees * pi / 180)

#For example, to shear the image in the Y axis along YZ plane using a shear angle of 30 degrees

shear_angle = 30.0

transform = cle.AffineTransform3D()
shear_transform= transform.shear_in_z_plane(angle_y_in_degrees=shear_angle)

transformed_image = cle.affine_transform(input_image, transform=shear_transform)

##display images in each plane
print("yz")
cle.imshow(cle.maximum_x_projection(transformed_image))
print("xz")
cle.imshow(cle.maximum_y_projection(transformed_image))
print("xy")
cle.imshow(cle.maximum_z_projection(transformed_image))
yz
../_images/4523d6990ba754f2d5ca5a32de06d4d13e711df4a453cc210d2ce93bdfe71f7d.png
xz
../_images/00653f97f42feb8ebadff81de25ef94fb184e8285280c07ceb5a525d777a524b.png
xy
../_images/403ea6862c2336bbc1e3f1f296c051748ee6a028973b21dfe8e1b06fa877dcff.png

Linear interpolation versus nearest neighbor interpolation#

Let’s crop the nose and transform it using different interpolation modes.

crop = input_image[50,125:150,45:70]

cle.imshow(crop)
../_images/3b4231a7c79b129f5c0ba17b61b4f264c8da1d448f01d1feb2e4b68e82a11aeb.png

Nearest neighbor interpolation#

# create a larger image
rescaled = cle.create(np.asarray(crop.shape) * 10)

# fill it with a scaled version of the image; 
cle.scale(crop, rescaled, factor_x=10, factor_y=10, factor_z=10, linear_interpolation=False)
cle.imshow(rescaled)
../_images/20fb59a4995d29c6a0ebe6e2f230b94fe52c18e90a1b0eaf9ecd9782498a37d9.png

Linear interpolation#

# fill it with a scaled version of the image; 
cle.scale(crop, rescaled, factor_x=10, factor_y=10, factor_z=10, linear_interpolation=True)
cle.imshow(rescaled)
../_images/5e39ac2af19022da0e0039f54d9a76e05a51b15c317bddde82001dc4cf1f86c0.png