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

  • #1
3
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.8 KB · Views: 674

Answers and Replies

  • #2
kreil
Insights Author
Gold Member
668
68
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.
 
  • #3
3
0
Wouldn't polyfit require vectors for each data set? right now I have multiple y values for each x value.
 
  • #4
kreil
Insights Author
Gold Member
668
68
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.
 
  • #5
3
0
That should work since the figure is symmetrical, I'll give it a shot. Thanks
 
  • #6
FactChecker
Science Advisor
Gold Member
6,275
2,439
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.
 

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

Replies
4
Views
4K
  • Last Post
Replies
2
Views
4K
  • Last Post
Replies
22
Views
2K
Replies
4
Views
924
  • Last Post
Replies
5
Views
43K
Replies
0
Views
600
Replies
0
Views
1K
M
Top