Example #1
0
    def GetObsUnfiltered(self, *args):

        # do we need to recompute?
        mustcompute = False
        if hasattr(self, 'obs_unfiltered') and self.obs_unfiltered.issame(
                self.show_frame):
            for arg in args:
                if self.obs_unfiltered.__dict__[arg] is None:
                    mustcompute = True
                    break
        else:
            mustcompute = True

        if DEBUG_TRACKINGSETTINGS: print 'mustcompute = ' + str(mustcompute)
        if DEBUG_TRACKINGSETTINGS and not mustcompute:
            print 'stored obs_unfiltered = ' + str(self.obs_unfiltered)

        # if we are only interested in the unfiltered observation
        if len(args) == 0:
            # if it has not yet been computed for this frame, compute
            if mustcompute:
                obs_unfiltered = ell.find_ellipses(self.bg_imgs.dfore.copy(),
                                                   self.bg_imgs.cc.copy(),
                                                   self.bg_imgs.ncc, False)
                self.obs_unfiltered = StoredObservations(
                    obs_unfiltered, self.show_frame)
            return self.obs_unfiltered.obs

        # compute if necessary
        if mustcompute:
            wx.BeginBusyCursor()
            wx.YieldIfNeeded()
            (obs_unfiltered,ellsmall,elllarge,didlowerthresh,didmerge,diddelete,didsplit) = \
                                         ell.find_ellipses_display(self.bg_imgs.dfore.copy(),self.bg_imgs.cc.copy(),self.bg_imgs.ncc)
            if DEBUG_TRACKINGSETTINGS:
                print 'computed obs_unfiltered = ' + str(
                    obs_unfiltered) + ', len = ' + str(len(obs_unfiltered))
            if DEBUG_TRACKINGSETTINGS: print 'ellsmall = ' + str(ellsmall)
            if DEBUG_TRACKINGSETTINGS: print 'elllarge = ' + str(elllarge)
            if DEBUG_TRACKINGSETTINGS:
                print 'didlowerthresh = ' + str(didlowerthresh)
            if DEBUG_TRACKINGSETTINGS: print 'didmerge = ' + str(didmerge)
            if DEBUG_TRACKINGSETTINGS: print 'diddelete = ' + str(diddelete)
            if DEBUG_TRACKINGSETTINGS: print 'didsplit = ' + str(didsplit)
            wx.EndBusyCursor()
            self.obs_unfiltered = StoredObservations(obs_unfiltered,
                                                     self.show_frame, ellsmall,
                                                     elllarge, didlowerthresh,
                                                     didmerge, diddelete,
                                                     didsplit)
            if DEBUG_TRACKINGSETTINGS: print 'stored obs_unfiltered: '
            if DEBUG_TRACKINGSETTINGS: print str(self.obs_unfiltered)

        # create return list
        ret = (self.obs_unfiltered.obs, )
        for arg in args:
            ret += (self.obs_unfiltered.__dict__[arg], )

        return ret
    def GetObsFiltered(self):
        if not(hasattr(self,'obs_filtered') and self.obs_filtered.issame(self.show_frame)):
            wx.BeginBusyCursor()
            wx.Yield()
            obs_filtered = ell.find_ellipses(self.bg_imgs.dfore,self.bg_imgs.bw,True)
            wx.EndBusyCursor()
            self.obs_filtered = StoredObservations(obs_filtered,self.show_frame)

        return self.obs_filtered.obs
 def GetObsPrev(self):
     if not(hasattr(self,'obs_prev') and self.obs_filtered.issame(self.show_frame-1)):
         wx.BeginBusyCursor()
         wx.Yield()
         prevframe = num.maximum(0,self.show_frame-1)
         (dfore,bw) = self.bg_imgs.sub_bg(prevframe)
         obs_filtered = ell.find_ellipses(dfore,bw,True)
         wx.EndBusyCursor()
         self.obs_prev = StoredObservations(obs_filtered,self.show_frame)
     return self.obs_prev.obs
    def GetObsFiltered(self):
        if not(hasattr(self,'obs_filtered') and self.obs_filtered.issame(self.show_frame)):
            if DEBUG_TRACKINGSETTINGS: print 'computing filtered observations'
            obs_filtered = ell.find_ellipses(self.bg_imgs.dfore.copy(),self.bg_imgs.cc.copy(),self.bg_imgs.ncc,True)
            self.obs_filtered = StoredObservations(obs_filtered,self.show_frame)
        else:
            if DEBUG_TRACKINGSETTINGS: print 'filtered observations already computed'
        if DEBUG_TRACKINGSETTINGS: print 'obs_filtered:\n' + str(self.obs_filtered)

        return self.obs_filtered.obs
Example #5
0
 def GetObsPrev(self):
     if not (hasattr(self, 'obs_prev')
             and self.obs_filtered.issame(self.show_frame - 1)):
         wx.BeginBusyCursor()
         wx.Yield()
         prevframe = num.maximum(0, self.show_frame - 1)
         (dfore, bw, cc, ncc) = self.bg_imgs.sub_bg(prevframe)
         obs_filtered = ell.find_ellipses(dfore, cc, ncc, True)
         wx.EndBusyCursor()
         self.obs_prev = StoredObservations(obs_filtered, self.show_frame)
     return self.obs_prev.obs
    def GetObsUnfiltered(self,*args):

        # do we need to recompute?
        mustcompute = False
        if hasattr(self,'obs_unfiltered') and self.obs_unfiltered.issame(self.show_frame):
            for arg in args:
               if self.obs_unfiltered.__dict__[arg] is None:
                   mustcompute = True
                   break
        else:
            mustcompute = True

        if DEBUG_TRACKINGSETTINGS: print 'mustcompute = ' + str(mustcompute)
        if DEBUG_TRACKINGSETTINGS and not mustcompute:
          print 'stored obs_unfiltered = ' + str(self.obs_unfiltered)

        # if we are only interested in the unfiltered observation
        if len(args) == 0:
            # if it has not yet been computed for this frame, compute
            if mustcompute:
                obs_unfiltered = ell.find_ellipses(self.bg_imgs.dfore.copy(),self.bg_imgs.cc.copy(),self.bg_imgs.ncc,False)
                self.obs_unfiltered = StoredObservations(obs_unfiltered,self.show_frame)
            return self.obs_unfiltered.obs
          
        # compute if necessary
        if mustcompute:
            wx.BeginBusyCursor()
            wx.YieldIfNeeded()
            (obs_unfiltered,ellsmall,elllarge,didlowerthresh,didmerge,diddelete,didsplit) = \
                                         ell.find_ellipses_display(self.bg_imgs.dfore.copy(),self.bg_imgs.cc.copy(),self.bg_imgs.ncc)
            if DEBUG_TRACKINGSETTINGS: print 'computed obs_unfiltered = ' + str(obs_unfiltered) + ', len = ' + str(len(obs_unfiltered))
            if DEBUG_TRACKINGSETTINGS: print 'ellsmall = ' + str(ellsmall)
            if DEBUG_TRACKINGSETTINGS: print 'elllarge = ' + str(elllarge)
            if DEBUG_TRACKINGSETTINGS: print 'didlowerthresh = ' + str(didlowerthresh)
            if DEBUG_TRACKINGSETTINGS: print 'didmerge = ' + str(didmerge)
            if DEBUG_TRACKINGSETTINGS: print 'diddelete = ' + str(diddelete)
            if DEBUG_TRACKINGSETTINGS: print 'didsplit = ' + str(didsplit)
            wx.EndBusyCursor()
            self.obs_unfiltered = StoredObservations(obs_unfiltered,self.show_frame,
                                                     ellsmall,elllarge,didlowerthresh,
                                                     didmerge,diddelete,didsplit)
            if DEBUG_TRACKINGSETTINGS: print 'stored obs_unfiltered: '
            if DEBUG_TRACKINGSETTINGS: print str(self.obs_unfiltered)

        # create return list
        ret = (self.obs_unfiltered.obs,)
        for arg in args:
            ret += (self.obs_unfiltered.__dict__[arg],)

        return ret
Example #7
0
    def GetObsFiltered(self):
        if not (hasattr(self, 'obs_filtered')
                and self.obs_filtered.issame(self.show_frame)):
            if DEBUG_TRACKINGSETTINGS: print 'computing filtered observations'
            obs_filtered = ell.find_ellipses(self.bg_imgs.dfore.copy(),
                                             self.bg_imgs.cc.copy(),
                                             self.bg_imgs.ncc, True)
            self.obs_filtered = StoredObservations(obs_filtered,
                                                   self.show_frame)
        else:
            if DEBUG_TRACKINGSETTINGS:
                print 'filtered observations already computed'
        if DEBUG_TRACKINGSETTINGS:
            print 'obs_filtered:\n' + str(self.obs_filtered)

        return self.obs_filtered.obs
    def GetObsUnfiltered(self,*args):

        # if we are only interested in the unfiltered observation
        if len(args) == 0:
            # if it has not yet been computed for this frame, compute
            if not(hasattr(self,'obs_unfiltered') and self.obs_unfiltered.issame(self.show_frame)):
                wx.BeginBusyCursor()
                wx.YieldIfNeeded()
                obs_unfiltered = ell.find_ellipses(self.bg_imgs.dfore,self.bg_imgs.bw,False)
                wx.EndBusyCursor()
                self.obs_unfiltered = StoredObservations(obs_unfiltered,self.show_frame)
            return self.obs_unfiltered.obs

        # otherwise, we need to check for attributes as well
        mustcompute = False
        if hasattr(self,'obs_unfiltered') and self.obs_unfiltered.issame(self.show_frame):
            for arg in args:
               if self.obs_unfiltered.__dict__[arg] is None:
                   mustcompute = True
                   break
        else:
            mustcompute = True
            
        # compute if necessary
        if mustcompute:
            wx.BeginBusyCursor()
            wx.YieldIfNeeded()
            (obs_unfiltered,issmall,islarge,didlowerthresh,didmerge,diddelete,didsplit) = \
                                         ell.find_ellipses_display(self.bg_imgs.dfore,self.bg_imgs.bw)
            wx.EndBusyCursor()
            self.obs_unfiltered = StoredObservations(obs_unfiltered,self.show_frame,
                                                     issmall,islarge,didlowerthresh,
                                                     didmerge,diddelete,didsplit)

        # create return list
        ret = (self.obs_unfiltered.obs,)
        for arg in args:
            ret += (self.obs_unfiltered.__dict__[arg],)

        return ret
Example #9
0
def run_movie(moviefile, comms):
    """Run new-style tracking for a test movie."""
    quit_val = comms['quit_val']
    cmp_list = comms['cmp_list']
    shape_dict = comms['shape_dict']

    movie, bg_model, ann_file, hindsight = setup_tracking(moviefile, False)

    if quit_val.value > 0: return

    try:
        test_shape_bounds()
    except:
        print "**aborting with invalid shape bounds"
        print "minshape", params.minshape
        print "maxshape", params.maxshape
        print_vals()
        quit_val.value = 1
        raise

    shape_dict['minshape'] = params.minshape
    shape_dict['meanshape'] = params.meanshape
    shape_dict['maxshape'] = params.maxshape

    fst = time.time()

    params.start_frame = 0
    if MAX_FRAMES is None:
        max_frames = movie.get_n_frames()
    else:
        max_frames = min(movie.get_n_frames(), MAX_FRAMES)
    for frame in range(max_frames):
        if cmp_list is not None: counts = []

        # perform background subtraction
        (dfore, isfore, cc, ncc) = bg_model.sub_bg(frame)

        # find observations
        if PRINT_COUNTS and (frame == PRINT_FRAME or PRINT_FRAME is None):
            print frame, "ncc", ncc
        if cmp_list is not None: counts.append(ncc)
        ellipses = ell.find_ellipses(dfore, cc, ncc)
        if PRINT_COUNTS and (frame == PRINT_FRAME or PRINT_FRAME is None):
            print frame, "found ellipses", len(ellipses)
        if cmp_list is not None: counts.append(len(ellipses))

        try:
            test_ellipses(ellipses)
        except AssertionError:
            print "**find_ellipses output error in frame", frame
            print_vals()
            quit_val.value = 1
            raise

        # match target identities to observations
        if len(ann_file) > 1:
            flies = ell.find_flies(ann_file[-2], ann_file[-1], ellipses,
                                   ann_file)
        elif len(ann_file) == 1:
            flies = ell.find_flies(ann_file[-1], ann_file[-1], ellipses,
                                   ann_file)
        else:
            flies = ell.TargetList()
            for i, obs in enumerate(ellipses):
                newid = ann_file.GetNewId()
                obs.identity = newid
                flies.append(obs)

        try:
            test_ellipses(flies)
        except AssertionError:
            print "**find_flies output error in frame", frame
            print_vals()
            quit_val.value = 1
            raise

        if PRINT_COUNTS and (frame == PRINT_FRAME or PRINT_FRAME is None):
            print frame, "found flies", len(flies)
        if cmp_list is not None: counts.append(len(flies))
        ann_file.append(flies)

        # fix any errors using hindsight
        if PRINT_COUNTS and (frame == PRINT_FRAME or PRINT_FRAME is None):
            print frame, "pre-hindsight", len(ann_file[-1])
        if cmp_list is not None: counts.append(len(ann_file[-1]))
        hindsight.fixerrors()
        if PRINT_COUNTS and (frame == PRINT_FRAME or PRINT_FRAME is None):
            print frame, "post-hindsight", len(ann_file[-1])
        if cmp_list is not None: counts.append(len(ann_file[-1]))

        try:
            test_ellipses(ann_file[-1])
        except AssertionError:
            print "**hindsight output error in frame", frame
            print_vals()
            quit_val.value = 1
            raise

        if frame % 500 == 0:
            print "__tracking", frame, "/", movie.get_n_frames()

        if quit_val.value > 0: return

        if cmp_list is not None:
            cmp_list.append((counts, ann_file[-1]))

    ann_file.close()

    at = (time.time() - fst) / max_frames
    print "avg. per frame: %.3f s" % (at)
Example #10
0
    def Track( self ):
        """Run the m-tracker."""

        ## initialization ##

        # maximum number of frames we will look back to fix errors
        self.maxlookback = max(params.lostdetection_length,
                               params.spuriousdetection_length,
                               params.mergeddetection_length,
                               params.splitdetection_length)

        # initialize annotation file
        self.ann_file = annot.AnnotationFile( self.ann_filename, params.version, True )
        self.ann_file.WriteAnnHeader( params.start_frame, self.bg_imgs )

        # if we haven't done any tracking yet
        if (self.ann_data is None) or (len(self.ann_data) == 0):
            self.ann_data = []
            params.nids = 0

        # write the current tracks to file
        for ff in range(len(self.ann_data)-self.maxlookback):
            self.ann_file.write_ellipses(self.ann_data[ff])
        self.lastframewritten = max(-1,len(self.ann_data)-self.maxlookback-1)

        wx.Yield()

        # initialize hindsight data structures
        self.hindsight = hindsight.Hindsight(self.ann_data,self.bg_imgs)
        self.hindsight.initialize_milestones()

        self.break_flag = False

        #print 'In Track, just before main loop: self.start_frame = %d, params.start_frame = %d, len(ann_data) = %d'%(self.start_frame,params.start_frame,len(self.ann_data))

        for self.start_frame in range(self.start_frame,self.n_frames):
        
            if self.break_flag:
                break

            last_time = time.time()

            # perform background subtraction
            try:
                (self.dfore,self.isfore) = self.bg_imgs.sub_bg( self.start_frame )
            except:
                break

            # write to sbfmf
            if self.dowritesbfmf:
                self.movie.writesbfmf_writeframe(self.isfore,
                                                 self.bg_imgs.curr_im,
                                                 self.bg_imgs.curr_stamp,
                                                 self.start_frame)
            
            #print 'time to perform background subtraction: '+str(time.time() - last_time)

            # process gui events
            if params.interactive:
                wx.Yield()
            if self.break_flag:
                break

            # find observations
            self.ellipses = ell.find_ellipses( self.dfore, self.isfore )

            #if params.DOBREAK:
            #    print 'Exiting at frame %d'%self.start_frame
            #    sys.exit(1)

            # process gui events
            if params.interactive:
                wx.Yield()
            if self.break_flag:
                break

            # match target identities to observations
            if len( self.ann_data ) > 1:
                flies = ell.find_flies( self.ann_data[-2],
                                        self.ann_data[-1],
                                        self.ellipses )
            elif len( self.ann_data ) == 1:
                flies = ell.find_flies( self.ann_data[0],
                                        self.ann_data[0],
                                        self.ellipses )
            else:
                flies = ell.TargetList()
                for i,obs in enumerate(self.ellipses):
                    if obs.isEmpty():
                        print 'empty observation'
                    else:
                        obs.identity = params.nids
                        flies.append(obs)
                        params.nids+=1

            # save to ann_data
            self.ann_data.append( flies )

            # fix any errors using hindsight
            self.hindsight.fixerrors()
            #print 'time to fix errors: '+str(time.time() - last_time)

            # write to file
            if (self.ann_data is not None) and \
                   (len(self.ann_data) > self.maxlookback+self.lastframewritten):
                self.lastframewritten = self.lastframewritten + 1
                self.ann_file.write_ellipses(self.ann_data[self.lastframewritten])
            #print 'In Track, after writing: self.start_frame = %d, params.start_frame = %d, len(ann_data) = %d'%(self.start_frame,params.start_frame,len(self.ann_data))

            # draw?
            if params.request_refresh or (params.do_refresh and ((self.start_frame % params.framesbetweenrefresh) == 0)):
                if params.interactive:
                    if self.start_frame:
                        self.ShowCurrentFrame()
                else:
                    sys.stderr.write("Frame %d / %d\n"%(self.start_frame,self.n_frames))
                params.request_refresh = False

            # process gui events
            if params.interactive:
                wx.Yield()
            if self.break_flag:
                break


        self.Finish()
Example #11
0
    def Track( self ):
        """Run the m-tracker."""

        ## initialization ##

        if DEBUG: print "Tracking from frame %d..."%self.start_frame
        if DEBUG: print "Last frame tracked = %d"%self.ann_file.lastframetracked


        # maximum number of frames we will look back to fix errors
        self.maxlookback = max(params.lostdetection_length,
                               params.spuriousdetection_length,
                               params.mergeddetection_length,
                               params.splitdetection_length)

        if params.interactive:
            wx.Yield()

        # initialize hindsight data structures

        if DEBUG: print "Initializing hindsight structures"
        self.hindsight = hindsight.Hindsight(self.ann_file,self.bg_imgs)
        self.hindsight.initialize_milestones()
        if DEBUG: print "Done initializing hindsight structures"

        self.break_flag = False

        if DEBUG: print "Initializing buffer for tracking"
        self.ann_file.InitializeBufferForTracking(self.start_frame)

        # initialize dfore and connected component buffer
        self.bg_imgs.set_buffer_maxnframes()

        for self.start_frame in range(self.start_frame,self.n_frames):

            if DEBUG: print "Tracking frame %d / %d"%(self.start_frame,self.n_frames-1)
        
            #if DEBUG:
            #    break

            if self.break_flag:
                break

            last_time = time.time()

            # perform background subtraction
            #try:
            (self.dfore,self.isfore,self.cc,self.ncc) = \
                self.bg_imgs.sub_bg( self.start_frame, dobuffer=True )
            #except:
            #    # catch all error types here, and just break out of loop
            #    break

            # write to sbfmf
            if self.dowritesbfmf:
                self.movie.writesbfmf_writeframe(self.isfore,
                                                 self.bg_imgs.curr_im,
                                                 self.bg_imgs.curr_stamp,
                                                 self.start_frame)
            
            # process gui events
            if params.interactive:
                wx.Yield()
            if self.break_flag:
                break

            # find observations
            self.ellipses = ell.find_ellipses( self.dfore, self.cc, self.ncc )

            #if params.DOBREAK:
            #    print 'Exiting at frame %d'%self.start_frame
            #    sys.exit(1)

            # process gui events
            if params.interactive:
                wx.Yield()
            if self.break_flag:
                break

            # match target identities to observations
            if len( self.ann_file ) > 1:
                flies = ell.find_flies( self.ann_file[-2],
                                        self.ann_file[-1],
                                        self.ellipses,
                                        self.ann_file)
            elif len( self.ann_file ) == 1:
                flies = ell.find_flies( self.ann_file[0],
                                        self.ann_file[0],
                                        self.ellipses,
                                        self.ann_file)
            else:
                flies = ell.TargetList()
                for i,obs in enumerate(self.ellipses):
                    if obs.isEmpty():
                        if DEBUG: print 'empty observation'
                    else:
                        newid = self.ann_file.GetNewId()
                        obs.identity = newid
                        flies.append(obs)

            if DEBUG: print "Done with frame %d, appending to ann_file"%self.start_frame

            # save to ann_data
            self.ann_file.append( flies )

            if DEBUG: print "Added to ann_file, now running fixerrors"

            # fix any errors using hindsight
            self.hindsight.fixerrors()
            #print 'time to fix errors: '+str(time.time() - last_time)

            

            # draw?
            if params.request_refresh or (params.do_refresh and ((self.start_frame % params.framesbetweenrefresh) == 0)):
                if params.interactive:
                    if self.start_frame:
                        self.ShowCurrentFrame()
                else:
                    print "Frame %d / %d\n"%(self.start_frame,self.n_frames)
                params.request_refresh = False

            # process gui events
            if params.interactive:
                wx.Yield()
            if self.break_flag:
                break


        self.Finish()
Example #12
0
    def Track(self):
        """Run the m-tracker."""

        ## initialization ##

        if DEBUG: print "Tracking from frame %d..." % self.start_frame
        if DEBUG:
            print "Last frame tracked = %d" % self.ann_file.lastframetracked

        print "YL:"
        print " bg_type", self.bg_imgs.bg_type
        print " norm_type", self.bg_imgs.norm_type
        print " thresh", params.n_bg_std_thresh, params.n_bg_std_thresh_low
        print " area", params.maxshape.area, params.minshape.area
        print " max_jump", params.max_jump, params.max_jump_split
        print " use_shadow_detector", params.use_shadow_detector
        print " recalc_bg_minutes", params.recalc_bg_minutes
        print " recalc_n_frames", self.movie.recalc_n_frames()
        print " fps %.1f" % self.movie.get_fps()
        strt = time.clock()

        if params.use_shadow_detector:
            fx, fy = self.matchTemplate()
            print " fx, fy", fx, fy
            bx = (fx[0] + fx[1]) / 2

        # maximum number of frames we will look back to fix errors
        self.maxlookback = max(params.lostdetection_length,
                               params.spuriousdetection_length,
                               params.mergeddetection_length,
                               params.splitdetection_length)

        if params.interactive:
            wx.Yield()

        # initialize hindsight data structures
        self.hindsight = hindsight.Hindsight(self.ann_file, self.bg_imgs)

        self.break_flag = False

        if DEBUG: print "Initializing buffer for tracking"
        self.ann_file.InitializeBufferForTracking(self.start_frame)

        # initialize dfore and connected component buffer
        self.bg_imgs.set_buffer_maxnframes()

        rc, rnf, bgs, nf = 0, self.movie.recalc_n_frames(
        ), [], self.movie.get_n_frames()

        def appendBg():
            bgRw = self.bg_imgs.centers if self.bg_imgs.varying_bg else [
                self.bg_imgs.center
            ]
            bgs.append(
                dict(bgs=[bg.astype(num.float32) for bg in bgRw],
                     varying_bg=self.bg_imgs.varying_bg,
                     mean_separator=self.bg_imgs.mean_separator))

        appendBg()
        for self.start_frame in range(self.start_frame, nf):

            # KB 20120109 added last_frame command-line option
            if self.start_frame >= self.last_frame:
                break

            if DEBUG_LEVEL > 0:
                print "Tracking frame %d / %d" % (self.start_frame, nf - 1)

            #if DEBUG:
            #    break

            if self.break_flag:
                break

            last_time = time.time()

            # recalculate background?
            rc += 1
            if rnf > 0 and rc > rnf:
                if nf - self.start_frame > rnf / 2:
                    assert self.start_frame % rnf == 0
                    # note: makes it easy to calculate which background was used;
                    #  not required for tracking
                    self.bg_imgs.bg_firstframe = self.start_frame
                    self.bg_imgs.bg_lastframe = self.start_frame + rnf - 1
                    self.OnComputeBg()
                    appendBg()
                rc = 1

            # perform background subtraction
            #try:
            (self.dfore,self.isfore,self.cc,self.ncc) = \
                self.bg_imgs.sub_bg( self.start_frame, dobuffer=True )
            #except:
            #    # catch all error types here, and just break out of loop
            #    break

            # write to sbfmf
            if self.dowritesbfmf:
                self.movie.writesbfmf_writeframe(self.isfore,
                                                 self.bg_imgs.curr_im,
                                                 self.bg_imgs.curr_stamp,
                                                 self.start_frame)

            # process gui events
            if params.interactive:
                wx.Yield()
            if self.break_flag:
                break

            # find observations
            self.ellipses = ell.find_ellipses(self.dfore, self.cc, self.ncc)

            # shadow detector
            if params.use_shadow_detector:
                ne = 2 * [0]  # idx: left, right (chamber)
                bei, bdist, bgood = 2 * [-1], 2 * [1000], 2 * [None]  # best
                for ei, e in enumerate(self.ellipses):
                    if e.area < params.minshape.area:
                        continue
                    good = e.area >= params.shadow_detector_minarea and (
                        not e.merged_areas or any(a >= params.minshape.area
                                                  for a in e.merged_areas))
                    i = e.center.x > bx
                    ne[i] += 1
                    dist = num.sqrt((e.center.x - fx[i])**2 +
                                    (e.center.y - fy[i])**2)
                    if bei[i] < 0 or good and not bgood[
                            i] or dist < bdist[i] and good == bgood[i]:
                        bei[i], bdist[i], bgood[i] = ei, dist, good
                #print "l:%d r:%d" %(ne)

                # keep only non-shadow ellipses
                numEll = len(self.ellipses)
                self.ellipses = [self.ellipses[i] for i in bei if i >= 0]
                if len(self.ellipses) < numEll:
                    print ">>> kept only", bei

            #if params.DOBREAK:
            #    print 'Exiting at frame %d'%self.start_frame
            #    sys.exit(1)

            # process gui events
            if params.interactive:
                wx.Yield()
            if self.break_flag:
                break

            # match target identities to observations
            if len(self.ann_file) > 1:
                flies = ell.find_flies(self.ann_file[-2], self.ann_file[-1],
                                       self.ellipses, self.ann_file)
            elif len(self.ann_file) == 1:
                flies = ell.find_flies(self.ann_file[-1], self.ann_file[-1],
                                       self.ellipses, self.ann_file)
            else:
                flies = ell.TargetList()
                for i, obs in enumerate(self.ellipses):
                    if obs.isEmpty():
                        if DEBUG: print 'empty observation'
                    else:
                        newid = self.ann_file.GetNewId()
                        obs.identity = newid
                        flies.append(obs)

            if DEBUG_LEVEL > 0:
                print "Done with frame %d, appending to ann_file" % self.start_frame

            # save to ann_data
            self.ann_file.append(flies)

            if DEBUG_LEVEL > 0:
                print "Added to ann_file, now running fixerrors"

            # fix any errors using hindsight
            self.hindsight.fixerrors()
            #print 'time to fix errors: '+str(time.time() - last_time)

            # draw?
            if self.request_refresh or (self.do_refresh and (
                (self.start_frame % self.framesbetweenrefresh) == 0)):
                if params.interactive:
                    if self.start_frame:
                        self.ShowCurrentFrame()
                else:
                    on = ("on " if self.bg_imgs.on else
                          "off ") if self.bg_imgs.varying_bg else ""
                    print "    Frame %d / %d %s[%ds]" \
                        %(self.start_frame, nf, on, time.clock()-strt)
                self.request_refresh = False

            # process gui events
            if params.interactive:
                wx.Yield()
            if self.break_flag:
                break

            if (self.start_frame %
                    100) == 0 and self.has('diagnostics_filename'):
                self.write_diagnostics()  # save ongoing

        self.saveBackgrounds(bgs)
        self.Finish()
Example #13
0
    def Track(self):
        """Run the tracker."""

        ## initialization ##
        if DEBUG: print "Tracking from frame %d..." % self.start_frame
        if DEBUG:
            print "Last frame tracked = %d" % self.ann_file.lastframetracked

        # maximum number of frames we will look back to fix errors
        self.maxlookback = max(params.lostdetection_length,
                               params.spuriousdetection_length,
                               params.mergeddetection_length,
                               params.splitdetection_length)

        if params.interactive:
            wx.Yield()

        # initialize hindsight data structures
        self.hindsight = hindsight.Hindsight(self.ann_file, self.bg_imgs)

        # initialize dfore and connected component buffer
        self.bg_imgs.set_buffer_maxnframes()

        self.track_timer_start_time = time.time()
        self.update_track_time()

        self.break_flag = False

        for self.start_frame in range(self.start_frame,
                                      self.movie.get_n_frames()):
            if DEBUG or DEBUG_TRACKINGSETTINGS: print "frame", self.start_frame

            # KB 20120109 added last_frame command-line option
            if self.start_frame >= self.last_frame:
                break

            if DEBUG_LEVEL > 0:
                print "Tracking frame %d / %d" % (
                    self.start_frame, self.movie.get_n_frames() - 1)

            if self.break_flag:
                break

            last_time = time.time()

            # perform background subtraction
            try:
                (dfore, isfore, cc,
                 ncc) = self.bg_imgs.sub_bg(self.start_frame)
            except IOError:
                if self.movie.type == 'cavi' and self.start_frame >= self.movie.get_n_frames(
                ) - 2:
                    # last frame or so wasn't present, no problem
                    print "ignoring compressed AVI IOError reading last frame"
                else:
                    traceback.print_exc()
                break
            except:
                # catch all error types here, and just break out of the loop
                traceback.print_exc()
                break
            if DEBUG_LEVEL > 1: print ncc, "connected components"

            # write to sbfmf
            if self.dowritesbfmf:
                self.movie.writesbfmf_writeframe(isfore, self.bg_imgs.curr_im,
                                                 self.bg_imgs.curr_stamp,
                                                 self.start_frame)

            # process gui events
            if params.interactive:
                wx.Yield()
            if self.break_flag:
                break

            # find observations
            ellipses = ell.find_ellipses(dfore, cc, ncc)
            if DEBUG_LEVEL > 1: print len(ellipses), "ellipses"

            # process gui events
            if params.interactive:
                wx.Yield()
            if self.break_flag:
                break

            # match target identities to observations
            if DEBUG_LEVEL > 0: print "matching identities"
            if len(self.ann_file) > 1:
                flies = ell.find_flies(self.ann_file[-2], self.ann_file[-1],
                                       ellipses, self.ann_file)
            elif len(self.ann_file) == 1:
                flies = ell.find_flies(self.ann_file[-1], self.ann_file[-1],
                                       ellipses, self.ann_file)
            else:
                flies = ell.TargetList()
                for i, obs in enumerate(ellipses):
                    if obs.isEmpty():
                        if DEBUG: print 'empty observation'
                    else:
                        newid = self.ann_file.GetNewId()
                        obs.identity = newid
                        flies.append(obs)
            if DEBUG_LEVEL > 1: print len(flies), "flies"

            if DEBUG_LEVEL > 0:
                print "Done with frame %d, appending to ann_file" % self.start_frame

            # save to ann_data
            self.ann_file.append(flies)

            # fix any errors using hindsight
            if DEBUG_LEVEL > 0:
                print "Added to ann_file, now running fixerrors"
            self.hindsight.fixerrors()

            # draw?
            if self.request_refresh or (self.do_refresh and (
                (self.start_frame % self.framesbetweenrefresh) == 0)):
                if params.interactive:
                    if self.start_frame:
                        self.ShowCurrentFrame()
                else:
                    print "    Frame %d / %d" % (self.start_frame,
                                                 self.movie.get_n_frames() - 1)
                self.request_refresh = False
            if DEBUG_LEVEL > 1:
                print len(self.ann_file[-1]), "flies in last frame"

            # process gui events
            if params.interactive:
                wx.Yield()
            if self.break_flag:
                break

            if (self.start_frame %
                    100) == 0 and self.has('diagnostics_filename'):
                self.write_diagnostics()  # save ongoing

        if hasattr(self, 'timer'):
            # timer.cancel() should work, but in case it doesn't, set a flag too
            self.timer.cancel()
            self.track_timer_cancel = True

        self.Finish()
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():
#        est.drawellipse(e,colors[id])
#    mpl.show()

tracks = []
cc = []
obs = ell.find_ellipses(bw[0].astype(float),bw[0],False)
(L,ncc) = meas.label(bw[0])
cc.append(L)
tracks.append(ell.TargetList())
for (i,o) in enumerate(obs):
    o.identity = i
    tracks[0].append(o)
params.nids = len(tracks[0])

for t in range(1,len(bw)):
    obs = ell.find_ellipses(bw[t].astype(float),bw[t],False)
    nids = params.nids
    (L,ncc) = meas.label(bw[t])
    if len(tracks) == 1:
        (targ,obs_for_target,unass_obs) = find_flies(tracks[-1],tracks[-1],obs)
    else:
Example #15
0
    def get_sub_image( self, sub_type, framenumber ):
        """Do background subtraction and return a particular image type."""
        wx.BeginBusyCursor()
        try: wx.Yield()
        except: pass # sometimes complains about recursive Yield()
        
        # do background subtraction if necessary
        if sub_type in [params.SHOW_DISTANCE,params.SHOW_THRESH,
                        params.SHOW_CC,params.SHOW_ELLIPSES,
                        params.SHOW_BACKGROUND,params.SHOW_DEV,
                        params.SHOW_BG_AND_DIST]:
            (dfore,bw,cc,ncc) = self.sub_bg( framenumber )   # sets self.on, etc.
        else:
            if hasattr( self, 'dfore' ):
                dfore = self.dfore
                bw = self.bw
                cc = self.cc
                ncc = self.ncc
            else:
                dfore = None
                bw = None
                cc = None
                ncc = None

        line_segs = []
        line_clrs = []
        img_format = None

        # format image in "subtraction type"
        if sub_type == params.SHOW_BACKGROUND:
            cntr = self.centers[self.on] if self.varying_bg else self.center
            img_8 = imagesk.double2mono8(cntr,donormalize=False)
            img_format = 'MONO8'
            preview_range = (0,255)
        elif sub_type == params.SHOW_DISTANCE:
            img_8 = imagesk.double2mono8(dfore)
            img_format = 'MONO8'
            preview_range = (dfore.min(), dfore.max())
        elif sub_type == params.SHOW_THRESH:
            img_8 = imagesk.double2mono8(bw.astype(float))
            img_format = 'MONO8'
            preview_range = (0,1)
        elif sub_type == params.SHOW_NONFORE:
            isnotarena = (self.isarena == False)
            img_8 = imagesk.double2mono8(isnotarena.astype(float))
            img_format = 'MONO8'
            preview_range = (0,1)
        elif sub_type == params.SHOW_DEV:
            dev = self.devs[self.on] if self.varying_bg else self.dev
            mu = num.mean(dev)
            sig = num.std(dev)
            if sig == 0:
                img_8 = imagesk.double2mono8(dev)
                n1 = mu
                n2 = mu
            else:
                n1 = max(0,mu - 3.*sig)
                n2 = mu + 3.*sig
                img_8 = imagesk.double2mono8(dev.clip(n1,n2))
            img_format = 'MONO8'
            preview_range = (n1,n2)
        elif sub_type == params.SHOW_CC:
            img_8,clim = colormap_image(cc)
            img_format = 'RGB8'
            preview_range = clim
        elif sub_type == params.SHOW_ELLIPSES:
            # find ellipse observations
            obs = find_ellipses(dfore,cc,ncc,False)
            im, stamp = params.movie.get_frame( int(framenumber) )
            img_8 = imagesk.double2mono8(im,donormalize=False)
            plot_linesegs = draw_ellipses(obs)
            (linesegs,linecolors) = \
                imagesk.separate_linesegs_colors(plot_linesegs)
            img_8 = imagesk.double2mono8(im,donormalize=False)
            img_format = 'MONO8'
            preview_range = (0,255)
            line_segs.extend( linesegs )
            line_clrs.extend( linecolors )
        elif sub_type == params.SHOW_BG_AND_DIST:
            cntr = self.centers[self.on] if self.varying_bg else self.center
            img = Image.fromarray(imagesk.double2mono8(cntr,donormalize=False)).convert('RGB')
            img.paste((0,255,0), None, Image.fromarray(imagesk.double2mono8(dfore)))
            img_8 = num.array(img)
            img_format = 'RGB8'
            preview_range = (0,255)
        elif sub_type in params.SHOW_EXPBGFGMODEL: 
            # check for expbgfgmodel
            if not params.use_expbgfgmodel or not hasattr(self,'expbgfgmodel') or \
            self.expbgfgmodel is None:
                resp = wx.MessageBox("No prior model loaded.","No prior model loaded",wx.OK)
            else:
                if sub_type == params.SHOW_EXPBGFGMODEL_LLR:
                    # read the current image, classify pixels
                    im, stamp = params.movie.get_frame( int(framenumber) )
                    llr = self.expbgfgmodel_llr(im)
                    img_8,clim = colormap_image(llr)
                    img_format = 'RGB'
                    preview_range = clim
                elif sub_type == params.SHOW_EXPBGFGMODEL_ISBACK:
                    im, stamp = params.movie.get_frame( int(framenumber) )
                    isback = self.thresh_expbgfgmodel_llr(im)
                    img_8 = imagesk.double2mono8(isback.astype(float))
                    img_format = 'MONO8'
                    preview_range = (0,1)
                elif sub_type == params.SHOW_EXPBGFGMODEL_BGMU:
                    img_8 = imagesk.double2mono8(self.expbgfgmodel.bg_mu,donormalize=False)
                    img_format = 'MONO8'
                    preview_range = (0,255)
                elif sub_type == params.SHOW_EXPBGFGMODEL_FGMU:
                    img_8 = imagesk.double2mono8(self.expbgfgmodel.fg_mu,donormalize=False)
                    img_format = 'MONO8'
                    preview_range = (0,255)
                elif sub_type == params.SHOW_EXPBGFGMODEL_BGSIGMA:
                    mu = num.mean(self.expbgfgmodel.bg_sigma)
                    sig = num.std(self.expbgfgmodel.bg_sigma)
                    if sig == 0:
                        n1 = mu
                        n2 = mu
                        img_8 = imagesk.double2mono8(self.expbgfgmodel.bg_sigma)
                    else:
                        n1 = max(0,mu - 3.*sig)
                        n2 = mu + 3.*sig
                        img_8 = imagesk.double2mono8(self.expbgfgmodel.bg_sigma.clip(n1,n2))
                    img_format = 'MONO8'
                    preview_range = (n1,n2)
                elif sub_type == params.SHOW_EXPBGFGMODEL_FGSIGMA:
                    mu = num.mean(self.expbgfgmodel.fg_sigma)
                    sig = num.std(self.expbgfgmodel.fg_sigma)
                    if sig == 0:
                        img_8 = imagesk.double2mono8(self.expbgfgmodel.fg_sigma)
                        n1 = mu
                        n2 = mu
                    else:
                        n1 = max(0,mu - 3.*sig)
                        n2 = mu + 3.*sig
                        img_8 = imagesk.double2mono8(self.expbgfgmodel.fg_sigma.clip(n1,n2))
                    img_format = 'MONO8'
                    preview_range = (n1,n2)
                elif sub_type == params.SHOW_EXPBGFGMODEL_FRACFRAMESISBACK:
                    if not hasattr(self,'fracframesisback') or self.fracframesisback is None:
                        resp = wx.MessageBox("fracframesisback not yet computed","fracframesisback Not Computed",wx.OK)
                        return
                    img_8,clim = colormap_image(self.fracframesisback)
                    img_format = 'RGB8'
                    preview_range = clim
                elif sub_type == params.SHOW_EXPBGFGMODEL_MISSINGDATA:
                    if not hasattr(self,'fracframesisback') or self.fracframesisback is None:
                        resp = wx.MessageBox("fracframesisback not yet computed","fracframesisback Not Computed",wx.OK)
                        return
                    missingdata = self.fracframesisback <= params.min_frac_frames_isback
                    img_8 = imagesk.double2mono8(missingdata.astype(float))
                    img_format = 'MONO8'
                    preview_range = (0,1)

        wx.EndBusyCursor()

        return img_8, img_format, line_segs, line_clrs, dfore, bw, cc, ncc, preview_range
Example #16
0
#    mpl.imshow(bw[t])
#    for (id,e) in tracks_true[t].iteritems():
#        est.drawellipse(e,colors[id])
#    mpl.show()

try:
    os.remove('tmp.ann')
except:
    pass
tracks = ann.AnnotationFile('tmp.ann', None, True, False, False)
tracks.InitializeData(0, -1)
tracks.InitializeBufferForTracking(0)

#tracks = []
cc = []
obs = ell.find_ellipses(bw[0].astype(float), bw[0], False)
(L, ncc) = meas.label(bw[0])
cc.append(L)
tracks.append(ell.TargetList())
for (i, o) in enumerate(obs):
    o.identity = i
    tracks[0].append(o)
params.nids = len(tracks[0])

for t in range(1, len(bw)):
    obs = ell.find_ellipses(bw[t].astype(float), bw[t], False)
    nids = params.nids
    (L, ncc) = meas.label(bw[t])
    if len(tracks) == 1:
        (targ, obs_for_target, unass_obs) = find_flies(tracks[-1], tracks[-1],
                                                       obs)
Example #17
0
    def get_sub_image(self, sub_type, framenumber):
        """Do background subtraction and return a particular image type."""
        wx.BeginBusyCursor()
        try:
            wx.Yield()
        except:
            pass  # sometimes complains about recursive Yield()

        # do background subtraction if necessary
        if sub_type in [
                params.SHOW_DISTANCE, params.SHOW_THRESH, params.SHOW_CC,
                params.SHOW_ELLIPSES, params.SHOW_BACKGROUND, params.SHOW_DEV,
                params.SHOW_BG_AND_DIST
        ]:
            (dfore, bw, cc,
             ncc) = self.sub_bg(framenumber)  # sets self.on, etc.
        else:
            if hasattr(self, 'dfore'):
                dfore = self.dfore
                bw = self.bw
                cc = self.cc
                ncc = self.ncc
            else:
                dfore = None
                bw = None
                cc = None
                ncc = None

        line_segs = []
        line_clrs = []
        img_format = None

        # format image in "subtraction type"
        if sub_type == params.SHOW_BACKGROUND:
            cntr = self.centers[self.on] if self.varying_bg else self.center
            img_8 = imagesk.double2mono8(cntr, donormalize=False)
            img_format = 'MONO8'
            preview_range = (0, 255)
        elif sub_type == params.SHOW_DISTANCE:
            img_8 = imagesk.double2mono8(dfore)
            img_format = 'MONO8'
            preview_range = (dfore.min(), dfore.max())
        elif sub_type == params.SHOW_THRESH:
            img_8 = imagesk.double2mono8(bw.astype(float))
            img_format = 'MONO8'
            preview_range = (0, 1)
        elif sub_type == params.SHOW_NONFORE:
            isnotarena = (self.isarena == False)
            img_8 = imagesk.double2mono8(isnotarena.astype(float))
            img_format = 'MONO8'
            preview_range = (0, 1)
        elif sub_type == params.SHOW_DEV:
            dev = self.devs[self.on] if self.varying_bg else self.dev
            mu = num.mean(dev)
            sig = num.std(dev)
            if sig == 0:
                img_8 = imagesk.double2mono8(dev)
                n1 = mu
                n2 = mu
            else:
                n1 = max(0, mu - 3. * sig)
                n2 = mu + 3. * sig
                img_8 = imagesk.double2mono8(dev.clip(n1, n2))
            img_format = 'MONO8'
            preview_range = (n1, n2)
        elif sub_type == params.SHOW_CC:
            img_8, clim = colormap_image(cc)
            img_format = 'RGB8'
            preview_range = clim
        elif sub_type == params.SHOW_ELLIPSES:
            # find ellipse observations
            obs = find_ellipses(dfore, cc, ncc, False)
            im, stamp = params.movie.get_frame(int(framenumber))
            img_8 = imagesk.double2mono8(im, donormalize=False)
            plot_linesegs = draw_ellipses(obs)
            (linesegs,linecolors) = \
                imagesk.separate_linesegs_colors(plot_linesegs)
            img_8 = imagesk.double2mono8(im, donormalize=False)
            img_format = 'MONO8'
            preview_range = (0, 255)
            line_segs.extend(linesegs)
            line_clrs.extend(linecolors)
        elif sub_type == params.SHOW_BG_AND_DIST:
            cntr = self.centers[self.on] if self.varying_bg else self.center
            img = Image.fromarray(imagesk.double2mono8(
                cntr, donormalize=False)).convert('RGB')
            img.paste((0, 255, 0), None,
                      Image.fromarray(imagesk.double2mono8(dfore)))
            img_8 = num.array(img)
            img_format = 'RGB8'
            preview_range = (0, 255)
        elif sub_type in params.SHOW_EXPBGFGMODEL:
            # check for expbgfgmodel
            if not params.use_expbgfgmodel or not hasattr(self,'expbgfgmodel') or \
            self.expbgfgmodel is None:
                resp = wx.MessageBox("No prior model loaded.",
                                     "No prior model loaded", wx.OK)
            else:
                if sub_type == params.SHOW_EXPBGFGMODEL_LLR:
                    # read the current image, classify pixels
                    im, stamp = params.movie.get_frame(int(framenumber))
                    llr = self.expbgfgmodel_llr(im)
                    img_8, clim = colormap_image(llr)
                    img_format = 'RGB'
                    preview_range = clim
                elif sub_type == params.SHOW_EXPBGFGMODEL_ISBACK:
                    im, stamp = params.movie.get_frame(int(framenumber))
                    isback = self.thresh_expbgfgmodel_llr(im)
                    img_8 = imagesk.double2mono8(isback.astype(float))
                    img_format = 'MONO8'
                    preview_range = (0, 1)
                elif sub_type == params.SHOW_EXPBGFGMODEL_BGMU:
                    img_8 = imagesk.double2mono8(self.expbgfgmodel.bg_mu,
                                                 donormalize=False)
                    img_format = 'MONO8'
                    preview_range = (0, 255)
                elif sub_type == params.SHOW_EXPBGFGMODEL_FGMU:
                    img_8 = imagesk.double2mono8(self.expbgfgmodel.fg_mu,
                                                 donormalize=False)
                    img_format = 'MONO8'
                    preview_range = (0, 255)
                elif sub_type == params.SHOW_EXPBGFGMODEL_BGSIGMA:
                    mu = num.mean(self.expbgfgmodel.bg_sigma)
                    sig = num.std(self.expbgfgmodel.bg_sigma)
                    if sig == 0:
                        n1 = mu
                        n2 = mu
                        img_8 = imagesk.double2mono8(
                            self.expbgfgmodel.bg_sigma)
                    else:
                        n1 = max(0, mu - 3. * sig)
                        n2 = mu + 3. * sig
                        img_8 = imagesk.double2mono8(
                            self.expbgfgmodel.bg_sigma.clip(n1, n2))
                    img_format = 'MONO8'
                    preview_range = (n1, n2)
                elif sub_type == params.SHOW_EXPBGFGMODEL_FGSIGMA:
                    mu = num.mean(self.expbgfgmodel.fg_sigma)
                    sig = num.std(self.expbgfgmodel.fg_sigma)
                    if sig == 0:
                        img_8 = imagesk.double2mono8(
                            self.expbgfgmodel.fg_sigma)
                        n1 = mu
                        n2 = mu
                    else:
                        n1 = max(0, mu - 3. * sig)
                        n2 = mu + 3. * sig
                        img_8 = imagesk.double2mono8(
                            self.expbgfgmodel.fg_sigma.clip(n1, n2))
                    img_format = 'MONO8'
                    preview_range = (n1, n2)
                elif sub_type == params.SHOW_EXPBGFGMODEL_FRACFRAMESISBACK:
                    if not hasattr(self, 'fracframesisback'
                                   ) or self.fracframesisback is None:
                        resp = wx.MessageBox(
                            "fracframesisback not yet computed",
                            "fracframesisback Not Computed", wx.OK)
                        return
                    img_8, clim = colormap_image(self.fracframesisback)
                    img_format = 'RGB8'
                    preview_range = clim
                elif sub_type == params.SHOW_EXPBGFGMODEL_MISSINGDATA:
                    if not hasattr(self, 'fracframesisback'
                                   ) or self.fracframesisback is None:
                        resp = wx.MessageBox(
                            "fracframesisback not yet computed",
                            "fracframesisback Not Computed", wx.OK)
                        return
                    missingdata = self.fracframesisback <= params.min_frac_frames_isback
                    img_8 = imagesk.double2mono8(missingdata.astype(float))
                    img_format = 'MONO8'
                    preview_range = (0, 1)

        wx.EndBusyCursor()

        return img_8, img_format, line_segs, line_clrs, dfore, bw, cc, ncc, preview_range