2D Gaussian fitting core for image/bitmap inputs (.NET Standard 2.0). Levenberg–Marquardt based.
$ dotnet add package GaussianFit.CoreA small .NET Standard 2.0 library for 2D Gaussian fitting from System.Drawing.Bitmap inputs.
Designed for laser / beam profile analysis, with optional ROI-based fitting while preserving
original image coordinates.
Works on .NET Framework 4.6.1+ and modern .NET (6/7/8).
dotnet add package GaussianFit.Core
using GaussianFit.Core;
var result = GaussianFitter.Run("beam.png", new FitOptions {
Mode = PrepMode.EdgeMean_BorderPct,
BorderPercent = 10,
CutoffPercent = 30,
MaxIterations = 200
});
Console.WriteLine(result.ReportText);
Example output:
A (amplitude) = 215.63
x0, y0 = 123.4, 98.5
σx, σy = 12.1, 11.9
RMSE = 0.024
nRMSE (RMSE/A) = 1.12 % [Excellent]
You can limit the fitting area using a Region of Interest (ROI) while still obtaining the beam center in original image coordinates.
using GaussianFit.Core;
var roi = new RoiRect(x: 202, y: 163, w: 750, h: 750);
var result = GaussianFitter.Run("beam.png", roi, new FitOptions {
Mode = PrepMode.EdgeMean_BorderPct,
BorderPercent = 10,
CutoffPercent = 30,
MaxIterations = 200
});
// Center in ORIGINAL image coordinates
Console.WriteLine($"Center (orig): x={result.X0:F2}, y={result.Y0:F2}");
Internally:
(x0, y0) is automatically converted back to the original image coordinate systemThis avoids edge-light contamination while keeping coordinates consistent.
| Property | Description | Default |
|---|---|---|
Mode | Preprocessing mode (EdgeMean_BorderPct or Percentile_GlobalP) | EdgeMean_BorderPct |
BorderPercent | Border area (%) used for background mean | 10.0 |
PercentileP | Percentile threshold (0.1–50.0%) for Percentile_GlobalP mode | 10.0 |
CutoffPercent | Minimum intensity (%) for fitting samples | 30.0 |
MaxIterations | Maximum Levenberg–Marquardt iterations | 200 |
Epsilon | Stop criterion ( ΔSSE / SSE) | 1e-9 |
The 2D Gaussian model is defined as:
f(x, y) = A · exp( -((x − x₀)² / (2σₓ²) + (y − y₀)² / (2σᵧ²)) ) + C
where:
| Symbol | Description |
|---|---|
| f(x, y) | Fitted Gaussian intensity value at coordinates (x, y) |
| A | Amplitude (peak value above background) |
| x₀, y₀ | Center position (pixel coordinates) |
| σₓ, σᵧ | Standard deviations (beam spread) |
| C | Constant background offset |
The library minimizes the Sum of Squared Errors (SSE) between the measured data and the model using a Levenberg–Marquardt optimizer.
When drawing overlays (as in the WinForms demo):
Overlay coordinates are provided in ROI-local space when ROI is used, which simplifies drawing on cropped preview images.
(x0, y0)FitResultMIT License © 2025 Napat Sutikant