def cc(self,t): # perform background subtraction (dfore,bw) = self.bg.sub_bg(t+params.start_frame) # for each pixel, find the target it most likely belongs to (y,x) = num.where(bw) mind = num.zeros(y.shape) mind[:] = num.inf closest = num.zeros(y.shape) for targ in self.tracks[t].itervalues(): S = est.ell2cov(targ.major,targ.minor,targ.angle) Sinv = num.linalg.inv(S) xx = x.astype(float) - targ.x yy = y.astype(float) - targ.y d = xx**2*Sinv[0,0] + 2*Sinv[0,1]*xx*yy + yy**2*Sinv[1,1] issmall = d <= mind mind[issmall] = d[issmall] closest[issmall] = targ.identity # set each pixel in L to belong to the closest target L = num.zeros(bw.shape) L[bw] = closest+1 #mpl.imshow(L) #mpl.show() return (L,dfore)
def splitobservation(bw,dfore,k,init): (r,c) = num.where(bw) if DEBUG: print 'number of pixels in component being split: %d'%len(r) x = num.hstack((c.reshape(c.size,1),r.reshape(r.size,1))) w = dfore[bw] if DEBUG: print 'data being clustered: ' if DEBUG: print x if DEBUG: print 'with weights: ' if DEBUG: print w # create means and covariance matrices to initialize mu0 = num.zeros((k,2)) S0 = num.zeros((k,2,2)) priors0 = num.zeros(k) for i in range(k): if DEBUG: print 'predicted ellipse %d: '%i + str(init[i]) mu0[i,0] = init[i].x mu0[i,1] = init[i].y S0[:,:,i] = est.ell2cov(init[i].major,init[i].minor,init[i].angle) priors0[i] = init[i].area (tmpmajor,tmpminor,tmpangle) = est.cov2ell(S0[:,:,i]) priors0 = priors0 / num.sum(priors0) if DEBUG: print 'initializing with ' if DEBUG: print 'mu0 = ' if DEBUG: print mu0 if DEBUG: print 'S0 = ' if DEBUG: for i in range(k): print S0[:,:,i] if DEBUG: print 'priors0 = ' if DEBUG: print priors0 # are there no data points? if len(r) == 0: return None (mu,S,priors,gamma,negloglik) = kcluster.gmmem(x,mu0,S0,priors0,weights=w,thresh=.1,mincov=.015625) obs = [] for i in range(k): (major,minor,angle) = est.cov2ell(S[:,:,i]) obs.append(ell.Ellipse(mu[i,0],mu[i,1],minor,major,angle)) obs[-1].compute_area() return obs