ChannelFirst2d#

class deeptrack.features.ChannelFirst2d(axis: int = -1, **kwargs: dict[str, 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 front, 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).

**kwargs:: dict of str to Any

Additional keyword arguments passed to the parent Feature class.

Methods#

get(image: np.ndarray, axis: int, **kwargs: dict[str, Any]) -> np.ndarray

Rearrange 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(image, axis, **kwargs)

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

Methods Documentation

get(image: ndarray, axis: int, **kwargs: dict[str, Any]) ndarray#

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: np.ndarray

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

axis: int

The axis to move to the first position (for 3D images).

**kwargs: Any

Additional keyword arguments (unused here).

Returns#

np.ndarray

The processed image in channel-first format.

Raises#

ValueError

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