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

• MATLAB
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
8.8 KB · Views: 674

kreil
Gold Member
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 fit

For 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.

kreil
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.

That should work since the figure is symmetrical, I'll give it a shot. Thanks

FactChecker