curve fitting

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

curve fitting

Kenneth Sloan-3
I'm about to have two curve/surface fitting problems.  They aren't necessarily
tied to images, per se - but I see (among other things) an ImageJ plugin called CurveFitter.

I'm interested in opinions:

Constraints:
 
 * I'd like to stick to Java code - either as an ImageJ plugin, or stand-alone.
    I can stumble along in R (and am interested in expanding my R skill set);
    MatLab is completely foreign to me (and I'm not eager to learn it)
    Java is "home" and I'm happy to tackle arbitrarily difficult problems there.

 * In 2D, I'm interested in both polynomial functions and also DoG (difference of Gaussians)
   I could probably live with low degree polynomials (perhaps 4-6?)
    - DoG might be an extravagance.  Of course, something that allowed for
   arbitrary code to define a function f(x) or g(x,y) (and derivatives) would work.

 * Once a fit is found, I'd like derivatives of the fitted function - I mostly care
    about zero-derivatives, but min/max are also of interest.  If I'm using a package
    that allowed/required me to write arbitrary f(x) and f'(x)...that's fine.

 * 3D is interesting, but probably a side project.  Here, I'd love to directly fit
    a smooth 2D surface embedded in 3D.  My functions are "almost radially symmetric" - close
    enough that a radially symmetric answer would be OK, but not optimal.  Again, it
    would be useful to have derivative information.  (hint: it appears that 2D DoG is a
    decent fit to the 2D data, but a 3D DoG is not)

 * assume that my 2D data is a set of x,y pairs (with x's being unique), and that
   my 3D data is a set of x,y,z triples (with unique x,y locations).  2D points are
   likely to be equally spaced in x; 3D points should NOT be assumed to follow any
   particular pattern (and certainly not form an "image" in x,y)

 * assume the data is available as a text file, probably in .csv format

For the 2D case, it's not clear to me that ImageJ is the right platform - but it looks like there has been some useful work done.  For the 3D case, ImageJ seems more likely, even though the data do not necessarily form an "image".

So far, I've found "CurveFitter.java" - an ImageJ plugin.  Comments on how well it fits my constraints, and hints on how to best use it would be most welcome.

I'm not sure if answers are of general interest.  Usually it's best to respond to the entire list.  If you prefer, feel free to contact me directly.

Thanks for your help!

--
Kenneth Sloan
[hidden email]
Vision is the art of seeing what is invisible to others.

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: curve fitting

Herbie
Dear Kenneth,

up to now I only used the ImageJ macro fit functionality with various
model functions, implemented ones and self-defined ones. I found that
the behavior is on a professional level that equals behavior I know from
KaleidaGraph. For general regression I think both use the same approach.

HTH

Best regards

Herbie

:::::::::::::::::::::::::::::::::::::::::::
Am 14.12.17 um 18:11 schrieb Kenneth Sloan:

> I'm about to have two curve/surface fitting problems.  They aren't necessarily
> tied to images, per se - but I see (among other things) an ImageJ plugin called CurveFitter.
>
> I'm interested in opinions:
>
> Constraints:
>  
>   * I'd like to stick to Java code - either as an ImageJ plugin, or stand-alone.
>      I can stumble along in R (and am interested in expanding my R skill set);
>      MatLab is completely foreign to me (and I'm not eager to learn it)
>      Java is "home" and I'm happy to tackle arbitrarily difficult problems there.
>
>   * In 2D, I'm interested in both polynomial functions and also DoG (difference of Gaussians)
>     I could probably live with low degree polynomials (perhaps 4-6?)
>      - DoG might be an extravagance.  Of course, something that allowed for
>     arbitrary code to define a function f(x) or g(x,y) (and derivatives) would work.
>
>   * Once a fit is found, I'd like derivatives of the fitted function - I mostly care
>      about zero-derivatives, but min/max are also of interest.  If I'm using a package
>      that allowed/required me to write arbitrary f(x) and f'(x)...that's fine.
>
>   * 3D is interesting, but probably a side project.  Here, I'd love to directly fit
>      a smooth 2D surface embedded in 3D.  My functions are "almost radially symmetric" - close
>      enough that a radially symmetric answer would be OK, but not optimal.  Again, it
>      would be useful to have derivative information.  (hint: it appears that 2D DoG is a
>      decent fit to the 2D data, but a 3D DoG is not)
>
>   * assume that my 2D data is a set of x,y pairs (with x's being unique), and that
>     my 3D data is a set of x,y,z triples (with unique x,y locations).  2D points are
>     likely to be equally spaced in x; 3D points should NOT be assumed to follow any
>     particular pattern (and certainly not form an "image" in x,y)
>
>   * assume the data is available as a text file, probably in .csv format
>
> For the 2D case, it's not clear to me that ImageJ is the right platform - but it looks like there has been some useful work done.  For the 3D case, ImageJ seems more likely, even though the data do not necessarily form an "image".
>
> So far, I've found "CurveFitter.java" - an ImageJ plugin.  Comments on how well it fits my constraints, and hints on how to best use it would be most welcome.
>
> I'm not sure if answers are of general interest.  Usually it's best to respond to the entire list.  If you prefer, feel free to contact me directly.
>
> Thanks for your help!
>
> --
> Kenneth Sloan
> [hidden email]
> Vision is the art of seeing what is invisible to others.
>
> --
> 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: curve fitting

Michael Schmid-3
In reply to this post by Kenneth Sloan-3
Hi Kenneth,

that's quite a few questions!

In general, my experience is that the CurveFitter of ImageJ can handle
two parameters in almost any case, three parameters work quite well,
four or five parameters is only possible for some problems. Most of the
built-in fits eliminate one or two parameters by linear regression;
these eliminated parameters do not count.
The CurveFitter of ImageJ does not use any derivatives, so (apart from
the parameters eliminated by linear regression) it does not care whether
the function is linear in the parameters or not.

Polynomials (two parameters are eliminated by linear regression) - this
is built in into ImageJ and usually works reasonably well up to 4th or
5th order. For 6th order, the x points should be spread around x=0. For
rules of thumb what will work, see the notes in the CurveFitter source code.

For 2D polynomials (images) there is a 'Fit Polynomial' plugin [1],
which does not use an iterative solution but an analytical method, so it
goes to higher order. You might also try misusing it for an image
consisting of n*1 pixels (set all orders except 'x' to zero and click on
'output fit').

Difference of Gaussians - this gives you five parameters:
- offset, height, center, and two widths.
For good performance, you should implement it in Java and use linear
regression to eliminate two (offset and height) [2], then I guess it
would work reasonably well.

Derivatives: For polynomials or DoG you can do that analytically.
Analytically finding the zero of a polynomial is easy up to 2nd order
(square equation). For higher orders, you may have a look at the EJML
(efficient java matrix library). If you need less accuracy, of course
you can do numeric differentiation.
I am not aware of an ImageJ method that finds the zero of a general
function - but it would be nice if someone could implement e.g. Ridders
method!
You will probably find several programs for this on the internet; e.g.
org.apache.commons.math.analysis.RiddersSolver

2D/3D: I don't think there is a finished solution, but of course you can
use the ImageJ Minimizer also for this purpose. The easiest would be to
map the 2D coordinates (x, y) to the same number of points in 1D (e.g.
x+y*width for a rectangular grid) and use the ImageJ Curve fitter. This
would have the advantage that you can eliminate constant offset and a
multiplicative parameter by linear regression. Nevertheless, remember
that the number of fitting parameters must not be too high!

Usually, for 2D images, at least as a first approximation, it is easier
to use methods other than curve fitting, e.g. template matching or
deconvolution as a first approximation.

If you want to go beyond the ImageJ Curve Fitter, you may also have a
look at the possibilities of the Jama or EJML package for linear fitting.

For nonlinear fitting beyond ImageJ:
I have no experience with R at all.
Octave/Mathlab certainly has more refined algorithms compared with the
simple Minimizer of ImageJ. I have no experience how it compares to ImageJ.

--

Reading values into ImageJ: ImageJ reads .csv files into a Results table:
   Opener.openResultsTable(String path)
You can then extract the columns with
   ResultsTable.getResultsTable().getColumn(int column)


Michael

[1]
http://imagejdocu.tudor.lu/doku.php?id=plugin:filter:fit_polynomial:start

[2] Use the setOffsetMultiplySlopeParams method of the curve fitter to
specify which parameter numbers are the offset and height.

[3] http://math.nist.gov/javanumerics/jama/
________________________________________________________________


On 14/12/2017 18:11, Kenneth Sloan wrote:

> I'm about to have two curve/surface fitting problems.  They aren't necessarily
> tied to images, per se - but I see (among other things) an ImageJ plugin called CurveFitter.
>
> I'm interested in opinions:
>
> Constraints:
>  
>   * I'd like to stick to Java code - either as an ImageJ plugin, or stand-alone.
>      I can stumble along in R (and am interested in expanding my R skill set);
>      MatLab is completely foreign to me (and I'm not eager to learn it)
>      Java is "home" and I'm happy to tackle arbitrarily difficult problems there.
>
>   * In 2D, I'm interested in both polynomial functions and also DoG (difference of Gaussians)
>     I could probably live with low degree polynomials (perhaps 4-6?)
>      - DoG might be an extravagance.  Of course, something that allowed for
>     arbitrary code to define a function f(x) or g(x,y) (and derivatives) would work.
>
>   * Once a fit is found, I'd like derivatives of the fitted function - I mostly care
>      about zero-derivatives, but min/max are also of interest.  If I'm using a package
>      that allowed/required me to write arbitrary f(x) and f'(x)...that's fine.
>
>   * 3D is interesting, but probably a side project.  Here, I'd love to directly fit
>      a smooth 2D surface embedded in 3D.  My functions are "almost radially symmetric" - close
>      enough that a radially symmetric answer would be OK, but not optimal.  Again, it
>      would be useful to have derivative information.  (hint: it appears that 2D DoG is a
>      decent fit to the 2D data, but a 3D DoG is not)
>
>   * assume that my 2D data is a set of x,y pairs (with x's being unique), and that
>     my 3D data is a set of x,y,z triples (with unique x,y locations).  2D points are
>     likely to be equally spaced in x; 3D points should NOT be assumed to follow any
>     particular pattern (and certainly not form an "image" in x,y)
>
>   * assume the data is available as a text file, probably in .csv format
>
> For the 2D case, it's not clear to me that ImageJ is the right platform - but it looks like there has been some useful work done.  For the 3D case, ImageJ seems more likely, even though the data do not necessarily form an "image".
>
> So far, I've found "CurveFitter.java" - an ImageJ plugin.  Comments on how well it fits my constraints, and hints on how to best use it would be most welcome.
>
> I'm not sure if answers are of general interest.  Usually it's best to respond to the entire list.  If you prefer, feel free to contact me directly.
>
> Thanks for your help!
>
> --
> Kenneth Sloan
> [hidden email]
> Vision is the art of seeing what is invisible to others.
>
> --
> 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: curve fitting

Kenneth Sloan-3
Michael and Herbie-

Thanks - that was very helpful.

My current plan is to work on my R skills, with the idea of doing this in R.

--
Kenneth Sloan
[hidden email]
Vision is the art of seeing what is invisible to others.

 

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