Question

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 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");

}
Was it helpful?

Solution

As hinted above, I already got an answer elsewhere. Nonetheless, I'd like to also keep it here for the record. Basically, the answer is already included in the original post, as it prints the individual variables into the "Log" window.

For the third-degree polynomial, I could have just used:

Fit.doFit(2, x, y); // 2 is 3rd Degree Polynomial
Fit.plot();
rename("Calibrating curve");

And then the can be extracted easily as thus:

a = Fit.p(0);
b = Fit.p(1);
c = Fit.p(2);
d = Fit.p(3);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top