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
TThe 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
dataTensor<T>The tensor data.
channelOrderChannelOrderThe channel ordering.
colorSpaceColorSpaceThe 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
heightintThe image height.
widthintThe image width.
channelsintThe number of channels.
channelOrderChannelOrderThe channel ordering.
colorSpaceColorSpaceThe 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
batchSizeintThe batch size.
heightintThe image height.
widthintThe image width.
channelsintThe number of channels.
channelOrderChannelOrderThe channel ordering (must be BCHW or BHWC).
colorSpaceColorSpaceThe color space.
Properties
BatchSize
Gets the batch size (1 for single images).
public int BatchSize { get; }
Property Value
ChannelOrder
Gets or sets the channel ordering.
public ChannelOrder ChannelOrder { get; set; }
Property Value
Channels
Gets the number of channels.
public int Channels { get; }
Property Value
ColorSpace
Gets or sets the color space.
public ColorSpace ColorSpace { get; set; }
Property Value
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
IsNormalized
Gets or sets whether the image is normalized to [0, 1].
public bool IsNormalized { get; set; }
Property Value
Metadata
Gets or sets additional metadata.
public IDictionary<string, object>? Metadata { get; set; }
Property Value
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
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
xintThe left edge x coordinate.
yintThe top edge y coordinate.
widthintThe region width.
heightintThe 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
Returns
- T
The pixel value.
GetPixelChannels(int, int)
Gets all channel values at a pixel location.
public T[] GetPixelChannels(int y, int x)
Parameters
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
yintThe y coordinate (row).
xintThe x coordinate (column).
channelintThe channel index.
valueTThe 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
ToChannelOrder(ChannelOrder)
Converts this image to a different channel order.
public ImageTensor<T> ToChannelOrder(ChannelOrder targetOrder)
Parameters
targetOrderChannelOrderThe target channel order.
Returns
- ImageTensor<T>
A new ImageTensor with the specified channel order.