1

I'm trying to use the "compare_ssim" function. I currently have two 2xN matrices of x,y coordinates where the first row is all the x coordinates and the second row is all the y coordinates of each of the two images. How can I calculate the SSIM for these two images (if there is a way to do so)

For example I have:

X = np.array([[1,2,3], [4,5,6]])
Y = np.array([[3,4,5],[5,6,7]])

compare_ssim(X,Y)

But I am getting the error

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

I'm not sure if I am missing a parameter or if I should convert the matrices in such a way that this function works. Or if there is a way that I am supposed to convert my coordinates to a grayscale matrix? I'm a bit confused on what the matrices for the parameters of the function should look like. I know that they are supposed to be ndarrays but the type(Y) and type(Y) are both numpy.ndarray.

Angie
  • 183
  • 3
  • 13

1 Answers1

4

Since you haven't mentioned which framework/library you are using, I am going with the assumption that you are using skimage's compare_ssim.

The error in question is due to the shape of your inputs. You can find more details here.

TL;DR: compare_ssim expects images in (H, W, C) dimensions but your input images have a dimension of (2, 3). So the function is confused which dimension to treat as the channel dimension. When multichannel=True, the last dimension is treated as the channel dimension.

There are 3 key problems with your code,

  1. compare_image expects Images as input. So your X and Y matrices should be of the dimensions (H, W, C) and not (2, 3)

  2. They should of float datatype.

Below I have shown a bit of demo code (note: Since skimage v1.7, compare_ssim has been moved to skimage.metrics.structural_similarity)


import numpy as np  
from skimage.metrics import structural_similarity

img1 = np.random.randint(0, 255, size=(200, 200, 3)).astype(np.float32)
img2 = np.random.randint(0, 255, size=(200, 200, 3)).astype(np.float32)

ssim_score = structural_similarity(img1, img2, multichannel=True) #score: 0.0018769083894301646

ssim_score = structural_similarity(img1, img1, multichannel=True) #score: 1.0

Dharman
  • 30,962
  • 25
  • 85
  • 135