def apply(self, frames): registrators = opflowreg.RegistrationInterfaces if self.reg_type == 'template': tstart = len(frames)/2 tstop = min(len(frames),tstart+50) template = np.max(frames[tstart:tstop],axis=0) def register_stack(stack, registrator): return opflowreg.register_stack_to_template(stack,template,registrator, njobs=self.n_cpu) elif self.reg_type == 'recursive': def register_stack(stack, registrator): return opflowreg.register_stack_recursive(stack,registrator)[1] else: raise NameError("Unknown registration type") # TODO: below is just crazy. has to be made neat later reg_dispatcher = {'affine':registrators.affine, 'homograhy':registrators.homography, 'shifts':registrators.shifts, 'Greenberg-Kerr':registrators.greenberg_kerr, 'softmesh':registrators.softmesh} operations = self.reg_pipeline.split('->') newframes = frames warp_history = [] for movement_model in operations: warps = register_stack(newframes, reg_dispatcher[movement_model]) warp_history.append(warps) newframes = opflowreg.apply_warps(warps, newframes, njobs=self.n_cpu) final_warps = [lib.flcompose(*warpchain) for warpchain in zip(*warp_history)] if self.save_recipe_to: opflowreg.save_recipe(final_warps, self.save_recipe_to) print 'saved motions stab recipe to %s'%self.save_recipe_to return newframes
def frames(self,): """ Return iterator over frames. The composition of functions in `self.fns` list is applied to each frame. By default, this list is empty. Examples of function "hooks" to put into `self.fns` are ``imfun.lib.DFoSD``, ``imfun.lib.DFoF`` or functions from ``scipy.ndimage``. """ fn = lib.flcompose(identity, *self.fns) return itt.imap(fn,self.mlfimg.flux_frame_iter())
def register_stack_recursive(frames, regfn): """ Given stack of frames, align frames recursively and return a mean frame of the aligned stack and a list of functions, each of which takes an image and return warped image, aligned to this mean frame. """ #import sys #sys.setrecursionlimit(len(frames)) L = len(frames) if L < 2: return frames[0], [lambda f:f] else: mf_l, warps_left = register_stack_recursive(frames[:L/2], regfn) mf_r, warps_right = register_stack_recursive(frames[L/2:], regfn) fn = regfn(mf_l, mf_r) fm = 0.5*(parametric_warp(mf_l,fn) + mf_r) return fm, [lib.flcompose(fx,fn) for fx in warps_left] + warps_right
def apply_reg(frames): if args.type == 'template': if args.verbose > 1: print 'stabilization type is template' tstart = len(frames)/2 tstop = min(len(frames),tstart+50) template = np.max(frames[tstart:tstop],axis=0) def register_stack(stack, registrator, **fnargs): return opflowreg.register_stack_to_template(stack,template,registrator,njobs=args.ncpu,**fnargs) elif args.type == 'recursive': def register_stack(stack, registrator,**fnargs): return opflowreg.register_stack_recursive(stack,registrator,**fnargs)[1] else: raise NameError("Unknown registration type") # TODO: below is just crazy. has to be made neat later reg_dispatcher = {'affine':registrators.affine, 'homography':registrators.homography, 'shifts':registrators.shifts, 'Greenberg-Kerr':registrators.greenberg_kerr, 'softmesh':registrators.softmesh} operations = args.model newframes = frames warp_history = [] for movement_model in operations: if not isinstance(movement_model, basestring): if len(movement_model)>1: model, model_params = movement_model else: model, model_params = movement_model[0],{} else: model = movement_model model_params = {} if args.verbose > 1: print 'correcting for {} with params: {}'.format(model, model_params) warps = register_stack(newframes, reg_dispatcher[model], **model_params) warp_history.append(warps) newframes = opflowreg.apply_warps(warps, newframes, njobs=args.ncpu) final_warps = [lib.flcompose(*warpchain) for warpchain in zip(*warp_history)] del newframes return final_warps
def pipeline(self): """Return the composite function to process frames based on self.fns""" return lib.flcompose(identity, *self.fns)