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