同形异义词的类型

Homographies 是保持点对齐的欧几里得空间的变换。同形对应的特定情况对应于更多性质的守恒,例如平行度(仿射变换)、形状(相似变换)或距离(欧几里得变换)。

2D欧几里得空间(即,对于2D灰度或多通道图像)上的单应由3x3矩阵定义。所有类型的单应关系都可以通过传递变换矩阵或更简单变换的参数(旋转、缩放等)来定义它们构成了完整的变形。

这里显示了SCRICKIT-IMAGE中可用的不同类型的单应项,通过增加复杂性的顺序(即,通过减少约束的数量)。虽然我们在这里关注的是变换的数学属性,但教程 使用几何变换 解释如何将此类转换用于各种任务,如图像扭曲或参数估计。

import math
import numpy as np
import matplotlib.pyplot as plt

from skimage import data
from skimage import transform
from skimage import img_as_float

欧几里得(刚性)变换

A Euclidean transformation ,也称为刚性变换,保持点对之间的欧几里得距离。它可以被描述为绕原点旋转,然后平移。

tform = transform.EuclideanTransform(
    rotation=np.pi / 12.,
    translation = (100, -20)
    )
print(tform.params)

输出:

[[  0.96592583  -0.25881905 100.        ]
 [  0.25881905   0.96592583 -20.        ]
 [  0.           0.           1.        ]]

现在,让我们将此转换应用于图像。因为我们正试图重建 图像 转换后,查看 坐标 从输入图像到输出,这就是转换给我们的结果。相反,对于输出图像中的每个像素(坐标),我们希望找出它在输入图像中的位置。因此,我们需要使用 tform ,而不是 tform 直接去吧。

img = img_as_float(data.chelsea())
tf_img = transform.warp(img, tform.inverse)
fig, ax = plt.subplots()
ax.imshow(tf_img)
_ = ax.set_title('Euclidean transformation')
Euclidean transformation

对于围绕图像中心的旋转,可以组成一个平移来改变第一个平移的原点、旋转,最后是逆平移。

rotation = transform.EuclideanTransform(rotation=np.pi/3)
shift = transform.EuclideanTransform(translation=-np.array(img.shape[:2]) / 2)
# Compose transforms by multiplying their matrices
matrix = np.linalg.inv(shift.params) @ rotation.params @ shift.params
tform = transform.EuclideanTransform(matrix)
tf_img = transform.warp(img, tform.inverse)
fig, ax = plt.subplots()
_ = ax.imshow(tf_img)
plot transform types

相似变换

A similarity transformation 保留对象的形状。它结合了缩放、平移和旋转。

tform = transform.SimilarityTransform(
    scale=0.5,
    rotation=np.pi/12,
    translation=(100, 50))
print(tform.params)
tf_img = transform.warp(img, tform.inverse)
fig, ax = plt.subplots()
ax.imshow(tf_img)
_ = ax.set_title('Similarity transformation')
Similarity transformation

输出:

[[  0.48296291  -0.12940952 100.        ]
 [  0.12940952   0.48296291  50.        ]
 [  0.           0.           1.        ]]

仿射变换

一个 affine transformation 保留线条(因此对象的对齐方式)以及线条之间的平行度。它可以分解为一个相似变换和一个 shear transformation

tform = transform.AffineTransform(
        shear=np.pi/6,
        )
print(tform.params)
tf_img = transform.warp(img, tform.inverse)
fig, ax = plt.subplots()
ax.imshow(tf_img)
_ = ax.set_title('Affine transformation')
Affine transformation

输出:

[[ 1.        -0.5        0.       ]
 [ 0.         0.8660254  0.       ]
 [ 0.         0.         1.       ]]

射影变换(单应)

A homography 也称为射影变换,它保留了直线,但不一定是平行的。

matrix = np.array([[1, -0.5, 100],
                   [0.1, 0.9, 50],
                   [0.0015, 0.0015, 1]])
tform = transform.ProjectiveTransform(matrix=matrix)
tf_img = transform.warp(img, tform.inverse)
fig, ax = plt.subplots()
ax.imshow(tf_img)
ax.set_title('Projective transformation')

plt.show()
Projective transformation

另请参阅

脚本的总运行时间: (0分0.775秒)

Gallery generated by Sphinx-Gallery