Exemplo n.º 1
0
 def __init__(self, video, functions, preprocess=None, use_threads=True):
     """ initializes the preprocessor
     `video` is the video to be iterated over
     `functions` is a dictionary of functions that should be applied while
         iterating
     `preprocess` can be a function that will be applied to the frame before
         anything is returned
     """
     if 'raw' in functions:
         raise KeyError('The key `raw` is reserved for the raw _frame and '
                        'may not be used for functions.')
     
     self.length = len(video)
     self.video_iter = iter(video)
     self.functions = functions
     self.preprocess = preprocess
     
     # initialize internal structures
     self._frame = None
     
     # initialize the background workers
     self._worker_next_frame = WorkerThread(self._get_next_frame,
                                            use_threads=use_threads)
     self._workers = {name: WorkerThread(func, use_threads=use_threads)
                      for name, func in self.functions.iteritems()}
     
     self._init_next_processing(self._get_next_frame())
Exemplo n.º 2
0
    def __init__(self,
                 parameters,
                 blur_function=None,
                 object_radius=0,
                 use_threads=True):
        """ initialize the background extractor with
        `parameters` is a dictionary of parameters influencing the algorithm
        `blur_function` is an optional function that, if given, supplies a
            blurred image of the background via the `blurred` property
        `object_radius` is an additional parameter that influences how the
            background extraction is done.
        """
        self.image = None
        self.image_uint8 = None
        self._adaptation_rate = None
        self.params = parameters

        self._blurred = None
        if blur_function:
            self._blur_worker = WorkerThread(blur_function,
                                             use_threads=use_threads)
        else:
            self._blur_worker = None

        if object_radius > 0:
            # create a simple template of the mouse, which will be used to update
            # the background image only away from the mouse.
            # The template consists of a core region of maximal intensity and a ring
            # region with gradually decreasing intensity.

            # determine the sizes of the different regions
            size_core = object_radius
            size_ring = 3 * object_radius
            size_total = size_core + size_ring

            # build a filter for finding the mouse position
            x, y = np.ogrid[-size_total:size_total + 1,
                            -size_total:size_total + 1]
            r = np.sqrt(x**2 + y**2)

            # build the mouse template
            object_mask = (
                # inner circle of ones
                (r <= size_core).astype(float)
                # + outer region that falls off
                + np.exp(-((r - size_core) / size_core)**
                         2)  # smooth function from 1 to 0
                * (size_core < r)  # mask on ring region
            )

            self._object_mask = 1 - object_mask