SSIM图像比较错误:“window_shape与arr_in.shape不兼容”

6

我想使用ssim来比较两张图片的相似度。 但是出现了这个错误window_shape is incompatible with arr_in.shape。 为什么?(这是什么意思?)

from skimage.measure import structural_similarity as ssim
from skimage import io

img1 = io.imread('http://pasteio.com/m85cc2eed18c661bf8a0ea7e43779e742')
img2 = io.imread('http://pasteio.com/m1d45b9c70afdb576f1e3b33d342bf7d0')

ssim( img1, img2 )

跟踪回溯(最近一次调用在最上面): File "", line 1, in File "/var/www/wt/local/lib/python2.7/site-packages/skimage/measure/_structural_similarity.py", line 58, in structural_similarity XW = view_as_windows(X, (win_size, win_size)) File "/var/www/wt/local/lib/python2.7/site-packages/skimage/util/shape.py", line 221, in view_as_windows raise ValueError("window_shape is incompatible with arr_in.shape") ValueError: window_shape is incompatible with arr_in.shape。 即使我将相同的文件两次输入ssim(img1,img1),我仍然会收到相同的错误。

请提供您测试图像的链接。 - Stefan van der Walt
@StefanvanderWalt 已更新。现在包括图像的 URL。 - Alex
2
你正在处理彩色图像,因此你希望使用 ssim(img1, img2, multichannel=True) - Stefan van der Walt
1
类型错误:structural_similarity()收到了一个意外的关键字参数“multichannel”,该参数在当前稳定版(0.11.3)中不可用。尝试安装dev版本,但由于依赖项问题而失败。 - Alex
然后先将您的图像转换为灰度:from skimage import color; img1 = color.rgb2gray(img1) 等等。 - Stefan van der Walt
谢谢。这似乎可以工作,对于我想要做的事情已经足够好了。 - Alex
1个回答

3

你需要确保图像大小相同,才能使用scikit的ssim进行比较:

from skimage.measure import compare_ssim
from skimage.transform import resize
from scipy.ndimage import imread
import numpy as np

# resized image sizes
height = 2**10
width = 2**10

a = imread('a.jpg', flatten=True).astype(np.uint8)
b = imread('b.jpg', flatten=True).astype(np.uint8)
a = resize(a, (height, width))
b = resize(b, (height, width))

sim, diff = compare_ssim(a, b, full=True)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接