Class ImageHelper<T>
Helper class for loading and saving images as tensors.
public static class ImageHelper<T>
Type Parameters
TThe 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
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
filePathstringPath to the image file.
normalizeboolWhether 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
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
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
filePathstringPath to the raw data file.
widthintImage width.
heightintImage height.
channelsintNumber of channels (1 for grayscale, 3 for RGB).
bytesPerChannelintBytes per channel (1 for 8-bit, 2 for 16-bit).
normalizeboolWhether 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
tensorTensor<T>Tensor with shape [1, channels, height, width] or [channels, height, width].
filePathstringOutput file path.
denormalizeboolWhether 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)