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

AI Thread Summary
To find the line of best fit through the longest axis of a figure represented as a binary image, the discussion highlights the use of the polyfit() function for linear fitting. The key challenge is that polyfit requires unique x-axis values, and the current dataset contains multiple y-values for each x-value. A suggested solution is to average the repeated x-values to create two vectors, which would allow for the application of polyfit. The use of accumarray is recommended for this averaging process. Additionally, linear regression can be calculated even with multiple y-values for the same x-value, and principal component analysis is mentioned as an alternative method for determining the best fit.
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: 842
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.
 

Similar threads

Replies
15
Views
2K
Replies
2
Views
3K
Replies
5
Views
2K
Replies
1
Views
2K
Replies
5
Views
2K
Replies
1
Views
2K
Back
Top