Skip to content

metrics.structural_similarity: win_size is not ignored if gaussian_weights is set to True #7231

Closed

Description

@anwolosz

Description:

Based on the documentation of structural_similarity parameter win_size should be ignored if parameter gaussian_weights is set to True. The reasoning is that if gaussian_weights is True, then win_size is calculated from sigma.

However, if win_size set to any valid integer, the important value setting if branch is skipped at line 169. (Still the code is working properly if win_size is None)
Later win_size is used unconditionally for calculating pad (line 254) and could be used if use_sample_covariance is set to True (line 222-226.).

Using the following images the results are changing depending on whether win_size is set to None or set to a valid integer. (While based on the documentation I expected no change in the results.)

camera
camera_noisy

Way to reproduce:

import skimage

im1 = skimage.io.imread("~/img/camera.png") 
im2 = skimage.io.imread("~/img/camera_noisy.png")
ssim_without_win_size = skimage.metrics.structural_similarity(im1, im2, gaussian_weights=True, win_size=None)
#By documentation win_size should be ignored is gaussian_weights is set to True...
ssim_with_win_size    = skimage.metrics.structural_similarity(im1, im2, gaussian_weights=True, win_size=101) 

print(ssim_without_win_size) #0.3654599419036462
print(ssim_with_win_size) #0.3890367780407127
#...but it did change the value.

Version information:

3.12.0 | packaged by conda-forge | (main, Oct  3 2023, 08:43:22) [GCC 12.3.0]
Linux-5.4.0-165-generic-x86_64-with-glibc2.31
scikit-image version: 0.23.0rc0.dev0+git20231030.c2725eaba
numpy version: 1.26.0

Activity

  1. mkcor commented on Nov 7, 2023

    @mkcor
    Member

    Thanks for reporting, @anwolosz! I would be tempted to fix the docstring, replacing "If gaussian_weights is True, this is ignored and the window size will depend on sigma." with "If gaussian_weights is True and win_size is None, the window size will depend on sigma."

    Otherwise, changing the code will introduce a breaking change, but maybe this would be the healthier way to go? I haven't written the function and I'm not sure how to interpret setting gaussian_weights to True:

    • is it meant to 'overwrite' anything else (here, the win_size value)?
    • is it meant as a 'fallback' (here, for the case when win_size isn't specified)?

    I'd like to ask my fellow @scikit-image/core members for their opinion.

    Also, noticing:

    win_size = 7 # backwards compatibility

  2. lagru commented on Nov 11, 2023

    @lagru
    Member

    I am not familiar at all with algorithm and implementation but this lines

    if win_size is None:
    if gaussian_weights:
    # set win_size used by crop to match the filter size
    r = int(truncate * sigma + 0.5) # radius as in ndimage
    win_size = 2 * r + 1
    else:
    win_size = 7 # backwards compatibility

    NP = win_size ** ndim
    # filter has already normalized by NP
    if use_sample_covariance:
    cov_norm = NP / (NP - 1) # sample covariance
    else:
    cov_norm = 1.0 # population covariance to match Wang et. al. 2004

    seem relevant. Regardless of gaussian_weights, win_size might affect the result via cov_norm if use_sample_covariance=True. I have no idea about the implications of a mismatching win_size and sigma, so not sure what to do with this information either.

    Unless someone more knowledgeable chimes in I'd go with your suggestion @mkcor. And maybe add a warning, in case a weird win_size is given with gaussian_weights=True...

    @grlee77, pinging you as the author of #1511 in case you have time and context. 🤞

  3. github-actions commented on May 10, 2024

    @github-actions

    Hello scikit-image core devs! There hasn't been any activity on this issue for more than 180 days. I have marked it as "dormant" to make it easy to find.
    To our contributors, thank you for your contribution and apologies if this issue fell through the cracks! Hopefully this ping will help bring some fresh attention to the issue. If you need help, you can always reach out on our forum
    If you think that this issue is no longer relevant, you may close it, or we may do it at some point (either way, it will be done manually).

  4. added a commit that references this issue on May 15, 2026
    e370be4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐛 Bug😴 Dormant

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions