尽管经过翻译,仍可使图像重叠

Her*_*son 6 comparison image skew

我会有两张照片.

它们将是相同或几乎相同的.

但有时任何一个图像都可能在任一轴上移动了几个像素.

什么是最好的方法来检测是否有这样的举动?

或者更好的是,操作图像的最佳方法是什么才能解决这种不必要的运动?

ohr*_*uus 2

如果图像确实几乎相同,并且只是平移(即没有倾斜、旋转、缩放等),则可以尝试使用互相关。

当您将图像与其自身互相关时(这是自相关),最大值将位于结果矩阵的中心。如果垂直或水平移动图像,然后与原始图像进行互相关,最大值的位置将相应移动。通过测量最大值位置相对于预期位置的偏移,您可以确定图像垂直和水平平移的距离。

这是一个 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)
Run Code Online (Sandbox Code Playgroud)

这会产生坐标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)
Run Code Online (Sandbox Code Playgroud)

这给出new_max_coords = (39, 60),正确地指示图像水平偏移 1(因为np.array(new_max_coords) - np.array(max_coords)[0, 1])。使用此信息,您可以移动图像以补偿平移。

请注意,如果您决定走这条路,您可能会遇到很多问题需要解决。在给定图像尺寸的情况下,确定最大坐标“应该”遵循相关性时(即避免计算自相关并凭经验确定这些坐标),尤其是在图像具有偶数的情况下,相差一的错误比比皆是。行/列数。在上面的例子中,中心只是[num_rows-1, num_cols-1],但我不确定这是否是一个更普遍的安全假设。

但对于许多情况,尤其是那些图像几乎完全相同且经过翻译的情况,这种方法应该效果很好。