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#
- axisint, optional
The axis to move to the first position. Defaults to -1 (last axis).
- **kwargsDict[str, Any]
Additional keyword arguments passed to the parent Feature class.
Example#
>>> 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#
- imagenp.ndarray
The input image to process. Can be 2D or 3D.
- axisint
The axis to move to the first position (for 3D images).
- **kwargsAny
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.