ChannelFirst2d#

class deeptrack.features.ChannelFirst2d(axis: int | Callable[[...], int] = -1, **kwargs: Any)#

Bases: Feature

Convert an image to a channel-first format.

This feature rearranges the axes of a 3D image so that the specified axis (e.g., channel axis) is moved to the first position. If the input image is 2D, it adds a new dimension at the first index, effectively treating the 2D image as a single-channel image.

Parameters#

axis: int, optional

The axis to move to the first position. Defaults to -1 (last axis), which is typically the channel axis for NumPy arrays.

**kwargs: Any

Additional keyword arguments passed to the parent Feature class.

Methods#

get(image, axis, **kwargs) -> array

It rearranges the axes of an image to channel-first format.

Examples#

>>> import numpy as np
>>> from deeptrack.features import ChannelFirst2d

Create a 2D input array:

>>> input_image_2d = np.random.rand(10, 10)
>>> print(input_image_2d.shape)
(10, 10)

Convert it to channel-first format:

>>> channel_first_feature = ChannelFirst2d()
>>> output_image = channel_first_feature.get(input_image_2d, axis=-1)
>>> print(output_image.shape)
(1, 10, 10)

Create a 3D input array:

>>> input_image_3d = np.random.rand(10, 10, 3)
>>> print(input_image_3d.shape)
(10, 10, 3)

Convert it to channel-first format:

>>> output_image = channel_first_feature.get(input_image_3d, axis=-1)
>>> print(output_image.shape)
(3, 10, 10)

Methods Summary

get(array[, axis])

Rearrange the axes of an image to channel-first format.

Methods Documentation

get(array: ndarray | Tensor, axis: int = -1, **kwargs: Any) ndarray | Tensor#

Rearrange the axes of an image to channel-first format.

Rearrange the axes of a 3D image to channel-first format or add a channel dimension to a 2D image.

Parameters#

image: array

The input image to process. Can be 2D or 3D.

axis: int

The axis to move to the first position (for 3D images). For 2D images, this argument does nothing.

**kwargs: Any

Additional keyword arguments (unused here).

Returns#

array

The processed image in channel-first format.

Raises#

ValueError

If the input image is neither 2D nor 3D.