Skip to content

BUG: pad_image and decrop_image failed on multichannel images - #965

Open
Srizdebnath wants to merge 1 commit into
ANTsX:mainfrom
Srizdebnath:fix/pad-decrop-multichannel
Open

Srizdebnath wants to merge 1 commit into
ANTsX:mainfrom
Srizdebnath:fix/pad-decrop-multichannel

Conversation

@Srizdebnath

Copy link
Copy Markdown

crop_image and crop_indices accept multichannel images: they split the channels, crop each one and merge them. Two related functions don't:

import ants
fi = ants.image_read(ants.get_ants_data("r16"))
mc = ants.merge_channels([fi, fi * 2])

cropped = ants.crop_image(mc)          # works
ants.decrop_image(cropped, mc)         # TypeError: cropImage(): incompatible function arguments
ants.pad_image(mc, pad_width=(4, 4))   # TypeError: padImage(): incompatible function arguments

The cropImage and padImage bindings only exist for scalar images, and these two functions pass the vector image straight through. As a result, a multichannel crop can't be put back with decrop_image. Neither works on, say, an RGB image or a multi-component derived image, and neither do functions built on pad_image.

This uses the same split, process and merge pattern as crop_image, and keeps channels_first:

  • pad_image pads every channel with the same widths. With return_padvals=True it still returns (image, lower, upper).
  • decrop_image pastes each cropped channel into the matching channel of full_image, or into full_image itself when that is a scalar image. A channel-count mismatch raises a ValueError.

test_decrop_image_multichannel and test_pad_image_multichannel check each output channel against the scalar function applied to that channel. They fail on main and pass here, and the existing crop and pad tests still pass.

crop_image and crop_indices split multichannel images into channels,
but pad_image and decrop_image passed the vector image straight to the
scalar-only padImage/cropImage bindings and raised a nanobind
TypeError. Process each channel and merge the result, keeping
channels_first. decrop_image pastes each cropped channel into the
matching channel of full_image, or into full_image itself when it is
scalar.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant