Getting the equation out of a fitted curve in ImageJ

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Getting the equation out of a fitted curve in ImageJ

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:
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");
}
Reply | Threaded
Open this post in threaded view
|

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

Michael Entrup
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
Reply | Threaded
Open this post in threaded view
|

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

Marek Szeles
Hello Michael,
I hope you'll excuse my impoliteness in the original post, as I was fully incapacitated by the problem.

I would like to thank you most dearly for solving my problem, which was, as you correctly hinted, caused by my misunderstanding of the functions. Hopefully I'll be wiser the next time round.

Once again thank you for your quick and insightful reply,

Best Regards,
Marek Szeles