Skip to content

Center cropping shifts images after odd-sized centered padding #133

Closed

Description

@sylvesterkaczmarek

Reproduced on master at ffb65074b39b67e1a686d1348f2d461c7ff3ab45.

center_crop subtracts separately rounded centers to compute its starting position. For an even-sized input and odd target, this selects the bottom/right alignment, whereas pad_to_size puts extra padding on the bottom/right. Padding then cropping can therefore lose original pixels.

import jax.numpy as jnp
import dm_pix
image = jnp.arange(9.).reshape(3, 3, 1)
padded = dm_pix.pad_to_size(image, 4, 4)
print(dm_pix.center_crop(padded, 3, 3)[..., 0])
# [[4, 5, 0], [7, 8, 0], [0, 0, 0]]

The round trip should recover the original 0–8 image. Deriving each offset from half the size difference matches centered padding and TensorFlow's crop-or-pad convention: https://www.tensorflow.org/api_docs/python/tf/image/resize_with_crop_or_pad

Prepared tests cover parity combinations, channel-first/channel-last data, batches, eager/JIT execution, exact round-trip gradients and the combined crop-or-pad API. Oversized target dimensions retain the existing no-op cropping behavior. This does not overlap the Gaussian-blur changes in PRs #119 and #131.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions