Login  Register

Re: Getting the equation out of a fitted curve in ImageJ

Posted by Michael Entrup on Feb 21, 2014; 2:19pm
URL: http://imagej.273.s1.nabble.com/Getting-the-equation-out-of-a-fitted-curve-in-ImageJ-tp5006617p5006618.html

Hi Marek,

I'm a bit confused. I think you have already solved your problem. You
are using 'Fit.p(j)' to access a,b,... If I use your script, my Log
windows shows

    3rd Degree Polynomial [y = a+bx+cx^2+dx^3]
    R^2=0.995
    p[0]=-1.283063
    p[1]=0.939838
    p[2]=0.033200
    p[3]=-0.000998

If you only use

    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);
    }
    Fit.doFit(2, x, y); // 2 is 3rd Degree Polynomial
    a = Fit.p(0);
    b = Fit.p(1);
    c = Fit.p(2);
    d = Fit.p(3);

there is everything you want. With 'Fit.f(x) ' you can even use the
fitted equation for calculations. If you want ImageJ to choose the best
fit automatically (highest R^2), then the code will be a bit more complex.

Best regards
Michael Entrup



Am 21.02.2014 12:56, schrieb Marek Szeles:

> 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:
> <http://imagej.1557.x6.nabble.com/file/n5006617/curve_fit.jpg>
>
> 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");
> }
>
>
>
> --
> View this message in context: http://imagej.1557.x6.nabble.com/Getting-the-equation-out-of-a-fitted-curve-in-ImageJ-tp5006617.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html