Search code examples
pythondeep-learningscikit-imagessim

Structural Similarity Index (SSIM) in Python (Multichannel error)


I want to calculate the Structural Similarity Index (SSIM) between a generated and a target image (that have been picked randomly from an array of images).

This is what I have tried-

from skimage.metrics import structural_similarity as ssim

print(tar_image.shape)
print(gen_image.shape)

ssim_skimg = ssim(tar_image, gen_image,
                  data_range = gen_image.max() - gen_image.min(), 
                  multichannel = True)

print("SSIM: based on scikit-image = ", ssim_skimg)

But I am getting this output:

(1, 128, 128, 3)
(1, 128, 128, 3)

ValueError: win_size exceeds image extent.  If the input is a multichannel (color) image, set multichannel=True.

Can someone please tell me where I am going wrong and how I can fix this problem?


Solution

  • You have 3 channel images, so you should use the channel_axis argument.

    Also you should remove the first dimension of your images to get (128,128,3) shapes

    import numpy as np
    from skimage.metrics import structural_similarity as ssim 
    
    tar_image = np.zeros((128, 128, 3))
    gen_image = np.zeros((128, 128, 3))
    
    ssim_skimg = ssim(tar_image, gen_image, channel_axis=-1, data_range=255)