def init_objects(stream,frames,currsum,denom,seglen,cutoff,frames_offset,SHAPE,size_h,size_bins,fol_h,fol_bins,transform='',outline_engine='homebrew'): hsl = seglen/2 min_arc_score = (2*max(size_h))+max(fol_h) #score of a "perfect" object arc of length 2 ols = [] last_frames = [] ols_offset = frames_offset + hsl c = itertools.cycle(['|','/','-','\\']) t = time.time() print >> sys.stderr, 'analyze %s frames:' % seglen for i in xrange(seglen): last_frames.append(frames[0]) frames_offset += 1 mm = vidtools.shift_frames_return_diff(stream,frames,currsum,denom,seglen,transform=transform) if outline_engine == 'homebrew': ol = vidtools.chain_outlines_from_mask(mm>cutoff,preshrink=1,debug=False,return_termini=False,order_points=True,sort_outlines=False) #order_points should be True elif outline_engine == 'shapely': ol = vidtools.chain_outlines_from_mask_shapely(mm>cutoff,preshrink=1) else: print >> sys.stderr, 'outline_engine must be one of %s' % (['homebrew','shapely']) raise ValueError ols.append(ol) print >> sys.stderr, '\r%s %s ' % (i,c.next()), print >> sys.stderr, 'done in', str(datetime.timedelta(seconds=int(time.time() - t))) objs = {} splits = defaultdict(list) objs_sizes = {} objs_fols = {} print >> sys.stderr, '\nperform initial object arc tracking on %s frames' % len(ols) #one round of object tracking; assumes next object tracking call will start a single frame from now to_retire_objs, to_retire_objs_sizes, to_retire_objs_fols = vidtools.find_objs_progressive(ols, ols_offset, ols_offset, ols_offset+1, SHAPE, objs, splits, \ objs_sizes, objs_fols, size_h,size_bins,fol_h,fol_bins) prelast_avg = vidtools.average_frames(last_frames[hsl:]) prelast_mm = vidtools.mousemask_from_object_arcs(frames_offset-hsl,frames_offset,min_arc_score,ols,ols_offset, \ Util.merge_dictlist([objs,to_retire_objs]), \ Util.merge_dictlist([objs_sizes,to_retire_objs_sizes]), \ Util.merge_dictlist([objs_fols,to_retire_objs_fols]), \ size_h, size_bins, fol_h, fol_bins,SHAPE) print >> sys.stderr, 'object initialization complete in', str(datetime.timedelta(seconds=int(time.time() - t))) return ols,ols_offset,frames_offset,objs,splits,objs_sizes,objs_fols,prelast_avg,prelast_mm,to_retire_objs,to_retire_objs_sizes,to_retire_objs_fols
bkgd_images = sorted(glob(imagedir + '/*.png'))[stop:stop + (stop - start)] frames = vidtools.load_normed_arrays(images, pixav) #print >> sys.stderr, 'time averaging invoked, window of %s frames' % (timeav) #frames = Util.zsmooth_convol(frames,timeav) #windowed_frames = vidtools.average_frames(frames,num_wins=wins) #frameav = vidtools.average_frames(windowed_frames) ''' try: frameav = vidtools.average_frames(bkgd_images,pixav) except IndexError: frameav = vidtools.average_frames(frames) ''' frameav = vidtools.average_frames(frames) SHAPE = frames[0].shape print >> sys.stderr, 'shape:', SHAPE if eval(maskfile) is not None: mask = numpy.fromfile(maskfile, dtype=bool).reshape(SHAPE) if ybounds is None: ybounds = (0, SHAPE[0]) #mice = dict([(images[i],vidtools.find_mouse(f[ybounds[0]:ybounds[1]],frameav[ybounds[0]:ybounds[1]],zcut=mousezcut,abs_val=True)) for i,f in enumerate(frames)]) mice = [] miceouts = [] for i, f in enumerate(frames):
boundmask[ty:by,tx:bx] = False boundmask += mask #piggybacking the mask subtraction for supplied maskfile on groundmask print >> sys.stderr, 'masking frames to %s' % opts['xybounds'] step = len(frames)/10 for i,f in enumerate(frames): frames[i] = Util.subtract_mask(f,boundmask,frames[i][ty:by,tx:bx].min()) if i % step == 0: print >> sys.stderr, 'frame %s done' % i #consider not replacing frames; keep a non-averaged copy to trace mice on for .micepoly files... if timeav: print >> sys.stderr, 'time averaging invoked, window of %s frames' % (timeav) frames = Util.zsmooth_convol(frames,timeav) frameav = vidtools.average_frames(frames) mouse_grow_by = 0 mouse_preshrink = 2 miceli = [] miceoutli = [] micez = [] micesize = [] micelen = [] print >>sys.stderr,'processing %s frames' % len(frames) tick = int(len(frames)/10) if opts['ground_anchors'] is None and opts['burrowz'] is None: print >> sys.stderr, 'both ground_anchors and burrowz set None; skipping mouse eraser' elif opts['skip_pre_erase']:
last_segavg_file = '%07d-%07d-segavg.mat' % (frames_offset,frames_offset+hsl) if opts.antfarm_config: last_prevactols_file = '%07d-%07d-prevact_ols.list' % (frames_offset,frames_offset+hsl) last_newactols_file = '%07d-%07d-newact_ols.list' % (frames_offset,frames_offset+hsl) last_ground_file = '%07d-%07d-ground.list' % (frames_offset,frames_offset+hsl) last_digdiff_file = '%07d-%07d-digdiff.mat' % (frames_offset,frames_offset+hsl) t = time.time() #eventually, choice to reload previous analysis goes here last_frames = [] while len(last_frames) < hsl: ols_offset, frames_offset = advance_analysis(ols,ols_offset,objs,splits,objs_sizes,objs_fols, \ to_retire_objs,to_retire_objs_sizes,to_retire_objs_fols, \ last_frames,stream,frames,currsum,denom,opts.seglen,cutoff,frames_offset, \ SHAPE,size_h,size_bins,fol_h,fol_bins,transform=opts.transform,outline_engine=opts.outline_engine) last_avg = vidtools.average_frames(last_frames) last_mm = vidtools.mousemask_from_object_arcs(frames_offset-len(last_frames),frames_offset,min_arc_score,ols,ols_offset, \ Util.merge_dictlist([objs,to_retire_objs]), \ Util.merge_dictlist([objs_sizes,to_retire_objs_sizes]), \ Util.merge_dictlist([objs_fols,to_retire_objs_fols]), \ size_h, size_bins, fol_h, fol_bins,SHAPE) this_avg = vidtools.average_frames(frames[:hsl]) this_mm = vidtools.mousemask_from_object_arcs(frames_offset,frames_offset+hsl,min_arc_score,ols,ols_offset, \ Util.merge_dictlist([objs,to_retire_objs]), \ Util.merge_dictlist([objs_sizes,to_retire_objs_sizes]), \ Util.merge_dictlist([objs_fols,to_retire_objs_fols]), \ size_h, size_bins, fol_h, fol_bins,SHAPE) prelast_masked = prelast_avg.copy() prelast_masked[prelast_mm] = numpy.mean(prelast_avg[:50,:50]) last_masked = last_avg.copy() last_masked[last_mm] = numpy.mean(last_avg[:50,:50])
def init_objects(stream, frames, currsum, denom, seglen, cutoff, frames_offset, SHAPE, size_h, size_bins, fol_h, fol_bins, transform='', outline_engine='homebrew'): hsl = seglen / 2 min_arc_score = (2 * max(size_h)) + max( fol_h) #score of a "perfect" object arc of length 2 ols = [] last_frames = [] ols_offset = frames_offset + hsl c = itertools.cycle(['|', '/', '-', '\\']) t = time.time() print >> sys.stderr, 'analyze %s frames:' % seglen for i in xrange(seglen): last_frames.append(frames[0]) frames_offset += 1 mm = vidtools.shift_frames_return_diff(stream, frames, currsum, denom, seglen, transform=transform) if outline_engine == 'homebrew': ol = vidtools.chain_outlines_from_mask( mm > cutoff, preshrink=1, debug=False, return_termini=False, order_points=True, sort_outlines=False) #order_points should be True elif outline_engine == 'shapely': ol = vidtools.chain_outlines_from_mask_shapely(mm > cutoff, preshrink=1) else: print >> sys.stderr, 'outline_engine must be one of %s' % ( ['homebrew', 'shapely']) raise ValueError ols.append(ol) print >> sys.stderr, '\r%s %s ' % (i, c.next()), print >> sys.stderr, 'done in', str( datetime.timedelta(seconds=int(time.time() - t))) objs = {} splits = defaultdict(list) objs_sizes = {} objs_fols = {} print >> sys.stderr, '\nperform initial object arc tracking on %s frames' % len( ols) #one round of object tracking; assumes next object tracking call will start a single frame from now to_retire_objs, to_retire_objs_sizes, to_retire_objs_fols = vidtools.find_objs_progressive(ols, ols_offset, ols_offset, ols_offset+1, SHAPE, objs, splits, \ objs_sizes, objs_fols, size_h,size_bins,fol_h,fol_bins) prelast_avg = vidtools.average_frames(last_frames[hsl:]) prelast_mm = vidtools.mousemask_from_object_arcs(frames_offset-hsl,frames_offset,min_arc_score,ols,ols_offset, \ Util.merge_dictlist([objs,to_retire_objs]), \ Util.merge_dictlist([objs_sizes,to_retire_objs_sizes]), \ Util.merge_dictlist([objs_fols,to_retire_objs_fols]), \ size_h, size_bins, fol_h, fol_bins,SHAPE) print >> sys.stderr, 'object initialization complete in', str( datetime.timedelta(seconds=int(time.time() - t))) return ols, ols_offset, frames_offset, objs, splits, objs_sizes, objs_fols, prelast_avg, prelast_mm, to_retire_objs, to_retire_objs_sizes, to_retire_objs_fols
last_newactols_file = '%07d-%07d-newact_ols.list' % ( frames_offset, frames_offset + hsl) last_ground_file = '%07d-%07d-ground.list' % (frames_offset, frames_offset + hsl) last_digdiff_file = '%07d-%07d-digdiff.mat' % (frames_offset, frames_offset + hsl) t = time.time() #eventually, choice to reload previous analysis goes here last_frames = [] while len(last_frames) < hsl: ols_offset, frames_offset = advance_analysis(ols,ols_offset,objs,splits,objs_sizes,objs_fols, \ to_retire_objs,to_retire_objs_sizes,to_retire_objs_fols, \ last_frames,stream,frames,currsum,denom,opts.seglen,cutoff,frames_offset, \ SHAPE,size_h,size_bins,fol_h,fol_bins,transform=opts.transform,outline_engine=opts.outline_engine) last_avg = vidtools.average_frames(last_frames) last_mm = vidtools.mousemask_from_object_arcs(frames_offset-len(last_frames),frames_offset,min_arc_score,ols,ols_offset, \ Util.merge_dictlist([objs,to_retire_objs]), \ Util.merge_dictlist([objs_sizes,to_retire_objs_sizes]), \ Util.merge_dictlist([objs_fols,to_retire_objs_fols]), \ size_h, size_bins, fol_h, fol_bins,SHAPE) this_avg = vidtools.average_frames(frames[:hsl]) this_mm = vidtools.mousemask_from_object_arcs(frames_offset,frames_offset+hsl,min_arc_score,ols,ols_offset, \ Util.merge_dictlist([objs,to_retire_objs]), \ Util.merge_dictlist([objs_sizes,to_retire_objs_sizes]), \ Util.merge_dictlist([objs_fols,to_retire_objs_fols]), \ size_h, size_bins, fol_h, fol_bins,SHAPE) prelast_masked = prelast_avg.copy() prelast_masked[prelast_mm] = numpy.mean(prelast_avg[:50, :50]) last_masked = last_avg.copy() last_masked[last_mm] = numpy.mean(last_avg[:50, :50])