def __init__(self, res=(640,480), Nframes=10): x=np.linspace(-10,10, res[0]) y=np.linspace(-10,10, res[1]) Y,X=np.meshgrid(y,x) Ypos=np.linspace(-3,3,Nframes/2) Ypos=np.hstack([Ypos, Ypos[::-1]]) #sclX=ones(Nframes) #gauss2d=100*gauss(X*sclX[:,np.newaxis, np.newaxis], [1, 0,0.2])*gauss(Y, [1, 0,0.2]) self.dat=np.empty((Nframes, res[0], res[1])) for k in range(Nframes): if k<4: g2d=0 else: g2d=200*MT.gauss2d(X,Y, 4, 4+0*Ypos[k],0.5,0.5,0) self.dat[k]=1*abs(np.random.normal(size=(res[0], res[1]) ) ) + g2d #self.dat=self.dat.astype('u4') self.nextInd=0
y, x = np.mgrid[:frame.shape[0], :frame.shape[1]] m00 = frame.sum() #or 1.0 m10 = (frame * x).sum() / m00 m01 = (frame * y).sum() / m00 dx, dy = x - m10, y - m01 m20 = (frame * dx ** 2).sum() / m00 m02 = (frame * dy ** 2).sum() / m00 m11 = (frame * dx * dy).sum() / m00 q = N.sqrt((m20 - m02) ** 2 + 4 * m11 ** 2) minor_axis = 2 ** 1.5 * N.sqrt(m20 + m02 + q) major_axis = 2 ** 1.5 * N.sqrt(m20 + m02 - q) angle = 0.5 * N.arctan2(2 * m11, m20 - m02) ellipticity = minor_axis / major_axis centroid = (m01, m10) return 8*m00/major_axis/minor_axis/N.pi, centroid, major_axis/4, minor_axis/4, angle if __name__=="__main__": from numpy import * from numpy import random import MT #x=arange(640) #y=arange(480) X,Y=mgrid[:640, :480] frame=7*MT.gauss2d(X,Y, 320, 240, 50,70, 0)+0.0*random.normal(size=(640,480)) gfit=GaussFitter() #print(gfit.fitgaussian(frame))