Skip to content

warp does not scale cval leading to unexpected (wrong?) behavior with preserve_range=False and non-zero cval #7336

@idc9

Description

@idc9

Description:

When you call warp with preserve_range=False. The values of the pixels in are usually scaled (e.g. if the input image were uint8 then 255 gets mapped to 1). However warp does not also scale the cval which is used to pad the image.

In the example below the pixels in the non-padded part of the output image are all between 0 and 1, but the padded pixels are set to 255.

I think the most straightforward solution is to rescale cval if preserve_range=False.

Way to reproduce:

from skimage import data
import numpy as np
import matplotlib.pyplot as plt

from skimage.transform import SimilarityTransform, warp
from skimage.color import rgb2gray


# uint8 grayscale image
image = data.astronaut()
image = rgb2gray(image)
image = (255 * image).astype(np.uint8)

tform = SimilarityTransform(translation=[20, 0])

# default with cval =0 -- the scaling problem does not show up here
wapped = warp(image=image, 
              inverse_map=tform.inverse,
              preserve_range=False,
              cval=0 # deafult value
             )

# all values in the image are between 0 and 1 since preserve_range=False
print(wapped.max())

1.0

plt.figure()
plt.imshow(wapped)
Screenshot 2024-03-07 at 7 12 06 PM
# now set non-zero cval
wapped = warp(image=image, 
              inverse_map=tform.inverse,
              preserve_range=False,
              cval=255 #
             )

# the padded values are 255, which was not scaled
print(wapped.max())

255.0

plt.figure()
plt.imshow(wapped)
Screenshot 2024-03-07 at 7 11 01 PM

Version information:

3.8.18 (default, Sep 11 2023, 08:17:33) 
[Clang 14.0.6 ]
macOS-10.16-x86_64-i386-64bit
scikit-image version: 0.21.0
numpy version: 1.24.4

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions