Table of Contents

Class ImageTensor<T>

Namespace
AiDotNet.Augmentation.Image
Assembly
AiDotNet.dll

Represents an image as a tensor with image-specific metadata and operations.

public class ImageTensor<T>

Type Parameters

T

The numeric type for pixel values.

Inheritance
ImageTensor<T>
Inherited Members

Remarks

ImageTensor wraps a Tensor<T> to provide image-specific functionality: - Channel ordering (CHW vs HWC) - Color space awareness (RGB, BGR, HSV, etc.) - Normalization state tracking - Image-specific operations (crop, resize, color conversion)

For Beginners: An image on a computer is stored as numbers representing pixel colors. This class represents those numbers in a way that's optimized for machine learning, while keeping track of important details like whether the image is in RGB or BGR format.

Constructors

ImageTensor(Tensor<T>, ChannelOrder, ColorSpace)

Creates an ImageTensor from an existing tensor.

public ImageTensor(Tensor<T> data, ChannelOrder channelOrder = ChannelOrder.CHW, ColorSpace colorSpace = ColorSpace.RGB)

Parameters

data Tensor<T>

The tensor data.

channelOrder ChannelOrder

The channel ordering.

colorSpace ColorSpace

The color space.

ImageTensor(int, int, int, ChannelOrder, ColorSpace)

Creates an ImageTensor with specified dimensions.

public ImageTensor(int height, int width, int channels = 3, ChannelOrder channelOrder = ChannelOrder.CHW, ColorSpace colorSpace = ColorSpace.RGB)

Parameters

height int

The image height.

width int

The image width.

channels int

The number of channels.

channelOrder ChannelOrder

The channel ordering.

colorSpace ColorSpace

The color space.

ImageTensor(int, int, int, int, ChannelOrder, ColorSpace)

Creates a batched ImageTensor with specified dimensions.

public ImageTensor(int batchSize, int height, int width, int channels = 3, ChannelOrder channelOrder = ChannelOrder.BCHW, ColorSpace colorSpace = ColorSpace.RGB)

Parameters

batchSize int

The batch size.

height int

The image height.

width int

The image width.

channels int

The number of channels.

channelOrder ChannelOrder

The channel ordering (must be BCHW or BHWC).

colorSpace ColorSpace

The color space.

Properties

BatchSize

Gets the batch size (1 for single images).

public int BatchSize { get; }

Property Value

int

ChannelOrder

Gets or sets the channel ordering.

public ChannelOrder ChannelOrder { get; set; }

Property Value

ChannelOrder

Channels

Gets the number of channels.

public int Channels { get; }

Property Value

int

ColorSpace

Gets or sets the color space.

public ColorSpace ColorSpace { get; set; }

Property Value

ColorSpace

Data

Gets the underlying tensor data.

public Tensor<T> Data { get; }

Property Value

Tensor<T>

Height

Gets the image height in pixels.

public int Height { get; }

Property Value

int

IsNormalized

Gets or sets whether the image is normalized to [0, 1].

public bool IsNormalized { get; set; }

Property Value

bool

Metadata

Gets or sets additional metadata.

public IDictionary<string, object>? Metadata { get; set; }

Property Value

IDictionary<string, object>

NormalizationMean

Gets or sets the normalization mean (per channel).

public T[]? NormalizationMean { get; set; }

Property Value

T[]

NormalizationStd

Gets or sets the normalization std (per channel).

public T[]? NormalizationStd { get; set; }

Property Value

T[]

OriginalRange

Gets or sets the original value range before normalization.

public (T min, T max)? OriginalRange { get; set; }

Property Value

(T Pitch, T Confidence)?

Width

Gets the image width in pixels.

public int Width { get; }

Property Value

int

Methods

Clone()

Creates a deep copy of this image tensor.

public ImageTensor<T> Clone()

Returns

ImageTensor<T>

A new ImageTensor with copied data.

Crop(int, int, int, int)

Extracts a rectangular region from the image.

public ImageTensor<T> Crop(int x, int y, int width, int height)

Parameters

x int

The left edge x coordinate.

y int

The top edge y coordinate.

width int

The region width.

height int

The region height.

Returns

ImageTensor<T>

A new ImageTensor containing the extracted region.

GetDimensions()

Gets the tensor dimensions as an array.

public int[] GetDimensions()

Returns

int[]

The dimensions array.

GetPixel(int, int, int)

Gets a pixel value at the specified coordinates.

public T GetPixel(int y, int x, int channel = 0)

Parameters

y int

The y coordinate (row).

x int

The x coordinate (column).

channel int

The channel index.

Returns

T

The pixel value.

GetPixelChannels(int, int)

Gets all channel values at a pixel location.

public T[] GetPixelChannels(int y, int x)

Parameters

y int

The y coordinate.

x int

The x coordinate.

Returns

T[]

Array of channel values.

SetPixel(int, int, int, T)

Sets a pixel value at the specified coordinates.

public void SetPixel(int y, int x, int channel, T value)

Parameters

y int

The y coordinate (row).

x int

The x coordinate (column).

channel int

The channel index.

value T

The value to set.

SetPixelChannels(int, int, T[])

Sets all channel values at a pixel location.

public void SetPixelChannels(int y, int x, T[] values)

Parameters

y int

The y coordinate.

x int

The x coordinate.

values T[]

The channel values.

ToChannelOrder(ChannelOrder)

Converts this image to a different channel order.

public ImageTensor<T> ToChannelOrder(ChannelOrder targetOrder)

Parameters

targetOrder ChannelOrder

The target channel order.

Returns

ImageTensor<T>

A new ImageTensor with the specified channel order.