Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metric.structural_similarity throws the wrong error for small sizes #5366

Closed
Pomax opened this issue Apr 30, 2021 · 6 comments · Fixed by #5395
Closed

metric.structural_similarity throws the wrong error for small sizes #5366

Pomax opened this issue Apr 30, 2021 · 6 comments · Fixed by #5395

Comments

@Pomax
Copy link

Pomax commented Apr 30, 2021

Description

when using structural_similarity when the images are small (<7 in any dimension) a ValueError gets thrown, but with the wrong text:

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

Instead of misleading the user into thinking there's something wrong with the way they read in, or converted, their image data, this error should read:

ValueError: image data too small to perform SSIM analysis on, please ensure that your images are at least 7x7.

Way to reproduce

import cv2
from skimage.metrics import structural_similarity

def gray(img):
	return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

image = cv2.imread('test.jpg', cv2.IMREAD_COLOR)

# a 7x7 crop will work just fine
crop1 = image[0:7, 0:7]
crop2 = image[8:15, 8:15]
structural_similarity(gray(crop1), gray(crop2))

# a 6x6 crop will cause a ValueError, with the wrong message
crop1 = image[0:6, 0:6]
crop2 = image[8:14, 8:4]
structural_similarity(gray(crop1), gray(crop2))

Version information

# Paste the output of the following python commands
from __future__ import print_function
import sys; print(sys.version)
import platform; print(platform.platform())
import skimage; print("scikit-image version: {}".format(skimage.__version__))
import numpy; print("numpy version: {}".format(numpy.__version__))
3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 10:41:24) [MSC v.1900 64 bit (AMD64)]
Windows-10-10.0.19041-SP0
scikit-image version: 0.18.1
numpy version: 1.20.2
@Pomax
Copy link
Author

Pomax commented Apr 30, 2021

Additionally it might be worth updating the documentation for the structural_similarity function to explicitly mention this, as well as explain which function(s) people should be using instead if they need to compare (parts of an) image data that is very thin or very shallow.

@grlee77
Copy link
Contributor

grlee77 commented Apr 30, 2021

Thanks for reporting this @Pomax. Indeed this error message should be updated to be more useful in this case.

normalized_root_mse and peak_signal_noise_ratio may be better for very small images

@lilisako
Copy link
Contributor

Can I take this issue?

@grlee77
Copy link
Contributor

grlee77 commented May 14, 2021

Can I take this issue?

Sure! Let us know if you need any help/guidance

@Pomax
Copy link
Author

Pomax commented Jul 17, 2021

@grlee77 @lilisako Small note, but the PR as merged is using the wrong syntax for the string argument to ValueError. So that's something you probably want to patch.

I.e.:

>>> ValueError(
...   "x"
...   "y"
...   "z"
... )
ValueError('xyz')

As opposed to

>>> ValueError(
...   "x",
...   "y",
...   "z"
... )
ValueError('x', 'y', 'z')

The test does not confirm that the message in the ValueError is actually the expected error, which is probably why this wasn't caught before merging.

@mkcor
Copy link
Member

mkcor commented Jul 18, 2021

The test does not confirm that the message in the ValueError is actually the expected error, which is probably why this wasn't caught before merging.

@Pomax right, we didn't go this route in the end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants