Table of Contents

Class Affine<T>

Namespace
AiDotNet.Augmentation.Image
Assembly
AiDotNet.dll

Applies random affine transformations (rotation, scale, shear, translation) to an image.

public class Affine<T> : SpatialAugmentationBase<T, ImageTensor<T>>, ISpatialAugmentation<T, ImageTensor<T>>, IAugmentation<T, ImageTensor<T>>

Type Parameters

T

The numeric type for calculations.

Inheritance
Affine<T>
Implements
Inherited Members

Remarks

Affine transformation is a general geometric transformation that preserves lines and parallelism. It combines rotation, scaling, shearing, and translation in a single operation, providing a powerful way to augment images with realistic geometric variations.

For Beginners: Think of this as combining multiple geometric operations into one. The image can be rotated, stretched, tilted (sheared), and moved around - all at once. This creates more diverse training examples than applying each transformation separately.

When to use:

  • General image classification with geometric invariance
  • Object detection where objects may be viewed from different angles
  • When you need combined geometric variations efficiently

Constructors

Affine((double Min, double Max)?, (double Min, double Max)?, (double Min, double Max)?, (double X, double Y)?, double, InterpolationMode, BorderMode, T?)

Creates a new affine transformation augmentation.

public Affine((double Min, double Max)? rotationRange = null, (double Min, double Max)? scaleRange = null, (double Min, double Max)? shearRange = null, (double X, double Y)? translationRange = null, double probability = 0.5, InterpolationMode interpolation = InterpolationMode.Bilinear, BorderMode borderMode = BorderMode.Reflect, T? borderValue = default)

Parameters

rotationRange (double Pitch, double Confidence)?

The range of rotation angles in degrees (min, max). Industry standard default is (-15, 15).

scaleRange (double Pitch, double Confidence)?

The range of scale factors (min, max). Industry standard default is (0.9, 1.1).

shearRange (double Pitch, double Confidence)?

The range of shear angles in degrees (min, max). Industry standard default is (-10, 10).

translationRange (double Pitch, double Confidence)?

The range of translation as fraction of image dimensions (x, y). Industry standard default is (0.1, 0.1) meaning up to 10% in each direction.

probability double

The probability of applying this augmentation (0.0 to 1.0). Industry standard default is 0.5.

interpolation InterpolationMode

The interpolation mode for sampling pixels. Industry standard default is Bilinear.

borderMode BorderMode

How to fill pixels that fall outside the original image bounds. Industry standard default is Reflect.

borderValue T

The constant value to use when borderMode is Constant.

Properties

BorderMode

Gets the border mode when pixels fall outside the original image bounds.

public BorderMode BorderMode { get; }

Property Value

BorderMode

BorderValue

Gets the constant value used when BorderMode is Constant.

public T BorderValue { get; }

Property Value

T

Interpolation

Gets the interpolation mode for pixel sampling.

public InterpolationMode Interpolation { get; }

Property Value

InterpolationMode

RotationRange

Gets the rotation angle range in degrees.

public (double Min, double Max) RotationRange { get; }

Property Value

(double min, double max)

ScaleRange

Gets the scale factor range.

public (double Min, double Max) ScaleRange { get; }

Property Value

(double min, double max)

ShearRange

Gets the shear angle range in degrees.

public (double Min, double Max) ShearRange { get; }

Property Value

(double min, double max)

TranslationRange

Gets the translation range as a fraction of image dimensions.

public (double X, double Y) TranslationRange { get; }

Property Value

(double min, double max)

Methods

ApplyWithTransformParams(ImageTensor<T>, AugmentationContext<T>)

Applies the affine transformation and returns transform parameters.

protected override (ImageTensor<T> data, IDictionary<string, object> parameters) ApplyWithTransformParams(ImageTensor<T> data, AugmentationContext<T> context)

Parameters

data ImageTensor<T>
context AugmentationContext<T>

Returns

(ImageTensor<T> data, IDictionary<string, object> parameters)

GetParameters()

Gets the parameters of this augmentation.

public override IDictionary<string, object> GetParameters()

Returns

IDictionary<string, object>

A dictionary of parameter names to values.

TransformBoundingBox(BoundingBox<T>, IDictionary<string, object>, AugmentationContext<T>)

Transforms a bounding box after affine transformation.

protected override BoundingBox<T> TransformBoundingBox(BoundingBox<T> box, IDictionary<string, object> transformParams, AugmentationContext<T> context)

Parameters

box BoundingBox<T>
transformParams IDictionary<string, object>
context AugmentationContext<T>

Returns

BoundingBox<T>

TransformKeypoint(Keypoint<T>, IDictionary<string, object>, AugmentationContext<T>)

Transforms a keypoint after affine transformation.

protected override Keypoint<T> TransformKeypoint(Keypoint<T> keypoint, IDictionary<string, object> transformParams, AugmentationContext<T> context)

Parameters

keypoint Keypoint<T>
transformParams IDictionary<string, object>
context AugmentationContext<T>

Returns

Keypoint<T>

TransformMask(SegmentationMask<T>, IDictionary<string, object>, AugmentationContext<T>)

Transforms a segmentation mask after affine transformation.

protected override SegmentationMask<T> TransformMask(SegmentationMask<T> mask, IDictionary<string, object> transformParams, AugmentationContext<T> context)

Parameters

mask SegmentationMask<T>
transformParams IDictionary<string, object>
context AugmentationContext<T>

Returns

SegmentationMask<T>