我有这个公式,我正在努力编写它的Python等价物:
其中I(x,y)是numpy数组形式的图像,强度值可使用行列索引访问。 xbar是质心x坐标,已知。
我对代码的尝试如下:
# centroid is a tuple of (x,y) of the coordinate position. image is a 2d array of the image.
import scipy.integrate as integrate
height, width = image.shape[0:2]
intensity_func = lambda x,y: image[y,x]
denom = int.dblquad(intensity_func, 0, height, lambda x: 0, lambda x: width)
x_f = lambda x,y: (x-centroid[0])**2 * intensity_func(x,y)
x_num = int.nquad(x_f, [[0, width],[0, height]])
sig_x = np.sqrt(x_num[0]/denom[0])
print 4*sig_x