Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB : How to find the line of best fit through a binary image?

  1. Jun 30, 2014 #1
    I have found the 2D Fourier transform of a figure and then changed it to a binary image (attached).

    I want to find the line of best fit through the longest "axis"? of this figure. What would be the simplest way to accomplish this?

    Attached Files:

  2. jcsd
  3. Jun 30, 2014 #2


    User Avatar
    Gold Member

    You can use polyfit() for best fit lines:

    Code (Text):

    p = polyfit(xdata, ydata, N)
    xdata: the x-axis values
    ydata: the function values corresponding to the values in xdata
    N: degree of the fit

    For a linear fit, use N = 1.
  4. Jun 30, 2014 #3
    Wouldn't polyfit require vectors for each data set? right now I have multiple y values for each x value.
  5. Jun 30, 2014 #4


    User Avatar
    Gold Member

    Yes you're absolutely right. I just looked quickly; polyfit doesn't work when there are repeated (or nearly repeated) xdata points.

    Is it feasible to average all of the repeated xdata points so that you just have 2 vectors with 1 function value for each x value? Then you can use polyfit with that data set. It seems to me like it might produce something reasonable, but I'm not a statistics expert.

    If so, you can use accumarray to perform the averaging.
  6. Jun 30, 2014 #5
    That should work since the figure is symmetrical, I'll give it a shot. Thanks
  7. Jul 13, 2014 #6


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    If you are talking about a linear line, you can calculate the linear regression of Y as a function of X. Because it works when random errors are added to Y, there can be multiple Y values for the same X value. Alternatively, the principle component that can be calculated through the data set.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook