Пример #1
0
    def center(self, loc=None):
        """Center the image foreground

        This function shifts the center of mass (CoM) to the image center.

        Parameters
        ----------
        loc : (col, row), optional
            The pixel location at which to center the CoM. By default, shifts
            the CoM to the image center.

        Returns
        -------
        stim : `ImageStimulus`
            A copy of the stimulus object containing the centered image

        """
        # Calculate center of mass:
        img = self.data.reshape(self.img_shape)
        m = img_moments(img, order=1)
        # No area found:
        if np.isclose(m[0, 0], 0):
            return img
        # Center location:
        if loc is None:
            loc = np.array(self.img_shape[::-1]) / 2.0 - 0.5
        # Shift the image by -centroid, +image center:
        transl = (loc[0] - m[0, 1] / m[0, 0], loc[1] - m[1, 0] / m[0, 0])
        tf_shift = SimilarityTransform(translation=transl)
        img = img_warp(img, tf_shift.inverse)
        return ImageStimulus(img,
                             electrodes=self.electrodes,
                             metadata=self.metadata)
Пример #2
0
    def scale(self, scaling_factor):
        """Scale the image foreground

        This function scales the image foreground (excluding black pixels)
        by a factor.

        Parameters
        ----------
        scaling_factor : float
            Factory by which to scale the image

        Returns
        -------
        stim : `ImageStimulus`
            A copy of the stimulus object containing the scaled image

        """
        if scaling_factor <= 0:
            raise ValueError("Scaling factor must be greater than zero")
        # Calculate center of mass:
        img = self.data.reshape(self.img_shape)
        m = img_moments(img, order=1)
        # No area found:
        if np.isclose(m[0, 0], 0):
            return img
        # Shift the phosphene to (0, 0):
        center_mass = np.array([m[0, 1] / m[0, 0], m[1, 0] / m[0, 0]])
        tf_shift = SimilarityTransform(translation=-center_mass)
        # Scale the phosphene:
        tf_scale = SimilarityTransform(scale=scaling_factor)
        # Shift the phosphene back to where it was:
        tf_shift_inv = SimilarityTransform(translation=center_mass)
        # Combine all three transforms:
        tf = tf_shift + tf_scale + tf_shift_inv
        img = img_warp(img, tf.inverse)
        return ImageStimulus(img,
                             electrodes=self.electrodes,
                             metadata=self.metadata)