<jupyter_start><jupyter_text>Using geometric transformationsIn this example, we will see how to use geometric transformations in the contextof image processing.<jupyter_code>import math
import numpy as np
import matplotlib.pyplot as plt

from skimage import data
from skimage import transform<jupyter_output><empty_output><jupyter_text>BasicsSeveral different geometric transformation types are supported: similarity,affine, projective and polynomial. For a tutorial on the available types oftransformations, see `sphx_glr_auto_examples_transform_plot_transform_types.py`.Geometric transformations can either be created using the explicitparameters (e.g. scale, shear, rotation and translation) or thetransformation matrix.First we create a transformation using explicit parameters:<jupyter_code>tform = transform.SimilarityTransform(scale=1, rotation=math.pi/2,
                                      translation=(0, 1))
print(tform.params)<jupyter_output><empty_output><jupyter_text>Alternatively you can define a transformation by the transformation matrixitself:<jupyter_code>matrix = tform.params.copy()
matrix[1, 2] = 2
tform2 = transform.SimilarityTransform(matrix)<jupyter_output><empty_output><jupyter_text>These transformation objects can then be used to apply forward and inversecoordinate transformations between the source and destination coordinatesystems:<jupyter_code>coord = [1, 0]
print(tform2(coord))
print(tform2.inverse(tform(coord)))<jupyter_output><empty_output><jupyter_text>Image warpingGeometric transformations can also be used to warp images:<jupyter_code>text = data.text()

tform = transform.SimilarityTransform(scale=1, rotation=math.pi/4,
                                      translation=(text.shape[0]/2, -100))

rotated = transform.warp(text, tform)
back_rotated = transform.warp(rotated, tform.inverse)

fig, ax = plt.subplots(nrows=3)

ax[0].imshow(text, cmap=plt.cm.gray)
ax[1].imshow(rotated, cmap=plt.cm.gray)
ax[2].imshow(back_rotated, cmap=plt.cm.gray)

for a in ax:
    a.axis('off')

plt.tight_layout()<jupyter_output><empty_output><jupyter_text>Parameter estimationIn addition to the basic functionality mentioned above you can alsoestimate the parameters of a geometric transformation using the least-squares method.This can amongst other things be used for image registration orrectification, where you have a set of control points orhomologous/corresponding points in two images.Let's assume we want to recognize letters on a photograph which was nottaken from the front but at a certain angle. In the simplest case of aplane paper surface the letters are projectively distorted. Simple matchingalgorithms would not be able to match such symbols. One solution to thisproblem would be to warp the image so that the distortion is removed andthen apply a matching algorithm:<jupyter_code>text = data.text()

src = np.array([[0, 0], [0, 50], [300, 50], [300, 0]])
dst = np.array([[155, 15], [65, 40], [260, 130], [360, 95]])

tform3 = transform.ProjectiveTransform()
tform3.estimate(src, dst)
warped = transform.warp(text, tform3, output_shape=(50, 300))

fig, ax = plt.subplots(nrows=2, figsize=(8, 3))

ax[0].imshow(text, cmap=plt.cm.gray)
ax[0].plot(dst[:, 0], dst[:, 1], '.r')
ax[1].imshow(warped, cmap=plt.cm.gray)

for a in ax:
    a.axis('off')

plt.tight_layout()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09746646795827124, 'fraction_numerical': 0.021758569299552905, 'mean_word_length': 5.848979591836735, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '59561', 'license': 'MIT', 'n_tokens_mistral': 1082, 'n_tokens_neox': 1006, 'n_words': 314, 'path': 'digital-image-processing/notebooks/transform/plot_geometric.ipynb', 'repo_name': 'sinamedialab/courses', 'size': '3355'}