Login  Register

Getting the equation out of a fitted curve in ImageJ

Posted by Marek Szeles on Feb 21, 2014; 11:56am
URL: http://imagej.273.s1.nabble.com/Getting-the-equation-out-of-a-fitted-curve-in-ImageJ-tp5006617.html

I am analysing gafchromic filters in a freeware called ImageJ, which uses a simplified form of Java to write macros.

I have a set of datapoints I have successfully connected with different methods and have decided that a third degree polynomial fits the data best, however I need to work with the actual curve, so I need to somehow extract the equation/formula of said polynomial. This should be possible as the variables defining the polynomial are listed on the generated graph, however I can't seem to find a way to extract them in the code.

Here's what i get:
Curve

And here's my code so far:

n = nResults();
x = newArray(n);
for (i=0; i<x.length; i++)
{
    x[i] = getResult("Grays ", i);
}

y = newArray(n);
for (i=0; i<y.length; i++)
{
    y[i] = getResult("Mean ", i);

}


// Do all possible fits, plot them and add the plots to a stack
setBatchMode(true);
for (i = 0; i < Fit.nEquations; i++) {
 Fit.doFit(i, x, y);
 Fit.plot();
 if (i == 0)
     stack = getImageID;
 else {
     run("Copy");
     close();
     selectImage(stack);
     run("Add Slice");
     run("Paste");
 }
 Fit.getEquation(i, name, formula);
 print(""); print(name+ " ["+formula+"]");
 print("   R^2="+d2s(Fit.rSquared,3));
 for (j=0; j<Fit.nParams; j++)
     print("   p["+j+"]="+d2s(Fit.p(j),6));
 }
 setBatchMode(false);
 run("Select None");
 rename("Curve Fits");
}