Quaternion¶
Quaternion creation module.
Quaternion can be used to create linear transformation (rigid and affine). They can be used to manually create a transformation and apply it to intensity or labelled images.
- timagetk.algorithms.quaternion.centered_rotation_trsf(image, angle, axis)[source]¶
Rotate an image around centered axis.
- Parameters:
image (timagetk.SpatialImage) – Image to rotate
angle (float) – Angle, in degree, of the rotation around the axis. Should be in the
]-360, 360[
range.axis ({"x", "y", "z"}) – Rotation axis.
- Returns:
Trsf – Rotation of given angle around axis, image centered, to use with
apply_trsf
.
See also
Examples
>>> from timagetk.io import imread >>> from timagetk.io.util import shared_data >>> from timagetk.algorithms.trsf import apply_trsf >>> from timagetk.algorithms.quaternion import centered_rotation_trsf >>> img = imread(shared_data('p58-t0-a0.lsm', 'p58')) >>> r_angle = 10 >>> # Rotate around z-axis by 90° clockwise: >>> trsf_z = centered_rotation_trsf(img, r_angle, 'z') >>> z_rotated_img = apply_trsf(img, trsf_z) >>> # Rotate around x-axis by 90° clockwise: >>> trsf_x = centered_rotation_trsf(img, r_angle, 'x') >>> x_rotated_img = apply_trsf(img, trsf_x) >>> # Display result images using 'contour projections': >>> from timagetk.visu.mplt import grayscale_imshow >>> img_names = ["Original image", "Z-axis {}° rotated image".format(r_angle), "X-axis {}° rotated image".format(r_angle)] >>> grayscale_imshow([img, z_rotated_img, x_rotated_img], suptitle="Axis centered rotation", title=img_names)
- timagetk.algorithms.quaternion.point_rotation_trsf(angle, axis, point, unit='voxel')[source]¶
Return a transformation, as a
Trsf
instance, for rotation around axis and point.- Parameters:
angle (float) –
- Angle, in degree, of the clockwise rotation around the axis.
Should be in the
]-360, 360[
range.
axis ({"x", "y", "z"}) – Rotation axis.
point (list of float) – Translation to apply to axis not selected in
axis
. Should be a list of length-2 values, pay attention to the associatedtrsf_unit
.unit ({'real', 'voxel'}) – The unit of the rotation matrix, important for given
point
units.
- Returns:
Trsf – Rotation of given angle around axis, centered at point, to use with
apply_trsf
.
See also
Examples
>>> from timagetk.io import imread >>> from timagetk.io.util import shared_data >>> from timagetk.algorithms.trsf import apply_trsf >>> from timagetk.algorithms.quaternion import point_rotation_trsf >>> img = imread(shared_data('p58-t0-a0.lsm', 'p58')) >>> x, y, z = img.get_extent()[::-1] >>> xy_center = (x/2., y/2.) >>> xz_center = (x/2., z/2.) >>> r_angle = 10 >>> # Rotate image around centered z-axis: >>> z_rotate_trsf = point_rotation_trsf(r_angle, 'z', xy_center) >>> z_rotated_img = apply_trsf(img, z_rotate_trsf) >>> # Rotate image around centered y-axis: >>> y_rotate_trsf = point_rotation_trsf(r_angle, 'y', xz_center) >>> y_rotated_img = apply_trsf(img, y_rotate_trsf) >>> # Display result images using 'contour projections': >>> from timagetk.visu.mplt import grayscale_imshow >>> img_names = ["Original image", "Z-axis {}° rotated image".format(r_angle), "Y-axis {}° rotated image".format(r_angle)] >>> grayscale_imshow([img, z_rotated_img, y_rotated_img], suptitle="Axis centered rotation", title=img_names)
- timagetk.algorithms.quaternion.quaternion_rotation_matrix(angle, axis)[source]¶
Return quaternion for rotation around axis.
- Parameters:
angle (float) – Angle, in degree, of the rotation around the axis. Should be in the
]-360, 360[
range.axis ({"x", "y", "z"}) – Rotation axis.
- Returns:
numpy.ndarray – Quaternion defining the rotation of given angle around given axis [wiki_rotmat].
References
Examples
>>> from timagetk.io import imread >>> from timagetk.io.util import shared_data >>> from timagetk.algorithms.quaternion import quaternion_rotation_matrix >>> from timagetk import Trsf >>> img = imread(shared_data('p58-t0-a0.lsm', 'p58')) >>> r_angle = 10 >>> quat = quaternion_rotation_matrix(r_angle, "z") >>> from timagetk.algorithms.trsf import apply_trsf, inv_trsf >>> trsf = Trsf(quat) # Trsf objects are in real units by default >>> print(trsf.get_array()) [[ 0.98480775 -0.17364818 0. 0. ] [ 0.17364818 0.98480775 0. 0. ] [ 0. 0. 1. 0. ] [ 0. 0. 0. 1. ]] >>> rotated_img = apply_trsf(img, inv_trsf(trsf)) # invert the transformation to rotate clockwise around the origin >>> # Display result images using 'contour projections': >>> from timagetk.visu.mplt import grayscale_imshow >>> img_names = ["Original", "Rotated by {}°".format(r_angle)] >>> grayscale_imshow([img, rotated_img], title=img_names, suptitle="Clockwise rotation along z-axis at origin")
- timagetk.algorithms.quaternion.quaternion_translation_matrix(tx, ty, tz)[source]¶
Return quaternion for image translation.
- Q = [[1., 0., 0., tx],
[0., 1., 0., ty], [0., 0., 1., tz], [0., 0., 0., 1.]]
- Parameters:
- Returns:
numpy.ndarray – Quaternion defining the translation.
Examples
>>> from timagetk.io import imread >>> from timagetk.io.util import shared_data >>> from timagetk.algorithms.quaternion import quaternion_translation_matrix >>> from timagetk import Trsf >>> img = imread(shared_data('p58-t0-a0.lsm', 'p58')) >>> # Defines a x & y translation by half the extent (*i.e.* real units) >>> xe, ye, ze = img.get_extent() >>> tx=round(xe/2., 3); ty=round(ye/2., 3); tz=0 >>> quat = quaternion_translation_matrix(tx, ty, tz) >>> # Apply this translation: >>> from timagetk.algorithms.trsf import apply_trsf, inv_trsf >>> trsf = Trsf(quat) # Trsf objects are in real units by default >>> print(trsf.get_array()) [[ 1. 0. 0. 29. ] [ 0. 1. 0. 45.973] [ 0. 0. 1. 0. ] [ 0. 0. 0. 1. ]] >>> translated_img = apply_trsf(img, inv_trsf(trsf)) # invert the transformation to "move away" from the origin >>> # Display result images using 'contour projections': >>> from timagetk.visu.mplt import grayscale_imshow >>> img_names = ["Original image", "Translated image"] >>> grayscale_imshow([img, translated_img], suptitle=f"Translated by {tx}µm in X and {ty}µm in Y", title=img_names) >>> # Defines a x & y translation by 100 voxels in X and 50 in Y: >>> quat = quaternion_translation_matrix(tx=100, ty=50, tz=0) >>> # Apply this translation: >>> from timagetk.algorithms.trsf import apply_trsf >>> trsf = Trsf(quat) >>> trsf.set_unit('voxel') # change transformation units from 'real' (default) to 'voxel' >>> print(trsf.get_array()) [[ 1. 0. 0. 100.] [ 0. 1. 0. 50.] [ 0. 0. 1. 0.] [ 0. 0. 0. 1.]] >>> translated_img = apply_trsf(img, inv_trsf(trsf)) # invert the transformation to "move away" from the origin >>> # Display result images using 'contour projections': >>> from timagetk.visu.mplt import grayscale_imshow >>> img_names = ["Original image", "Translated image"] >>> grayscale_imshow([img, translated_img], suptitle="Translated by 100 voxels in X and 50 in Y", title=img_names)
- timagetk.algorithms.quaternion.rotation_trsf(angle, axis, unit='real')[source]¶
Return a transformation, as a
Trsf
instance, for rotation around given axis.- Parameters:
angle (float) – angle, in degree, of the rotation around the axis. Should be in the
]-360, 360[
range.axis ({"x", "y", "z"}) – Rotation axis.
unit ({"real", "voxel"}) – Defines if the transformation is in real unit (default) or voxel unit.
- Returns:
Trsf – Rotation of given angle around axis, to use with
apply_trsf
.
See also
Examples
>>> from timagetk.io import imread >>> from timagetk.io.util import shared_data >>> from timagetk.algorithms.trsf import apply_trsf >>> from timagetk.algorithms.quaternion import rotation_trsf >>> from timagetk.visu.mplt import grayscale_imshow >>> img = imread(shared_data('p58-t0-a0.lsm', 'p58')) >>> # Origin centered z-axis rotation by 10°: >>> trsf_z10 = rotation_trsf(10, "z") >>> rotated_imgz10 = apply_trsf(img, trsf_z10) >>> # Origin centered x-axis rotation by 10°: >>> trsf_x10 = rotation_trsf(10, "x") >>> rotated_imgx10 = apply_trsf(img, trsf_x10) >>> # Display result images using 'contour projections': >>> img_names = ["Original image", "Clockwise z-rotation by 10°", "Clockwise x-rotation by 10°"] >>> grayscale_imshow([img, rotated_imgz10, rotated_imgx10], suptitle="Origin centered rotations", title=img_names)
- timagetk.algorithms.quaternion.translation_trsf(tx, ty, tz, unit='real')[source]¶
Return a transformation, as a
Trsf
instance, for image translation.- Parameters:
- Returns:
trsf – Trsf defining the translation, to use with
apply_trsf
.
Notes
Function
apply_trsf
has a different convention than the one used inquaternion_translation_matrix
, thus the translations are inverted.Examples
>>> from timagetk.io import imread >>> from timagetk.io.util import shared_data >>> from timagetk.algorithms.quaternion import translation_trsf >>> from timagetk.algorithms.trsf import apply_trsf >>> from timagetk.visu.mplt import grayscale_imshow >>> # Defines a -20 voxels x-translation, this will move the image toward the x-axis origin >>> trsf = translation_trsf(tx=-20., ty=0, tz=0, unit='voxel') >>> print(trsf) vtTransformation : { type : AFFINE_3D, unit : voxel, } >>> print(trsf.get_array()) [[ 1. 0. 0. 20.] [ 0. 1. 0. 0.] [ 0. 0. 1. 0.] [ 0. 0. 0. 1.]] >>> img = imread(shared_data('p58-t0-a0.lsm', 'p58')) >>> # Apply this translation using shared data as example: >>> translated_img = apply_trsf(img, trsf) >>> # Display result images using 'contour projections': >>> img_names = ["Original image", "Translated image"] >>> grayscale_imshow([img, translated_img], img_title=img_names) >>> # Defines a y-translation by half the y-extent, this will move the image by away from the y-axis origin >>> xe, ye, ze = img.get_extent() >>> trsf = translation_trsf(tx=0, ty=ye/2., tz=0) >>> print(trsf) vtTransformation : { type : AFFINE_3D, unit : real, } >>> print(trsf.get_array()) [[ 1. 0. 0. 0. ] [ 0. 1. 0. -45.97333385] [ 0. 0. 1. 0. ] [ 0. 0. 0. 1. ]] >>> # Apply this translation using shared data as example: >>> translated_img = apply_trsf(img, trsf) >>> # Display result images using 'contour projections': >>> img_names = ["Original image", "Translated image"] >>> grayscale_imshow([img, translated_img], title=img_names)