The function skimage.util.compare_images fails silently if called with integers matrices
Hish15 opened this issue · comments
Hish15 commented
Description:
I was trying to call the skimage.util.compare_images
with integers matrices (the documentation does not prevent this).
The function does return a value, but not the expected one.
I found out that the function skimage.util.img_as_float32
called in skimage.util.compare_images
does not convert the int matrix to a float matrix as expected, resulting into a wrong return value for compare_images.
Way to reproduce:
import skimage.util as skiu
mat_a = skiu.img_as_float32([[1.0, 1.0], [1.0, 1.0]])
mat_b = skiu.img_as_float32([[1, 1], [1, 1]])
mat_c = skiu.compare_images(mat_a, mat_b, method='diff')
assert mat_a[0][0] == 1.0 # OK
assert mat_b[0][0] == 1.0 # Will fail
assert mat_c[0][0] == 0 #Will fail too
Version information:
3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0]
Linux-4.4.0-22621-Microsoft-x86_64-with-glibc2.36
scikit-image version: 0.22
Marianne Corvellec commented
Right, it looks like the issue stems from skimage.util.img_as_float
(or, likewise, skimage.util.img_as_float32
), since:
import numpy as np
import skimage as ski
arr = np.array([[1, 1], [1, 1]])
ski.util.img_as_float(arr)
# array([[1.08420217e-19, 1.08420217e-19],
# [1.08420217e-19, 1.08420217e-19]])
ski.util.img_as_float32(arr)
# array([[1.0842022e-19, 1.0842022e-19],
# [1.0842022e-19, 1.0842022e-19]], dtype=float32)