use of uk.ac.sussex.gdsc.smlm.fitting.FitResult in project GDSC-SMLM by aherbert.
the class GaussianFit method fit.
/**
* Fits a single 2D Gaussian to the data. The fit is initialised at the highest value and then
* optimised.
*
* <p>Data must be arranged in yx block order, i.e. height rows of width.
*
* <p>The angle parameter is only set if using elliptical Gaussian fitting.
*
* <p>Note: The returned fit coordinates should be offset by 0.5 if the input data represents
* pixels
*
* @param data the data
* @param width the width
* @param height the height
* @return Array containing the fitted curve data: Background, Amplitude, PosX, PosY, StdDevX,
* StdDevY, Angle. Null if no fit is possible.
*/
@Nullable
public double[] fit(float[] data, int width, int height) {
if (data == null || data.length != width * height) {
return null;
}
// Get the limits
float max = Float.MIN_VALUE;
int maxIndex = -1;
for (int i = data.length; i-- > 0; ) {
final float f = data[i];
if (max < f) {
max = f;
maxIndex = i;
}
}
if (maxIndex < 0) {
return null;
}
final Gaussian2DFitter gf = createGaussianFitter(false);
final FitResult fitResult = gf.fit(SimpleArrayUtils.toDouble(data), width, height, new int[] { maxIndex });
if (fitResult.getStatus() == FitStatus.OK) {
chiSquared = fitResult.getError();
final double[] params = fitResult.getParameters();
// Check bounds
final double x = params[Gaussian2DFunction.X_POSITION];
final double y = params[Gaussian2DFunction.Y_POSITION];
if (x < 0 || x >= width || y < 0 || y >= height) {
return null;
}
// Re-arrange order for backwards compatibility with old code.
final double background = params[Gaussian2DFunction.BACKGROUND];
final double intensity = params[Gaussian2DFunction.SIGNAL];
final double sx = params[Gaussian2DFunction.X_SD];
final double sy = params[Gaussian2DFunction.Y_SD];
final double angle = params[Gaussian2DFunction.ANGLE];
final double amplitude = Gaussian2DPeakResultHelper.getAmplitude(intensity, sx, sy);
return new double[] { background, amplitude, x, y, sx, sy, angle };
}
return null;
}
Aggregations