计算两个阵列之间转换的简便方法?蟒蛇

时间:2015-04-21 09:58:38

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

我有两个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)

2 个答案:

答案 0 :(得分:2)

如果你确定除了这种转变之外的图像将完全相同,那么认为最好的解决方案是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]))

答案 1 :(得分:1)

如果没有看到图像很难说,在一张图片上运行的图片可能无法在下一张图像上运行。但总的来说,尝试:

  • 多尺度估算 - 对数组进行下采样,计算粗尺度的偏移,并在下一个尺度上使用此偏移进行初始化(补偿缩放)。
  • 强大的错误分数,如绝对差异的总和。
相关问题