【问题标题】:Make images overlap, despite being translated使图像重叠,尽管被翻译
【发布时间】:2016-03-18 12:20:44
【问题描述】:

我会有两张图片。

它们要么相同,要么几乎相同。

但有时任一图像可能已在任一轴上移动了几个像素。

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

或者更好的是,处理图像以修复这种不需要的运动的最佳方法是什么?

【问题讨论】:

  • 图像是否歪斜?或者只是翻译,正如“在任一轴上移动几个像素”所暗示的那样?
  • 刚搬家。我修正了措辞。

标签: image comparison skew


【解决方案1】:

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

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

这是一个 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],但我不确定这是否是更普遍的安全假设。

但在很多情况下——尤其是那些图像几乎完全相同且翻译的情况——这种方法应该很有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-24
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多