Scikit Image - Euclidean Transform



The Euclidean transformation, also known as a rigid transformation, is a specific type of transformation that includes rotation and translation. This transformation preserves the distances between points in a Euclidean space and is often used in image processing and computer vision.

Following are the equations that describe the transformation for a point (x, y) in 2D space:

X = a0 * x - b0 * y + a1 
Y = b0 * x + a0 * y + b1

where the homogeneous transformation matrix is −

[[a0  b0  a1]
 [b0  a0  b1]
 [0   0    1]]

Pythons Scikit-image library provides the EuclideanTransform class in the transform module to apply Euclidean transformation.

The skimage.transform.EuclideanTransform class

The EuclideanTransform class in the scikit image library provides a convenient way to apply Euclidean transformations to images and other geometric objects.

Syntax

Following is the syntax of this class −

class skimage.transform.EuclideanTransform(matrix=None, rotation=None, translation=None, *, dimensionality=2)

Here are the parameters and attributes of the class −

  • matrix: An optional parameter that represents the homogeneous transformation matrix. It should be a (D+1, D+1) array-like object, where D is the dimensionality of the transformation. The matrix defines the specific transformation to be applied.
  • rotation: An optional parameter that allows you to specify the rotation angle in radians. It can be a single float value or a sequence of float values. In 2D, it's a single rotation; in 3D, it represents Euler rotation angles.
  • translation: An optional parameter that specifies a sequence of float values representing translation along each axis.
  • dimensionality: An optional integer parameter that represents the number of dimensions of the transformation.
  • params: It is an attribute that stores the homogeneous transformation matrix. It is a (D+1, D+1) array, where D is the dimensionality of the transformation.

Following are the methods of the class −

  • estimate(src, dst): This method is used to estimate the transformation based on a set of corresponding points (source and destination coordinates).
  • inverse: This method returns a transform object representing the inverse transformation.

The EuclideanTransform class is inherited from the ProjectiveTransform class.

Example

In the following example, a Euclidean transformation is applied to the input image using the transform.EuclideanTransform class from scikit-image, which specifies a rotation of np.pi/12 radians (15 degrees) and a translation of (100, -20) pixels.

import numpy as np
import matplotlib.pyplot as plt
from skimage import io, transform, img_as_float

# Define the EuclideanTransform
tform = transform.EuclideanTransform(rotation=np.pi / 12., translation=(100, -20))

# Load an input image
image = io.imread('Images/logo.jpg')
img = img_as_float(image)

# Apply the transformation to the image
tf_img = transform.warp(img, tform.inverse)

# Plot the original and transformed images side by side
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].axis('off')

axes[1].imshow(tf_img)
axes[1].set_title('Euclidean transformation')
axes[1].axis('off')
plt.show()

Output

On executing the above program, you will get the following output −

Example

In this example, the Euclidean transformation is defined by directly providing a transformation matrix to the transform.EuclideanTransform constructor.

import numpy as np
import matplotlib.pyplot as plt
from skimage import io, transform, img_as_float

# Define the EuclideanTransform using the full transformation matrix
matrix = np.array([[np.cos(np.pi/12), -np.sin(np.pi/12), 100],
                   [np.sin(np.pi/12), np.cos(np.pi/12), -20],
                   [0, 0, 1]])
tform = transform.EuclideanTransform(matrix)

# Load an input image
image = io.imread('Images/butterfly.jpg')

img = img_as_float(image)

# Apply the transformation to the image
tf_img = transform.warp(img, tform.inverse)

# Plot the original and transformed images side by side
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].axis('off')

axes[1].imshow(tf_img)
axes[1].set_title('Euclidean transformation')
axes[1].axis('off')
plt.show()

Output

On executing the above program, you will get the following output −

Advertisements