MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / AsChannelLast

Class AsChannelLast

monai/transforms/utility/array.py:145–172  ·  view source on GitHub ↗

Change the channel dimension of the image to the last dimension. Some of other 3rd party transforms assume the input image is in the channel-last format with shape (spatial_dim_1[, spatial_dim_2, ...], num_channels). This transform could be used to convert, for example, a channel-

Source from the content-addressed store, hash-verified

143
144
145class AsChannelLast(Transform):
146 """
147 Change the channel dimension of the image to the last dimension.
148
149 Some of other 3rd party transforms assume the input image is in the channel-last format with shape
150 (spatial_dim_1[, spatial_dim_2, ...], num_channels).
151
152 This transform could be used to convert, for example, a channel-first image array in shape
153 (num_channels, spatial_dim_1[, spatial_dim_2, ...]) into the channel-last format,
154 so that MONAI transforms can construct a chain with other 3rd party transforms together.
155
156 Args:
157 channel_dim: which dimension of input image is the channel, default is the first dimension.
158 """
159
160 backend = [TransformBackends.TORCH, TransformBackends.NUMPY]
161
162 def __init__(self, channel_dim: int = 0) -> None:
163 if not (isinstance(channel_dim, int) and channel_dim >= -1):
164 raise ValueError(f"invalid channel dimension ({channel_dim}).")
165 self.channel_dim = channel_dim
166
167 def __call__(self, img: NdarrayOrTensor) -> NdarrayOrTensor:
168 """
169 Apply the transform to `img`.
170 """
171 out: NdarrayOrTensor = convert_to_tensor(moveaxis(img, self.channel_dim, -1), track_meta=get_track_meta())
172 return out
173
174
175class EnsureChannelFirst(Transform):

Callers 2

__init__Method · 0.90
test_shapeMethod · 0.90

Calls

no outgoing calls

Tested by 1

test_shapeMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…