当使用报错structural_similarity计算两幅图像之间的SSIM指标时窗口大小报错如下:
ValueError: win_size exceeds image extent. Either ensure that your images are at least 7x7; or pass win_size explicitly in the function call, with an odd value less than or equal to the smaller side of your images. If your images are multichannel (with color channels), set channel_axis to the axis number corresponding to the channels.
它翻译为中文大概意思是:这个报错是由于窗口大小 win_size
超出了图像尺寸范围所致。建议确保图像至少为 7x7 大小,或者在调用函数时显式传递一个奇数值小于或等于图像较小一侧尺寸的 win_size
参数。如果你的图像是多通道图像(带有颜色通道),还需要设置 channel_axis
参数,指定对应通道的轴编号。
这有可能是因为你使用较新版本的scikit-image这个库计算两图片ssim,在在1.0.版本之后将会移除参数multichannel,将其替换成channel_axis
参数,若此时你依然使用multichannel就有可能报错。
比如使用以下代码便会报错
from skimage.metrics import structural_similarity
def ssim(img1, img2):
return structural_similarity(img1, img2, multichannel=(len(img1.shape) == 3), data_range=check_img_data_range(img1))
可以改成,即可运行成功
return structural_similarity(img1, img2, channel_axis=-1, data_range=check_img_data_range(img1))
这是因为,在1.0.版本之后将会移除参数multichannel,将其替换成channel_axis
参数,其中,对channel_axis
的描述为:
channel_axis : int or None, optional
If None, the image is assumed to be a grayscale (single channel) image.
Otherwise, this parameter indicates which axis of the array corresponds
to channels.
它的意思就是这个参数指定的是数组里面通道数的axis,为None的话默认是计算灰度图像的ssim。
因此,对三通道数组进行计算SSIM时,例如数组维度为(H, W, C)
,此时指定channel_axis=-1
即可,不用再指定multichannel=True
。