【问题标题】:Easy way to calculate the shift between two arrays? Python计算两个数组之间偏移的简单方法? Python
【发布时间】:2015-04-21 09:58:37
【问题描述】:

我有两个 dicom 图像,可以使用下面的代码计算图像的均方误差。但是,与另一张图像相比,一张图像可能存在固有的偏移(如果我的成像器略微未对齐)。有没有一种简单的方法来计算两个 numpy 数组的移位?

我已尝试将数组每个方向移动几个像素并计算最小 MSQ。但是,这还不够健壮。任何帮助或建议将不胜感激!

import numpy as np
import dicom

#first image
ds = dicom.read_file("U:\\temp\\1.dcm")
array1 = ds.pixel_array
#second image
ds1 = dicom.read_file("U:\\temp\\5.dcm")
array2 = ds1.pixel_array

#shifting image by a number of pixels in any direction
arr1 = np.roll(array2, 100,  axis=1)
imshow(arr1)

def mse(imageA, imageB):
    # the 'Mean Squared Error' between the two images is the
    # sum of the squared difference between the two images;
    err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
    err /= float(imageA.shape[0] * imageA.shape[1])

    # return the MSE, the lower the error, the more "similar"
    # the two images are
    return err

first_try = mse(array1, array2)
second_try = mse(arr1, array2)

【问题讨论】:

  • 如果“移位”始终是一种平移,phase correlation 通常工作得很好,而且它的优势是它对图像之间的噪声和亮度变化非常稳健

标签: python image-processing numpy dicom image-registration


【解决方案1】:

如果您确定图像将完全相同,除了这个转变,认为最好的解决方案是scipy.signal.correlate2d

import numpy as np
from scipy.signal import correlate2d

a = np.random.random((200, 200))
b = np.roll(np.roll(a, 15, axis=0),-33, axis=1)

corr = correlate2d(a, b)

shift = np.where(corr==corr.max())
shift = (shift[0]%a.shape[0], shift[1]%a.shape[1])

这给出了正确的值:

(array([184]), array([32]))

【讨论】:

  • 标准互相关往往对模板和参考图像的亮度和对比度变化相当敏感。对于逼真的图像,通常需要通过在重叠区域下减去均值并除以标准差来normalize
  • @ali_m 是的,这就是为什么我说“如果您确定图像将完全相同,除了这个转变”。
  • 当然 - 我只是想建议一种方法来处理它们不相同的更常见情况
【解决方案2】:

不看图片很难说,对一张图片有效的方法可能对下一张无效。但是一般尝试:

  • 多尺度估计 - 对数组进行下采样,在粗略尺度上计算位移,并在下一个尺度上使用该位移(补偿缩放)进行初始化。
  • 可靠的错误分数,如绝对差之和。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    相关资源
    最近更新 更多