Example #1
0
    def __init__(self, img, gaussian_kernel_1d, size=None):
        """Create an SSIMImage.

        Args:
          img: PIL Image object or file name.
          gaussian_kernel_1d: Gaussian kernel from get_gaussian_kernel.
          size: New image size to resize image to.
        """
        self.gaussian_kernel_1d = gaussian_kernel_1d
        self.img = img
        if isinstance(img, compat.basestring):
            self.img = compat.Image.open(img)
        if size and size != self.img.size:
            self.img = self.img.resize(size)
        self.size = self.img.size
        self.img_gray, self.img_alpha = to_grayscale(self.img)
        if self.img_alpha is not None:
            self.img_gray[self.img_alpha == 255] = 0
        self.img_gray_squared = self.img_gray ** 2
        self.img_gray_mu = convolve_gaussian_2d(
            self.img_gray, self.gaussian_kernel_1d)
        self.img_gray_mu_squared = self.img_gray_mu ** 2
        self.img_gray_sigma_squared = convolve_gaussian_2d(
            self.img_gray_squared, self.gaussian_kernel_1d)
        self.img_gray_sigma_squared -= self.img_gray_mu_squared
Example #2
0
    def __init__(self, img, gaussian_kernel_1d, size=None):
        """Create an SSIMImage.

        Args:
          img: PIL Image object or file name.
          gaussian_kernel_1d: Gaussian kernel from get_gaussian_kernel.
          size: New image size to resize image to.
        """
        self.gaussian_kernel_1d = gaussian_kernel_1d
        self.img = img
        if isinstance(img, compat.basestring):
            self.img = compat.Image.open(img)
        if size and size != self.img.size:
            self.img = self.img.resize(size)
        self.size = self.img.size
        self.img_gray, self.img_alpha = to_grayscale(self.img)
        if self.img_alpha is not None:
            self.img_gray[self.img_alpha == 255] = 0
        self.img_gray_squared = self.img_gray**2
        self.img_gray_mu = convolve_gaussian_2d(self.img_gray,
                                                self.gaussian_kernel_1d)
        self.img_gray_mu_squared = self.img_gray_mu**2
        self.img_gray_sigma_squared = convolve_gaussian_2d(
            self.img_gray_squared, self.gaussian_kernel_1d)
        self.img_gray_sigma_squared -= self.img_gray_mu_squared
Example #3
0
    def __init__(self, img, gaussian_kernel_1d=None, size=None):
        """Create an SSIMImage.

        Args:
          img (str or PIL.Image): PIL Image object or file name.
          gaussian_kernel_1d (np.ndarray, optional): Gaussian kernel
          that was generated with utils.get_gaussian_kernel is used
          to precompute common objects for SSIM computation
          size (tuple, optional): New image size to resize image to.
        """
        # Use existing or create a new PIL.Image
        try:
            self.img = img if not isinstance(img, compat.basestring) \
            else compat.Image.open(img)
        except IOError as e:
            logging.debug("Unable to open %s" % img)
            raise IOError("Image probably malformed")

        # Resize image if size is defined and different
        # from original image
        if size and size != self.img.size:
            self.img = self.img.resize(size, Image.ANTIALIAS)

        # Set the size of the image
        self.size = self.img.size

        # If gaussian kernel is defined we create
        # common SSIM objects
        if gaussian_kernel_1d is not None:

            self.gaussian_kernel_1d = gaussian_kernel_1d

            # np.array of grayscale and alpha image
            self.img_gray, self.img_alpha = to_grayscale(self.img)
            if self.img_alpha is not None:
                self.img_gray[self.img_alpha == 255] = 0

            # Squared grayscale
            self.img_gray_squared = self.img_gray**2

            # Convolve grayscale image with gaussian
            self.img_gray_mu = convolve_gaussian_2d(self.img_gray,
                                                    self.gaussian_kernel_1d)

            # Squared mu
            self.img_gray_mu_squared = self.img_gray_mu**2

            # Convolve squared grayscale with gaussian
            self.img_gray_sigma_squared = convolve_gaussian_2d(
                self.img_gray_squared, self.gaussian_kernel_1d)

            # Substract squared mu
            self.img_gray_sigma_squared -= self.img_gray_mu_squared

        # If we don't define gaussian kernel, we create
        # common CW-SSIM objects
        else:
            # Grayscale PIL.Image
            self.img_gray = ImageOps.grayscale(self.img)
Example #4
0
    def __init__(self, img, gaussian_kernel_1d=None, size=None):
        """Create an SSIMImage.

        Args:
          img (str or PIL.Image): PIL Image object or file name.
          gaussian_kernel_1d (np.ndarray, optional): Gaussian kernel
          that was generated with utils.get_gaussian_kernel is used
          to precompute common objects for SSIM computation
          size (tuple, optional): New image size to resize image to.
        """
        # Use existing or create a new PIL.Image
        self.img = img if not isinstance(img, compat.basestring) \
            else compat.Image.open(img)

        # Resize image if size is defined and different
        # from original image
        if size and size != self.img.size:
            self.img = self.img.resize(size, Image.ANTIALIAS)

        # Set the size of the image
        self.size = self.img.size

        # Set the numeber of channels of the image
        self.nChannels = len(self.img.getbands())

        # If gaussian kernel is defined we create
        # common SSIM objects
        if gaussian_kernel_1d is not None:

            self.gaussian_kernel_1d = gaussian_kernel_1d

            # np.array of ycbcr channels and alpha image
            self.img_channels, self.img_alpha = to_ycbcr(self.img)

            # Squared channels
            self.img_channels_squared = self.img_channels**2

            # Convolve channels image with gaussian
            self.img_channels_mu = convolve_gaussian_2d(
                self.img_channels, self.gaussian_kernel_1d)

            # Squared mu
            self.img_channels_mu_squared = self.img_channels_mu**2

            # Convolve squared channels with gaussian
            self.img_channels_sigma_squared = convolve_gaussian_2d(
                self.img_channels_squared, self.gaussian_kernel_1d)

            # Substract squared mu
            self.img_channels_sigma_squared -= self.img_channels_mu_squared

        # If we don't define gaussian kernel, we create
        # common CW-SSIM objects
        else:
            # Grayscale PIL.Image
            self.img_channels = self.img.convert('YCbCr')
Example #5
0
    def __init__(self, img, gaussian_kernel_1d=None, size=None):
        """Create an SSIMImage.

        Args:
          img (str or PIL.Image): PIL Image object or file name.
          gaussian_kernel_1d (np.ndarray, optional): Gaussian kernel
          that was generated with utils.get_gaussian_kernel is used
          to precompute common objects for SSIM computation
          size (tuple, optional): New image size to resize image to.
        """
        # Use existing or create a new PIL.Image
        try:
            self.img = img if not isinstance(img, compat.basestring) else compat.Image.open(img)
        except IOError as e:
            logging.debug("Unable to open %s" % img)
            raise IOError("Image probably malformed")

        # Resize image if size is defined and different
        # from original image
        if size and size != self.img.size:
            self.img = self.img.resize(size, Image.ANTIALIAS)

        # Set the size of the image
        self.size = self.img.size

        # If gaussian kernel is defined we create
        # common SSIM objects
        if gaussian_kernel_1d is not None:

            self.gaussian_kernel_1d = gaussian_kernel_1d

            # np.array of grayscale and alpha image
            self.img_gray, self.img_alpha = to_grayscale(self.img)
            if self.img_alpha is not None:
                self.img_gray[self.img_alpha == 255] = 0

            # Squared grayscale
            self.img_gray_squared = self.img_gray ** 2

            # Convolve grayscale image with gaussian
            self.img_gray_mu = convolve_gaussian_2d(self.img_gray, self.gaussian_kernel_1d)

            # Squared mu
            self.img_gray_mu_squared = self.img_gray_mu ** 2

            # Convolve squared grayscale with gaussian
            self.img_gray_sigma_squared = convolve_gaussian_2d(self.img_gray_squared, self.gaussian_kernel_1d)

            # Substract squared mu
            self.img_gray_sigma_squared -= self.img_gray_mu_squared

        # If we don't define gaussian kernel, we create
        # common CW-SSIM objects
        else:
            # Grayscale PIL.Image
            self.img_gray = ImageOps.grayscale(self.img)
Example #6
0
    def ssim_value(self, target):
        """Compute the SSIM value from the reference image to the target image.

        Args:
          target (str or PIL.Image): Input image to compare the reference image
          to. This may be a PIL Image object or, to save time, an SSIMImage
          object (e.g. the img member of another SSIM object).

        Returns:
          Computed SSIM float value.
        """
        # Performance boost if handed a compatible SSIMImage object.
        if not isinstance(target, SSIMImage) or not np.array_equal(self.gaussian_kernel_1d, target.gaussian_kernel_1d):
            target = SSIMImage(target, self.gaussian_kernel_1d, self.img.size)

        img_mat_12 = self.img.img_gray * target.img_gray
        img_mat_sigma_12 = convolve_gaussian_2d(img_mat_12, self.gaussian_kernel_1d)
        img_mat_mu_12 = self.img.img_gray_mu * target.img_gray_mu
        img_mat_sigma_12 = img_mat_sigma_12 - img_mat_mu_12

        # Numerator of SSIM
        num_ssim = (2 * img_mat_mu_12 + self.c_1) * (2 * img_mat_sigma_12 + self.c_2)

        # Denominator of SSIM
        den_ssim = (self.img.img_gray_mu_squared + target.img_gray_mu_squared + self.c_1) * (
            self.img.img_gray_sigma_squared + target.img_gray_sigma_squared + self.c_2
        )

        ssim_map = num_ssim / den_ssim
        index = np.average(ssim_map)
        return index
Example #7
0
    def ssim_value(self, img2):
        """Compute the SSIM value from the reference image to the given image.

        Args:
          img2: Input image to compare the reference image to.

        Returns:
          Computed SSIM float value.
        """
        img2 = SSIMImage(img2, self.gaussian_kernel_1d, self.img1.size)
        img_mat_12 = self.img1.img_gray * img2.img_gray
        img_mat_sigma_12 = convolve_gaussian_2d(
            img_mat_12, self.gaussian_kernel_1d)
        img_mat_mu_12 = self.img1.img_gray_mu * img2.img_gray_mu
        img_mat_sigma_12 = img_mat_sigma_12 - img_mat_mu_12

        # Numerator of SSIM
        num_ssim = ((2 * img_mat_mu_12 + self.c_1) *
                    (2 * img_mat_sigma_12 + self.c_2))

        # Denominator of SSIM
        den_ssim = (
            (self.img1.img_gray_mu_squared + img2.img_gray_mu_squared +
             self.c_1) *
            (self.img1.img_gray_sigma_squared +
             img2.img_gray_sigma_squared + self.c_2))

        ssim_map = num_ssim / den_ssim
        index = numpy.average(ssim_map)
        return index
Example #8
0
    def ssim_value(self, img2):
        """Compute the SSIM value from the reference image to the given image.

        Args:
          img2: Input image to compare the reference image to.

        Returns:
          Computed SSIM float value.
        """
        img2 = SSIMImage(img2, self.gaussian_kernel_1d, self.img1.size)
        img_mat_12 = self.img1.img_gray * img2.img_gray
        img_mat_sigma_12 = convolve_gaussian_2d(img_mat_12,
                                                self.gaussian_kernel_1d)
        img_mat_mu_12 = self.img1.img_gray_mu * img2.img_gray_mu
        img_mat_sigma_12 = img_mat_sigma_12 - img_mat_mu_12

        # Numerator of SSIM
        num_ssim = ((2 * img_mat_mu_12 + self.c_1) *
                    (2 * img_mat_sigma_12 + self.c_2))

        # Denominator of SSIM
        den_ssim = ((self.img1.img_gray_mu_squared + img2.img_gray_mu_squared +
                     self.c_1) * (self.img1.img_gray_sigma_squared +
                                  img2.img_gray_sigma_squared + self.c_2))

        ssim_map = num_ssim / den_ssim
        index = numpy.average(ssim_map)
        return index
Example #9
0
    def ssim_value(self, target):
        """Compute the SSIM value from the reference image to the target image.

        Args:
          target (str or PIL.Image): Input image to compare the reference image
          to. This may be a PIL Image object or, to save time, an SSIMImage
          object (e.g. the img member of another SSIM object).

        Returns:
          Computed SSIM float value.
        """
        # Performance boost if handed a compatible SSIMImage object.
        if not isinstance(target, SSIMImage) \
          or not np.array_equal(self.gaussian_kernel_1d,
                                target.gaussian_kernel_1d):
            target = SSIMImage(target, self.gaussian_kernel_1d, self.img.size)

        img_mat_12 = self.img.img_channels * target.img_channels
        img_mat_sigma_12 = convolve_gaussian_2d(img_mat_12,
                                                self.gaussian_kernel_1d)
        img_mat_mu_12 = self.img.img_channels_mu * target.img_channels_mu
        img_mat_sigma_12 = img_mat_sigma_12 - img_mat_mu_12

        # Numerator of SSIM
        num_ssim = ((2 * img_mat_mu_12 + self.c_1) *
                    (2 * img_mat_sigma_12 + self.c_2))

        # Denominator of SSIM
        den_ssim = ((self.img.img_channels_mu_squared +
                     target.img_channels_mu_squared + self.c_1) *
                    (self.img.img_channels_sigma_squared +
                     target.img_channels_sigma_squared + self.c_2))

        ssim_map = num_ssim / den_ssim
        indexes = np.average(ssim_map, axis=(0, 1))
        index = np.average(indexes, weights=self.w)
        return index