def GetTargetMotion(self): # get current positions obs_curr = self.GetObsFiltered() # get previous positions obs_prev = self.GetObsPrev() # give identities to previous positions target_prev = ell.TargetList() for i,obs in enumerate(obs_prev): obs.identity = i target_prev.append(obs) # match previous and current targets, no velocity oldnids = params.params.nids target_curr = ell.find_flies(target_prev,target_prev,obs_curr) # don't actually assign new identities params.params.nids = oldnids # delete targets that aren't in both frames keyscurr = set(target_curr.keys()) keysprev = set(target_prev.keys()) keysremove = keyscurr - keysprev for i in keysremove: tmp = target_curr.pop(i) keysremove = keysprev - keyscurr for i in keysremove: tmp = target_prev.pop(i) # compute predicted positions target_pred = cvpred(target_prev,target_curr) # store targetmotion = (target_prev,target_curr,target_pred) # return return targetmotion
def GetTargetMotion(self): # get current positions obs_curr = self.GetObsFiltered() # get previous positions obs_prev = self.GetObsPrev() # give identities to previous positions target_prev = ell.TargetList() for i, obs in enumerate(obs_prev): obs.identity = i target_prev.append(obs) # match previous and current targets, no velocity oldnids = params.params.nids target_curr = ell.find_flies(target_prev, target_prev, obs_curr) # don't actually assign new identities params.params.nids = oldnids # delete targets that aren't in both frames keyscurr = set(target_curr.keys()) keysprev = set(target_prev.keys()) keysremove = keyscurr - keysprev for i in keysremove: tmp = target_curr.pop(i) keysremove = keysprev - keyscurr for i in keysremove: tmp = target_prev.pop(i) # compute predicted positions target_pred = cvpred(target_prev, target_curr) # store targetmotion = (target_prev, target_curr, target_pred) # return return targetmotion
def find_flies( old0, old1, obs, ann_file=None ): """All arguments are EllipseLists. Returns an EllipseList.""" # possibly matchidentities should be smart enough to deal with this case # instead of handling it specially here if len(obs) == 0: flies = TargetList() #for e in old1: # flies.append( Ellipse() ) # no obs for any target return flies # make predicted targets targ = m_id.cvpred( old0, old1 ) if params.print_crap: print "targ (%d)"%len(targ), targ print "obs (%d)"%len(obs), obs # make a cost matrix containing the distance from each target to each obs ids = [] for i in targ.iterkeys(): ids.append(i) vals = [] for i in targ.itervalues(): vals.append(i) cost = num.empty( (len(obs), len(targ)) ) for i, observation in enumerate( obs ): for j, target in enumerate( vals ): if target.isDummy(): cost[i,j] = params.max_jump + eps # will be ignored else: cost[i,j] = observation.dist( target ) # find optimal matching between targ and observations obs_for_target, unass_obs = m_id.matchidentities( cost, params.max_jump ) if params.print_crap: print "best matches:", obs_for_target # make a new list containing the best matches to each prediction flies = TargetList() for tt in range( len(targ) ): if obs_for_target[tt] >= 0: obs[obs_for_target[tt]].identity = ids[tt] flies.append( obs[obs_for_target[tt]] ) #else: # flies.append( Ellipse() ) # empty ellipse as a placeholder # append the targets that didn't match any observation for oo in range( len(obs) ): if unass_obs[oo]: if ann_file is None: obs[oo].identity = params.nids params.nids+=1 else: obs[oo].identity = ann_file.GetNewId() flies.append( obs[oo] ) if params.print_crap: print "returning", flies return flies
def cvpred(self,prev,curr,id): if not prev.hasItem(id): if curr.hasItem(id): prev = curr else: return -1 if not curr.hasItem(id): curr = prev currlist = ell.TargetList() prevlist = ell.TargetList() prevlist[id] = prev currlist[id] = curr pred = matchidentities.cvpred(prev,curr)[id] return pred
def GetTargetMotion(self): # if it has already been computed, then just grab and return if hasattr(self,'targetmotion') and self.targetmotion[3] == self.show_frame \ and self.targetmotion[4] == params.params: return self.targetmotion[0:3] # get current positions obs_curr = self.GetObsFiltered() # get previous positions obs_prev = self.GetObsPrev() # give identities to previous positions target_prev = ell.TargetList() for i,obs in enumerate(obs_prev): obs.identity = i target_prev.append(obs) # match previous and current targets, no velocity oldnids = params.params.nids target_curr = ell.find_flies(target_prev,target_prev,obs_curr) # don't actually assign new identities params.params.nids = oldnids # delete targets that aren't in both frames keyscurr = set(target_curr.keys()) keysprev = set(target_prev.keys()) keysremove = keyscurr - keysprev for i in keysremove: tmp = target_curr.pop(i) keysremove = keysprev - keyscurr for i in keysremove: tmp = target_prev.pop(i) # compute predicted positions target_pred = cvpred(target_prev,target_curr) # store self.targetmotion = (target_prev,target_curr,target_pred,self.show_frame,params.params.copy()) # return return self.targetmotion[0:3]
tracks[0][0] = ell.Ellipse(10., 10., 1., 2., num.pi / 3, 0., 0) tracks[0][0].compute_area() # frame 1 # just one ellipse with id = 0 tracks.append(ell.TargetList()) tracks[1][0] = tracks[0][0].copy() tracks[1][0].x += 10. tracks[1][0].y += 10. # frame 2 # detection missed at pred # new ellipse at another location tracks.append(ell.TargetList()) #tracks[2][1] = ell.Ellipse(20.,20.,1.,2.,num.pi/3,0.,1) prev = tracks[0].copy() curr = tracks[1].copy() pred = matchidentities.cvpred(prev, curr) # frame 3 # no ellipses, but predicted to be at pred tracks.append(ell.TargetList()) prev = curr curr = pred pred = matchidentities.cvpred(prev, curr) # frame 4 # new ellipse at pred tracks.append(ell.TargetList()) prev = curr curr = pred pred = matchidentities.cvpred(prev, curr) pred0 = pred[0].copy() pred0.identity = 1 tracks[4][1] = pred0
def fix_lostdetection(self,id2,t2): if DEBUG: print 'testing to see if birth of id2=%d in frame t2=%d is from a lost detection'%(id2,t2) # look for death of some target id1 between t2-1 and t2-params.lostdetection_length+1 # where the distance between the predicted position of id1 in frame t2 is near the # actual position of id2 T = len(self.tracks) # initialize previous and current positions curr = ell.TargetList() prev = ell.TargetList() curr[id2] = self.tracks[t2][id2] if (t2 < T-1) and (self.tracks[t2+1].hasItem(id2)): prev[id2] = self.tracks[t2+1][id2] else: prev[id2] = self.tracks[t2][id2] # initialize the distances mind = [] ids = [] # loop through all frames death can take place on t3 = int(round(max(t2-params.lostdetection_length,0))) t2 = int(t2) for t1 in range(t2-1,t3,-1): # update predicted location pred = matchidentities.cvpred(prev,curr) # compute distance to all targets from predicted location for id1 in list(self.milestones.getdeaths(t1)): mind.append(pred[id2].dist(self.tracks[t1-1][id1])) ids.append(id1) # update prev and curr positions prev = curr curr = pred if len(mind) == 0: if DEBUG: print 'no deaths within %d frames of birth of id2=%d in frame t2=%d'%(params.lostdetection_length,id2,t2) return False # compute closest newborn to predicted location i = num.argmin(num.array(mind)) mind = mind[i] id1 = ids[i] # if this is too far, then we can't fix if mind > params.lostdetection_distance: if DEBUG: print 'id1=%d dies in frame %d, but distance between predicted positions = %.2f > %.2f'%(id1,self.milestones.getdeathframe(id1),mind,params.lostdetection_length) return False # get death frame of id1 t1 = self.milestones.getdeathframe(id1) # add in tracks in frames t1 thru t2-1 # by interpolating start = self.tracks[t1-1][id1] end = self.tracks[t2][id2] for t in range(t1,t2): self.tracks[t][id1] = ellipseinterpolate(start,end,t-t1+1,t2-t) # replace identity id2 in frames t2 thru death of id2 with id1 for t in range(t2,len(self.tracks)): if not self.tracks[t].hasItem(id2): if DEBUG: print 'no id2=%d in frame t=%d'%(id2,t) break tmp = self.tracks[t].pop(id2) tmp.identity = id1 self.tracks[t][id1] = tmp # update death, birth frame data structures d2 = self.milestones.getdeathframe(id2) # remove id2 from all data structures self.milestones.deleteid(id2) # reset death of id1 as d2 self.milestones.setdeath(id1,d2) if DEBUG: print 'fixing lost detection: id1=%d lost in frame t1=%d, found again in frame t2=%d with id2=%d'%(id1,t1,t2,id2) return True
cc = [] # frame 0 tracks_true.append(ell.TargetList()) tracks_true[0][0] = ell.Ellipse(37.,70.,2.,4.,num.pi/3,0.,0) tracks_true[0][0].compute_area() tracks_true[0][1] = ell.Ellipse(60.,70.,2.,4.,num.pi/3,0.,1) tracks_true[0][1].compute_area() # frame 1 tracks_true.append(ell.TargetList()) tracks_true[1][0] = ell.Ellipse(37.,62.,2.,4.,num.pi/3,0.,0) tracks_true[1][0].compute_area() tracks_true[1][1] = ell.Ellipse(53.,62.,2.,4.,num.pi/3,0.,1) tracks_true[1][1].compute_area() for t in range(2,8): tracks_true.append(matchidentities.cvpred(tracks_true[-2],tracks_true[-1])) bw = [] bounds = [0,100,0,100] nr = bounds[1]-bounds[0] nc = bounds[3]-bounds[2] for track_true in tracks_true: tmp = num.zeros((nr,nc),dtype=bool) for ellipse in track_true.itervalues(): tmp |= est.ellipsepixels(ellipse,bounds) bw.append(tmp) colors = ['r','g','b','y'] #for t in range(len(tracks_true)): # mpl.imshow(bw[t]) # for (id,e) in tracks_true[t].iteritems():
return (flies, obs_for_target, unass_obs) tracks_true = [] cc = [] # frame 0 tracks_true.append(ell.TargetList()) tracks_true[0][0] = ell.Ellipse(50., 10., 2., 4., num.pi / 3, 0., 0) tracks_true[0][0].compute_area() # frame 1 tracks_true.append(ell.TargetList()) tracks_true[1][0] = ell.Ellipse(50., 20., 2., 4., num.pi / 3, 0., 0) tracks_true[1][0].compute_area() for t in range(2, 9): tracks_true.append(matchidentities.cvpred(tracks_true[-2], tracks_true[-1])) bw = [] bounds = [0, 100, 0, 100] nr = bounds[1] - bounds[0] nc = bounds[3] - bounds[2] for track_true in tracks_true: tmp = num.zeros((nr, nc), dtype=bool) for ellipse in track_true.itervalues(): tmp |= est.ellipsepixels(ellipse, bounds) bw.append(tmp) for t in range(3, 6): x = tracks_true[t][0].x bw[t][:, x] = False
def find_flies(old0, old1, obs, ann_file=None): """All arguments are EllipseLists. Returns an EllipseList.""" # possibly matchidentities should be smart enough to deal with this case # instead of handling it specially here if len(obs) == 0: flies = TargetList() #for e in old1: # flies.append( Ellipse() ) # no obs for any target return flies # make predicted targets targ = m_id.cvpred(old0, old1) if DEBUG_TRACKINGSETTINGS: print "targ (%d) = %s" % (len(targ), str(targ)) print "obs (%d) = %s" % (len(obs), str(obs)) # make a cost matrix containing the distance from each target to each obs ids = [] for i in targ.iterkeys(): ids.append(i) vals = [] for i in targ.itervalues(): vals.append(i) cost = num.empty((len(obs), len(targ))) # KB 20120109: keep track of whether an observation is the result of splitting a connected component # if it is, then max_jump_split is the maximum jump distance issplit = num.zeros(len(obs), dtype=bool) for i, observation in enumerate(obs): issplit[i] = observation.issplit for j, target in enumerate(vals): if target.isDummy(): cost[i, j] = params.max_jump + eps # will be ignored else: cost[i, j] = observation.dist(target) if DEBUG_TRACKINGSETTINGS: print "cost = " + str(cost) # find optimal matching between targ and observations # KB 20120109: changes to matchidentities so that we could have a different maximum jump distance for # observations that are the result of splitting a connected component obs_for_target, unass_obs = m_id.matchidentities(cost, params.max_jump, issplit, params.max_jump_split) if DEBUG_TRACKINGSETTINGS: print "best matches:", obs_for_target # make a new list containing the best matches to each prediction flies = TargetList() for tt in range(len(targ)): if obs_for_target[tt] >= 0: obs[obs_for_target[tt]].identity = ids[tt] flies.append(obs[obs_for_target[tt]]) #else: # flies.append( Ellipse() ) # empty ellipse as a placeholder # append the targets that didn't match any observation for oo in range(len(obs)): if unass_obs[oo]: if ann_file is None: obs[oo].identity = params.nids params.nids += 1 else: obs[oo].identity = ann_file.GetNewId() flies.append(obs[oo]) if DEBUG_TRACKINGSETTINGS: print "returning", flies return flies
def find_flies( old0, old1, obs, ann_file=None ): """All arguments are EllipseLists. Returns an EllipseList.""" # possibly matchidentities should be smart enough to deal with this case # instead of handling it specially here if len(obs) == 0: flies = TargetList() #for e in old1: # flies.append( Ellipse() ) # no obs for any target return flies # make predicted targets targ = m_id.cvpred( old0, old1 ) if DEBUG_TRACKINGSETTINGS: print "targ (%d) = %s"%(len(targ),str(targ)) print "obs (%d) = %s"%(len(obs),str(obs)) # make a cost matrix containing the distance from each target to each obs ids = [] for i in targ.iterkeys(): ids.append(i) vals = [] for i in targ.itervalues(): vals.append(i) cost = num.empty( (len(obs), len(targ)) ) # KB 20120109: keep track of whether an observation is the result of splitting a connected component # if it is, then max_jump_split is the maximum jump distance issplit = num.zeros(len(obs),dtype=bool) for i, observation in enumerate( obs ): issplit[i] = observation.issplit for j, target in enumerate( vals ): if target.isDummy(): cost[i,j] = params.max_jump + eps # will be ignored else: cost[i,j] = observation.dist( target ) if DEBUG_TRACKINGSETTINGS: print "cost = " + str(cost) # find optimal matching between targ and observations # KB 20120109: changes to matchidentities so that we could have a different maximum jump distance for # observations that are the result of splitting a connected component obs_for_target, unass_obs = m_id.matchidentities( cost, params.max_jump, issplit, params.max_jump_split ) if DEBUG_TRACKINGSETTINGS: print "best matches:", obs_for_target # make a new list containing the best matches to each prediction flies = TargetList() for tt in range( len(targ) ): if obs_for_target[tt] >= 0: obs[obs_for_target[tt]].identity = ids[tt] flies.append( obs[obs_for_target[tt]] ) #else: # flies.append( Ellipse() ) # empty ellipse as a placeholder # append the targets that didn't match any observation for oo in range( len(obs) ): if unass_obs[oo]: if ann_file is None: obs[oo].identity = params.nids params.nids+=1 else: obs[oo].identity = ann_file.GetNewId() flies.append( obs[oo] ) if DEBUG_TRACKINGSETTINGS: print "returning", flies return flies
tracks[0][0] = ell.Ellipse(10.,10.,1.,2.,num.pi/3,0.,0) tracks[0][0].compute_area() # frame 1 # just one ellipse with id = 0 tracks.append(ell.TargetList()) tracks[1][0] = tracks[0][0].copy() tracks[1][0].x += 10. tracks[1][0].y += 10. # frame 2 # detection missed at pred # new ellipse at another location tracks.append(ell.TargetList()) #tracks[2][1] = ell.Ellipse(20.,20.,1.,2.,num.pi/3,0.,1) prev = tracks[0].copy() curr = tracks[1].copy() pred = matchidentities.cvpred(prev,curr) # frame 3 # no ellipses, but predicted to be at pred tracks.append(ell.TargetList()) prev = curr curr = pred pred = matchidentities.cvpred(prev,curr) # frame 4 # new ellipse at pred tracks.append(ell.TargetList()) prev = curr curr = pred pred = matchidentities.cvpred(prev,curr) pred0 = pred[0].copy() pred0.identity = 1 tracks[4][1] = pred0