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

Join the discussion
Registration is free. Start your own thread to ask a follow-up.
5 replies · 8K views
tbeta006
Messages
3
Reaction score
0
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?
 
Attachments
  • BinaryImage.PNG
    BinaryImage.PNG
    8.5 KB · Views: 883
Physics news on Phys.org
You can use polyfit() for best fit lines:

Code:
p = polyfit(xdata, ydata, N)

xdata: the x-axis values
ydata: the function values corresponding to the values in xdata
N: degree of the fitFor a linear fit, use N = 1.
 
Wouldn't polyfit require vectors for each data set? right now I have multiple y values for each x value.
 
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.
 
That should work since the figure is symmetrical, I'll give it a shot. Thanks
 
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.