如果图像真的几乎相同,并且只是简单地平移(即没有倾斜、旋转、缩放等),您可以尝试使用互相关。
当您将图像与其自身进行互相关时(这是自相关),最大值将位于结果矩阵的中心。如果您垂直或水平移动图像,然后与原始图像互相关,最大值的位置将相应移动。通过测量最大值位置相对于预期位置的偏移,您可以确定图像垂直和水平平移了多远。
这是一个 Python 中的玩具示例。首先导入一些东西,生成测试图像,然后检查自相关:
import numpy as np
from scipy.signal import correlate2d
# generate a test image
num_rows, num_cols = 40, 60
image = np.random.random((num_rows, num_cols))
# get the auto-correlation
correlated = correlate2d(image, image, mode='full')
# get the coordinates of the maximum value
max_coords = np.unravel_index(correlated.argmax(), correlated.shape)
这会产生坐标max_coords = (39, 59)。现在来测试一下方法,将图像向右移动一列,在左侧添加一些随机值,然后再次找到互相关中的最大值:
image_translated = np.concatenate(
(np.random.random((image.shape[0], 1)), image[:, :-1]),
axis=1)
correlated = correlate2d(image_translated, image, mode='full')
new_max_coords = np.unravel_index(correlated.argmax(), correlated.shape)
这给出了new_max_coords = (39, 60),正确指示图像水平偏移1(因为np.array(new_max_coords) - np.array(max_coords) 是[0, 1])。使用此信息,您可以移动图像以补偿翻译。
请注意,如果您决定走这条路,您可能需要解决很多问题。给定图像的尺寸,在确定最大坐标“应该”遵循相关性(即避免计算自相关并凭经验确定这些坐标)时,会出现很多错误,尤其是在图像具有偶数的情况下行数/列数。在上面的示例中,中心只是[num_rows-1, num_cols-1],但我不确定这是否是更普遍的安全假设。
但在很多情况下——尤其是那些图像几乎完全相同且仅翻译的情况——这种方法应该很有效。