最初,我在中心附近有3个明亮物体的图像(600x600 numpy数组),以及一个相同形状的单独参考图像。我想通过运行互相关来找到每个图像中质心相对于参考图像的相对位移,这是成功的。然后,我想使用这些相对偏移来生成单个图像的2D加权直方图,该二维直方图相对于参考图像完全居中。但是,当我在加权直方图和参考图像之间进行另一个互相关以进行检查时,我期望的是(0.000,0.000)的相对偏移,但得到的值非零。为什么是这样?我认为它们现在应该位于完全相同的位置。我的代码在下面。
import numpy as np
from skimage import feature
from photutils import centroids
# Find precise (subpixel) coordinates of centroid in reference image
x_0, y_0 = centroids.centroid_1dg(ref_img) #uses photutils Gaussian fit
# Cross-correlations: 3 individual images vs. reference image -- indiv_imgs is a list of the 3 image arrays
dx, dy = [], []
for i in range(3):
data = indiv_imgs[i]
# Shift (below) is in [y, x] format
shift, error, diffphase = feature.register_translation(ref_img, data, 1000, space='real')
dx.append(x_0+shift[1])
dy.append(y_0+shift[0])
# Generate weighted 2D histogram given relative shifts
x, y = np.meshgrid(np.arange(600), np.arange(600))
bins = np.linspace(-300, 300, 601)
dx, dy = np.array(dx)[:, None, None]-x[None, ...], np.array(dy)[:, None, None]-y[None, ...]
h, xe, ye = np.histogram2d(dx.ravel(), dy.ravel(), bins=bins, weights=indiv_imgs.ravel())
# Check result: cross-correlation of weighted histogram vs. reference image -- relative shifts should be 0, but aren't!
shift_new, error_new, diffphase_new = feature.register_translation(ref_img, h, 1000, space='real')
print('Detected pixel offset (y, x): {}'.format(shift)) #for some reason this is nonzero: (-0.19, 0.87)