Table of Contents

Class ImageHelper<T>

Namespace
AiDotNet.Helpers
Assembly
AiDotNet.dll

Helper class for loading and saving images as tensors.

public static class ImageHelper<T>

Type Parameters

T

The numeric type for tensor values.

Inheritance
ImageHelper<T>
Inherited Members

Remarks

Supports common image formats without external dependencies: - BMP: Windows Bitmap format (uncompressed) - PPM/PGM: Portable Pixmap/Graymap (simple text or binary) - RAW: Raw pixel data with specified dimensions

For Beginners: This class converts image files into tensors for neural networks. Images are loaded as [channels, height, width] or [batch, channels, height, width] tensors.

Methods

LoadBmp(string, bool)

Loads a BMP (Windows Bitmap) image file.

public static Tensor<T> LoadBmp(string filePath, bool normalize = true)

Parameters

filePath string

Path to the BMP file.

normalize bool

Whether to normalize to [0, 1].

Returns

Tensor<T>

Tensor with shape [1, 3, height, width] for RGB images.

LoadImage(string, bool)

Loads an image from a file path and returns it as a tensor.

public static Tensor<T> LoadImage(string filePath, bool normalize = true)

Parameters

filePath string

Path to the image file.

normalize bool

Whether to normalize pixel values to [0, 1] range.

Returns

Tensor<T>

Tensor with shape [1, channels, height, width].

Exceptions

FileNotFoundException

If the file does not exist.

NotSupportedException

If the image format is not supported.

LoadPgm(string, bool)

Loads a PGM (Portable Graymap) image file.

public static Tensor<T> LoadPgm(string filePath, bool normalize = true)

Parameters

filePath string

Path to the PGM file.

normalize bool

Whether to normalize to [0, 1].

Returns

Tensor<T>

Tensor with shape [1, 1, height, width].

LoadPpm(string, bool)

Loads a PPM (Portable Pixmap) image file.

public static Tensor<T> LoadPpm(string filePath, bool normalize = true)

Parameters

filePath string

Path to the PPM file.

normalize bool

Whether to normalize to [0, 1].

Returns

Tensor<T>

Tensor with shape [1, 3, height, width].

LoadRaw(string, int, int, int, int, bool)

Loads raw pixel data from a file.

public static Tensor<T> LoadRaw(string filePath, int width, int height, int channels = 3, int bytesPerChannel = 1, bool normalize = true)

Parameters

filePath string

Path to the raw data file.

width int

Image width.

height int

Image height.

channels int

Number of channels (1 for grayscale, 3 for RGB).

bytesPerChannel int

Bytes per channel (1 for 8-bit, 2 for 16-bit).

normalize bool

Whether to normalize values.

Returns

Tensor<T>

Tensor with shape [1, channels, height, width].

SaveBmp(Tensor<T>, string, bool)

Saves a tensor as a BMP image file.

public static void SaveBmp(Tensor<T> tensor, string filePath, bool denormalize = true)

Parameters

tensor Tensor<T>

Tensor with shape [1, channels, height, width] or [channels, height, width].

filePath string

Output file path.

denormalize bool

Whether to denormalize from [0, 1] to [0, 255].

SavePpm(Tensor<T>, string, bool, bool)

Saves a tensor as a PPM image file.

public static void SavePpm(Tensor<T> tensor, string filePath, bool denormalize = true, bool binary = true)

Parameters

tensor Tensor<T>

Tensor with shape [1, 3, height, width] or [3, height, width].

filePath string

Output file path.

denormalize bool

Whether to denormalize from [0, 1] to [0, 255].

binary bool

Whether to save as binary (P6) or ASCII (P3).