def resize(self, shape, electrodes=None):
        """Resize the video

        Parameters
        ----------
        shape : (rows, cols)
            Shape of each frame in the resized video. If one of the dimensions
            is set to -1, its value will be inferred by keeping a constant
            aspect ratio.
        electrodes : int, string or list thereof; optional
            Optionally, you can provide your own electrode names. If none are
            given, electrode names will be numbered 0..N.

            .. note::
               The number of electrode names provided must match the number of
               pixels in the resized video.

        Returns
        -------
        stim : `VideoStimulus`
            A copy of the stimulus object containing the resized video

        """
        height, width = shape
        if height < 0 and width < 0:
            raise ValueError('"height" and "width" cannot both be -1.')
        if height < 0:
            height = int(self.vid_shape[0] * width / self.vid_shape[1])
        if width < 0:
            width = int(self.vid_shape[1] * height / self.vid_shape[0])
        vid = vid_resize(self.data.reshape(self.vid_shape),
                         (height, width, *self.vid_shape[2:]))
        return VideoStimulus(vid, electrodes=electrodes, time=self.time,
                             metadata=self.metadata)
Beispiel #2
0
 def __init__(self,
              source,
              format=None,
              resize=None,
              as_gray=False,
              electrodes=None,
              time=None,
              metadata=None,
              compress=False):
     if metadata is None:
         metadata = {}
     elif type(metadata) != dict:
         metadata = {'user': metadata}
     if isinstance(source, str):
         # Filename provided, read the video:
         reader = video_reader(source, format=format)
         vid = np.array([frame for frame in reader])
         # Move frame index to the last dimension:
         if vid.ndim == 4:
             vid = vid.transpose((1, 2, 3, 0))
         elif vid.ndim == 3:
             vid = vid.transpose((1, 2, 0))
         # Combine video metadata with user-specified metadata:
         meta = reader.get_meta_data()
         if meta is not None:
             metadata.update(meta)
         metadata['source'] = source
         metadata['source_shape'] = vid.shape
         # Infer the time points from the video frame rate:
         time = np.arange(vid.shape[-1]) * meta['fps']
     elif isinstance(source, VideoStimulus):
         vid = source.data.reshape(source.vid_shape)
         metadata.update(source.metadata)
         if electrodes is None:
             electrodes = source.electrodes
         if time is None:
             time = source.time
     elif isinstance(source, np.ndarray):
         vid = source
     else:
         raise TypeError("Source must be a filename, a 3D NumPy array or "
                         "another VideoStimulus, not %s." % type(source))
     if vid.ndim < 3 or vid.ndim > 4:
         raise ValueError("Videos must have 3 or 4 dimensions, not "
                          "%d." % vid.ndim)
     # Convert to grayscale if necessary:
     if as_gray:
         if vid.ndim == 4:
             vid = rgb2gray(vid.transpose((0, 1, 3, 2)))
     # Resize if necessary:
     if resize is not None:
         height, width = resize
         if height < 0 and width < 0:
             raise ValueError('"height" and "width" cannot both be -1.')
         if height < 0:
             height = int(vid.shape[0] * width / vid.shape[1])
         if width < 0:
             width = int(vid.shape[1] * height / vid.shape[0])
         vid = vid_resize(vid, (height, width, *vid.shape[2:]))
     # Store the original image shape for resizing and color conversion:
     self.vid_shape = vid.shape
     # Convert to float array in [0, 1] and call the Stimulus constructor:
     vid = img_as_float32(vid)
     super(VideoStimulus, self).__init__(vid.reshape((-1, vid.shape[-1])),
                                         time=time,
                                         electrodes=electrodes,
                                         metadata=metadata,
                                         compress=compress)
     self.metadata = metadata
     self.rewind()