scikit-image / scikit-image

Image processing in Python

Home Page:https://scikit-image.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeError "can't multiply sequence by non-int of type float" in `structural_similarity`

tanjia123456 opened this issue · comments

Description:

when compute ssim, the error occurs: TypeError: can't multiply sequence by non-int of type 'float'

from skimage.metrics import structural_similarity as compare_ssim
# c ans b is gray image, which shape=(512*512), dtype=float32
ssimw = compare_ssim(c, b, win_size=3, data_range=(1.0, 1.0))

The code used to work, but now it doesn't. python=3.11 scikit-image=0.22.0

so, how to solve this problem?

Way to reproduce:

Traceback (most recent call last):
  File "E:\CT2CTA\CTA-GAN-main5\train.py", line 53, in <module>
    main()
  File "E:\CT2CTA\CTA-GAN-main5\train.py", line 48, in main
    trainer.test()
  File "E:\CT2CTA\CTA-GAN-main5\trainer\Hd_Trainer_x.py", line 410, in test
    ssimw = compare_ssim(tmp_c, tmp_b, win_size=3, data_range=(1.0, 1.0))
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\software\anaconda\envs\pytorch311\Lib\site-packages\skimage\metrics\_structural_similarity.py", line 243, in structural_similarity
    C1 = (K1 * R) ** 2
          ~~~^~~
TypeError: can't multiply sequence by non-int of type 'float'

Version information:

python=3.11
scikit-image=0.22.0

@grlee77 Hi, can you give me some advice

Hello @tanjia123456, you are passing (1.0, 1.0) to data_range which isn't supported. Could you try

ssimw = compare_ssim(c, b, win_size=3, data_range=c.ptp())

instead? This assumes that c and b have the same "distance between minimum and maximum possible values" as documented in the docstring.

Hello @tanjia123456, you are passing (1.0, 1.0) to data_range which isn't supported. Could you try

ssimw = compare_ssim(c, b, win_size=3, data_range=c.ptp())

instead? This assumes that c and b have the same "distance between minimum and maximum possible values" as documented in the docstring.

thank you, it's works! When I use this line of code
ssimw = compare_ssim(c, b, win_size=3, data_range=255)
, it seems to work, so what's the difference? Also, my image is grayscale

Hi @tanjia123456,

Your original code didn't work because you passed data_range=(1.0, 1.0) while this argument should be a scalar (255 is a scalar value, so that's fine). I tried to make this even clearer with the above pull request, #7345. I'll close this for now. Feel free to reopen if I missed something about your issue.