Python skimage.measure.compare_ssim() Examples

The following are 30 code examples of skimage.measure.compare_ssim(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module skimage.measure , or try the search function .
Example #1
Source File: compare_imgs.py    From imgcomp-cvpr with GNU General Public License v3.0 6 votes vote down vote up
def compare(inp_img, out_img, calc_ssim=True, calc_msssim=True, calc_psnr=True):
    inp_img = _read_if_not_array(inp_img)
    out_img = _read_if_not_array(out_img)

    assert inp_img.shape == out_img.shape

    def get_ssim():
        return compare_ssim(inp_img, out_img, multichannel=True, gaussian_weights=True, sigma=1.5)

    def get_msssim():
        return MultiScaleSSIM(make_batched(inp_img), make_batched(out_img))

    def get_psnr():
        return compare_psnr(inp_img, out_img)

    def _run_if(cond, fn):
        return fn() if cond else None

    return _run_if(calc_ssim, get_ssim), _run_if(calc_msssim, get_msssim), _run_if(calc_psnr, get_psnr) 
Example #2
Source File: image_compare.py    From Yugioh-bot with MIT License 6 votes vote down vote up
def compare_images(image_a, image_b, title):
    # compute the mean squared error and structural similarity
    # index for the images
    m = mse(image_a, image_b)
    s = compare_ssim(image_a, image_b, multichannel=True)

    # setup the figure
    fig = plt.figure(title)
    plt.suptitle("MSE: %.2f, SSIM: %.2f" % (m, s))

    # show first image
    ax = fig.add_subplot(1, 2, 1)
    plt.imshow(image_a, cmap=plt.cm.gray)
    plt.axis("off")

    # show the second image
    ax = fig.add_subplot(1, 2, 2)
    plt.imshow(image_b, cmap=plt.cm.gray)
    plt.axis("off")

    # show the images
    plt.show() 
Example #3
Source File: trainer.py    From Pose-Guided-Person-Image-Generation with MIT License 6 votes vote down vote up
def generate(self, x_fixed, x_target_fixed, pose_target_fixed, root_path=None, path=None, idx=None, save=True):
        G = self.sess.run(self.G, {self.x: x_fixed, self.pose_target: pose_target_fixed})
        ssim_G_x_list = []
        # x_0_255 = utils_wgan.unprocess_image(x_target_fixed, 127.5, 127.5)
        for i in xrange(G.shape[0]):
            # G_gray = rgb2gray((G[i,:]/127.5-1).clip(min=-1,max=1))
            # x_target_gray = rgb2gray((x_target_fixed[i,:]).clip(min=-1,max=1))
            G_gray = rgb2gray((G[i,:]).clip(min=0,max=255).astype(np.uint8))
            x_target_gray = rgb2gray(((x_target_fixed[i,:]+1)*127.5).clip(min=0,max=255).astype(np.uint8))
            ssim_G_x_list.append(ssim(G_gray, x_target_gray, data_range=x_target_gray.max() - x_target_gray.min(), multichannel=False))
        ssim_G_x_mean = np.mean(ssim_G_x_list)
        if path is None and save:
            path = os.path.join(root_path, '{}_G_ssim{}.png'.format(idx,ssim_G_x_mean))
            save_image(G, path)
            print("[*] Samples saved: {}".format(path))
        return G 
Example #4
Source File: metric_gen.py    From PSSR with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def slice_process(x1, x2, y):
    if len(x1.shape) == 3: x1 = x1[0,:,:]
    if len(x2.shape) == 3: x2 = x2[0,:,:]
    if len(y.shape) == 3: y = y[0,:,:]

    # a scaled and shifted version of pred and bilinear
    x1 = 2*x1 + 100
    x2 = 2*x2 + 100

    # normalize/scale images
    (y_norm1, x1_norm) = norm_minmse(y, x1)
    (y_norm2, x2_norm) = norm_minmse(y, x2)

    # calulate psnr and ssim of the normalized/scaled images
    psnr1 = compare_psnr(*(y_norm1, x1_norm), data_range = 1.)
    psnr2 = compare_psnr(*(y_norm2, x2_norm), data_range = 1.)
    ssim1 = compare_ssim(*(y_norm1, x1_norm), data_range = 1.)
    ssim2 = compare_ssim(*(y_norm2, x2_norm), data_range = 1.)
    return psnr1, ssim1, psnr2, ssim2, y_norm1, x1_norm, y_norm2, x2_norm 
Example #5
Source File: test_nox.py    From Yugioh-bot with MIT License 6 votes vote down vote up
def compare_images(image_a, image_b, title):
    # compute the mean squared error and structural similarity
    # index for the images
    m = mse(image_a, image_b)
    s = compare_ssim(image_a, image_b, multichannel=True)

    # setup the figure
    fig = plt.figure(title)
    plt.suptitle("MSE: %.2f, SSIM: %.2f" % (m, s))

    # show first image
    ax = fig.add_subplot(1, 2, 1)
    plt.imshow(image_a, cmap=plt.cm.gray)
    plt.axis("off")

    # show the second image
    ax = fig.add_subplot(1, 2, 2)
    plt.imshow(image_b, cmap=plt.cm.gray)
    plt.axis("off")

    # show the images
    plt.show() 
Example #6
Source File: trainer.py    From Disentangled-Person-Image-Generation with MIT License 6 votes vote down vote up
def generate(self, x_fixed, x_target_fixed, pose_fixed, part_bbox_fixed, root_path=None, path=None, idx=None, save=True):
        G_pose_rcv, G_pose = self.sess.run([self.G_pose_rcv, self.G_pose])
        G_pose_inflated = py_poseInflate(G_pose_rcv, is_normalized=True, radius=4, img_H=256, img_W=256)
        # G = self.sess.run(self.G, {self.x: x_fixed, self.G_pose_inflated: G_pose_inflated, self.part_bbox: part_bbox_fixed})
        G_pose_inflated_img = np.tile(np.amax((G_pose_inflated+1)*127.5, axis=-1, keepdims=True), [1,1,1,3])
        
        # ssim_G_x_list = []
        # for i in xrange(G_pose.shape[0]):
        #     G_gray = rgb2gray((G[i,:]).clip(min=0,max=255).astype(np.uint8))
        #     x_gray = rgb2gray(((x_fixed[i,:]+1)*127.5).clip(min=0,max=255).astype(np.uint8))
        #     ssim_G_x_list.append(ssim(G_gray, x_gray, data_range=x_gray.max() - x_gray.min(), multichannel=False))
        # ssim_G_x_mean = np.mean(ssim_G_x_list)
        if path is None and save:
            # path = os.path.join(root_path, '{}_G_ssim{}.png'.format(idx,ssim_G_x_mean))
            # save_image(G, path)
            # print("[*] Samples saved: {}".format(path))
            path = os.path.join(root_path, '{}_G_pose.png'.format(idx))
            save_image(G_pose, path)
            print("[*] Samples saved: {}".format(path))
            path = os.path.join(root_path, '{}_G_pose_inflated.png'.format(idx))
            save_image(G_pose_inflated_img, path)
            print("[*] Samples saved: {}".format(path))
        return G_pose 
Example #7
Source File: AttackEvaluations.py    From DEEPSEC with MIT License 6 votes vote down vote up
def avg_SSIM(self):

        ori_r_channel = np.transpose(np.round(self.nature_samples * 255), (0, 2, 3, 1)).astype(dtype=np.float32)
        adv_r_channel = np.transpose(np.round(self.adv_samples * 255), (0, 2, 3, 1)).astype(dtype=np.float32)

        totalSSIM = 0
        cnt = 0

        """
        For SSIM function in skimage: http://scikit-image.org/docs/dev/api/skimage.measure.html

        multichannel : bool, optional If True, treat the last dimension of the array as channels. Similarity calculations are done 
        independently for each channel then averaged.
        """
        for i in range(len(self.adv_samples)):
            if self.successful(adv_softmax_preds=self.softmax_prediction[i], nature_true_preds=self.labels_samples[i],
                               targeted_preds=self.targets_samples[i], target_flag=self.Targeted):
                cnt += 1
                totalSSIM += SSIM(X=ori_r_channel[i], Y=adv_r_channel[i], multichannel=True)

        print('ASS:\t{:.3f}'.format(totalSSIM / cnt))
        return totalSSIM / cnt

    # 6: PSD: Perturbation Sensitivity Distance 
Example #8
Source File: utilty.py    From dcscn-super-resolution with MIT License 6 votes vote down vote up
def compute_psnr_and_ssim(image1, image2, border_size=0):
    """
    Computes PSNR and SSIM index from 2 images.
    We round it and clip to 0 - 255. Then shave 'scale' pixels from each border.
    """
    if len(image1.shape) == 2:
        image1 = image1.reshape(image1.shape[0], image1.shape[1], 1)
    if len(image2.shape) == 2:
        image2 = image2.reshape(image2.shape[0], image2.shape[1], 1)

    if image1.shape[0] != image2.shape[0] or image1.shape[1] != image2.shape[1] or image1.shape[2] != image2.shape[2]:
        return None

    image1 = trim_image_as_file(image1)
    image2 = trim_image_as_file(image2)

    if border_size > 0:
        image1 = image1[border_size:-border_size, border_size:-border_size, :]
        image2 = image2[border_size:-border_size, border_size:-border_size, :]

    psnr = compare_psnr(image1, image2, data_range=255)
    ssim = compare_ssim(image1, image2, win_size=11, gaussian_weights=True, multichannel=True, K1=0.01, K2=0.03,
                        sigma=1.5, data_range=255)
    return psnr, ssim 
Example #9
Source File: test_ssim.py    From gputools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _test_single(dshape):
    d0 =np.zeros(dshape,np.float32)

    ss = tuple(slice(s//4,-s//4) for s in dshape)
    d0[ss] = 10.

    d1 = d0+np.random.uniform(0, 1, dshape).astype(np.float32)

    drange = np.amax(d0) - np.amin(d0)
    print(drange)

    m1, m2 = compare_ssim(d0,d1, data_range = drange), ssim(d0,d1)

    print("shape: %s \t\tSSIM = %.2f\t\tdifference: %s" % (dshape, m1, np.abs(m1-m2)))
    npt.assert_almost_equal(m1,m2, decimal = 5)
    return m1, m2 
Example #10
Source File: test_sup.py    From FC-AIDE-Keras with MIT License 5 votes vote down vote up
def get_SSIM(self, X, X_hat):
        
        test_SSIM = measure.compare_ssim(X, X_hat, dynamic_range=X.max() - X.min())
        
        return test_SSIM 
Example #11
Source File: test.py    From MSRN-PyTorch with MIT License 5 votes vote down vote up
def SSIM(pred, gt, shave_border=0):
    height, width = pred.shape[:2]
    pred = pred[shave_border:height - shave_border, shave_border:width - shave_border]
    gt = gt[shave_border:height - shave_border, shave_border:width - shave_border]
    ssim = compare_ssim(gt, pred, data_range=gt.max() - pred.min())
    if ssim == 0:
        return 100
    return ssim 
Example #12
Source File: test_ft.py    From FC-AIDE-Keras with MIT License 5 votes vote down vote up
def get_SSIM(self, X, X_hat):
        
        test_SSIM = measure.compare_ssim(X, X_hat, dynamic_range=X.max() - X.min())
        
        return test_SSIM 
Example #13
Source File: benchmark.py    From tensorflow-SRGAN with MIT License 5 votes vote down vote up
def SSIM(self, gt, pred):
    ssim = compare_ssim(gt, pred, data_range=255, gaussian_weights=True)
    return ssim 
Example #14
Source File: util.py    From rabbitVE with GNU General Public License v3.0 5 votes vote down vote up
def calc_ssim(a, b):
    ssim = compare_ssim(cv2.cvtColor(a, cv2.COLOR_BGR2GRAY), cv2.cvtColor(b, cv2.COLOR_BGR2GRAY))
    return ssim 
Example #15
Source File: player.py    From OverwatchDataAnalysis with GNU General Public License v3.0 5 votes vote down vote up
def _identify_ult_charge_digit(self, digit, digit_refs):
        """Retrieves ultimate charge for current player.

        Author:
            Appcell
            
        Args:
            None

        Returns:
            None
        """
        score = 0
        res = -1
        digit = ImageUtils.float_to_uint8(digit)
        for i in range(10):
            match_result = []

            match_result = cv2.matchTemplate(
                digit, digit_refs[i], cv2.TM_CCOEFF_NORMED)

            _, max_val, _, max_loc = cv2.minMaxLoc(match_result)
            temp_digit = ImageUtils.crop(
                digit, 
                [max_loc[1], digit_refs[i].shape[0], max_loc[0], digit_refs[i].shape[1]])

            score_ssim = measure.compare_ssim(temp_digit, digit_refs[i], multichannel=True)
            if score_ssim + max_val > score:
                score = score_ssim + max_val
                res = i
        return [res, score] 
Example #16
Source File: hsi_evaluate.py    From tensorflow-exercise with Apache License 2.0 5 votes vote down vote up
def mssim(x_true,x_pred):
    """
        :param x_true: 高光谱图像:格式:(H, W, C)
        :param x_pred: 高光谱图像:格式:(H, W, C)
        :return: 计算原始高光谱数据与重构高光谱数据的结构相似度
    """
    SSIM = compare_ssim(X=x_true, Y=x_pred, multichannel=True)
    return SSIM 
Example #17
Source File: util.py    From SMIT with MIT License 5 votes vote down vote up
def dssim(p0, p1, range=255.):
    return (1 - compare_ssim(p0, p1, data_range=range, multichannel=True)) / 2.


# def rgb2lab(in_img, mean_cent=False):
#     from skimage import color
#     img_lab = color.rgb2lab(in_img)
#     if (mean_cent):
#         img_lab[:, :, 0] = img_lab[:, :, 0] - 50
#     return img_lab 
Example #18
Source File: test_blind_ft.py    From FC-AIDE-Keras with MIT License 5 votes vote down vote up
def get_SSIM(self, X, X_hat):
        
        test_SSIM = measure.compare_ssim(X, X_hat, dynamic_range=X.max() - X.min())
        
        return test_SSIM 
Example #19
Source File: test_blind_sup.py    From FC-AIDE-Keras with MIT License 5 votes vote down vote up
def get_SSIM(self, X, X_hat):
        
        test_SSIM = measure.compare_ssim(X, X_hat, dynamic_range=X.max() - X.min())
        
        return test_SSIM 
Example #20
Source File: util.py    From TecoGAN with Apache License 2.0 5 votes vote down vote up
def dssim(p0, p1, range=255.):
    # embed()
    return (1 - compare_ssim(p0, p1, data_range=range, multichannel=True)) / 2. 
Example #21
Source File: test.py    From MSRN-PyTorch with MIT License 5 votes vote down vote up
def SSIM(pred, gt, shave_border=0):
    height, width = pred.shape[:2]
    pred = pred[shave_border:height - shave_border, shave_border:width - shave_border]
    gt = gt[shave_border:height - shave_border, shave_border:width - shave_border]
    ssim = compare_ssim(gt, pred, data_range=gt.max() - pred.min())
    if ssim == 0:
        return 100
    return ssim 
Example #22
Source File: evaluation.py    From sbmc with Apache License 2.0 5 votes vote down vote up
def _ssim(im, ref):
    """Structural Similarity error (1-SSIM, or DSSIM).

    Args:
        im(np.array): image to test.
        ref(np.array): reference for the comparison.

    Returns:
        (float) error value.
    """
    return 1-ssim(im, ref, multichannel=True) 
Example #23
Source File: _legacy_big_metrics.py    From sbmc with Apache License 2.0 5 votes vote down vote up
def _ssim(im, ref):
    return 1-ssim(im, ref, multichannel=True) 
Example #24
Source File: test_nox.py    From Yugioh-bot with MIT License 5 votes vote down vote up
def test_initial_pass_through_compare(self):
        original = cv2.imread(os.path.join(self.provider.assets, "start_screen.png"))
        against = self.provider.get_img_from_screen_shot()
        wrong = cv2.imread(os.path.join(self.provider.assets, "battle.png"))

        # convert the images to grayscale
        original = mask_image([127], [255], cv2.cvtColor(original, cv2.COLOR_BGR2GRAY), True)
        against = mask_image([127], [255], cv2.cvtColor(against, cv2.COLOR_BGR2GRAY), True)
        wrong = mask_image([127], [255], cv2.cvtColor(wrong, cv2.COLOR_BGR2GRAY), True)
        # initialize the figure
        (score, diff) = compare_ssim(original, against, full=True)
        diff = (diff * 255).astype("uint8")
        self.assertTrue(score > .90, 'If this is less then .90 the initial compare of the app will fail')
        (score, nothing) = compare_ssim(original, wrong, full=True)
        self.assertTrue(score < .90)
        if self.__debug_pictures__:
            # threshold the difference image, followed by finding contours to
            # obtain the regions of the two input images that differ
            thresh = cv2.threshold(diff, 0, 255,
                                   cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
            cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                                    cv2.CHAIN_APPROX_SIMPLE)
            cnts = cnts[0]
            # loop over the contours
            for c in cnts:
                # compute the bounding box of the contour and then draw the
                # bounding box on both input images to represent where the two
                # images differ
                (x, y, w, h) = cv2.boundingRect(c)
                cv2.rectangle(original, (x, y), (x + w, y + h), (0, 0, 255), 2)
                cv2.rectangle(against, (x, y), (x + w, y + h), (0, 0, 255), 2)
            # show the output images
            diffs = ("Original", original), ("Modified", against), ("Diff", diff), ("Thresh", thresh)
            images = ("Original", original), ("Against", against), ("Wrong", wrong)
            self.setup_compare_images(diffs)
            self.setup_compare_images(images) 
Example #25
Source File: steam.py    From Yugioh-bot with MIT License 5 votes vote down vote up
def __is_initial_screen__(self, *args, **kwargs):
        original = cv2.imread(os.path.join(self.assets, "home_page_steam.png"))
        against = self.get_img_from_screen_shot()
        # convert the images to grayscale
        original = mask_image([127], [255], cv2.cvtColor(original, cv2.COLOR_BGR2GRAY), True)
        against = mask_image([127], [255], cv2.cvtColor(against, cv2.COLOR_BGR2GRAY), True)
        (score, diff) = compare_ssim(original, against, full=True)
        if score > .9:
            return True
        return False 
Example #26
Source File: nox.py    From Yugioh-bot with MIT License 5 votes vote down vote up
def __is_initial_screen__(self, *args, **kwargs):
        original = cv2.imread(os.path.join(self.assets, "start_screen.png"))
        against = self.get_img_from_screen_shot()
        # convert the images to grayscale
        original = mask_image([127], [255], cv2.cvtColor(original, cv2.COLOR_BGR2GRAY), True)
        against = mask_image([127], [255], cv2.cvtColor(against, cv2.COLOR_BGR2GRAY), True)
        (score, diff) = compare_ssim(original, against, full=True)
        if score > .9:
            return True
        return False 
Example #27
Source File: metrics.py    From proSR with GNU General Public License v3.0 5 votes vote down vote up
def eval_psnr_and_ssim(im1, im2, scale):
    im1_t = np.atleast_3d(img_as_float(im1))
    im2_t = np.atleast_3d(img_as_float(im2))

    if im1_t.shape[2] == 1 or im2_t.shape[2] == 1:
        im1_t = im1_t[..., 0]
        im2_t = im2_t[..., 0]

    else:
        im1_t = rgb2ycbcr(im1_t)[:, :, 0:1] / 255.0
        im2_t = rgb2ycbcr(im2_t)[:, :, 0:1] / 255.0

    if scale > 1:
        im1_t = mod_crop(im1_t, scale)
        im2_t = mod_crop(im2_t, scale)

        # NOTE conventionally, crop scale+6 pixels (EDSR, VDSR etc)
        im1_t = crop_boundaries(im1_t, int(scale) + 6)
        im2_t = crop_boundaries(im2_t, int(scale) + 6)

    psnr_val = compare_psnr(im1_t, im2_t)
    ssim_val = compare_ssim(
        im1_t,
        im2_t,
        win_size=11,
        gaussian_weights=True,
        multichannel=True,
        data_range=1.0,
        K1=0.01,
        K2=0.03,
        sigma=1.5)

    return psnr_val, ssim_val 
Example #28
Source File: evaluate.py    From sigmanet with MIT License 5 votes vote down vote up
def ssim(gt, pred):
    """ Compute Structural Similarity Index Metric (SSIM). """
    return compare_ssim(
        gt.transpose(1, 2, 0), pred.transpose(1, 2, 0), multichannel=True, data_range=gt.max()
    ) 
Example #29
Source File: sim.py    From findit with MIT License 5 votes vote down vote up
def execute(
        self, template_object: np.ndarray, target_object: np.ndarray, *_, **__
    ) -> FindItEngineResponse:
        resp = FindItEngineResponse()

        resized_target = cv2.resize(
            target_object, template_object.shape[::-1], interpolation=cv2.INTER_CUBIC
        )
        ssim = compare_ssim(resized_target, template_object)

        resp.append("conf", self.__dict__)
        resp.append("ssim", ssim, important=True)
        resp.append("ok", True, important=True)
        return resp 
Example #30
Source File: getMetrics_market.py    From Human-Pose-Transfer with MIT License 5 votes vote down vote up
def ssim_score(generated_images, reference_images):
    ssim_score_list = []
    for reference_image, generated_image in zip(reference_images, generated_images):
        ssim = compare_ssim(reference_image, generated_image, gaussian_weights=True, sigma=1.5,
                            use_sample_covariance=False, multichannel=True,
                            data_range=generated_image.max() - generated_image.min())
        ssim_score_list.append(ssim)
    return np.mean(ssim_score_list)