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

In summary, the conversation discusses finding the line of best fit through a figure that has been transformed to a binary image. The recommended method is to use polyfit() with xdata and ydata vectors, with a degree of fit of N=1 for a linear fit. However, there is a potential issue with repeated xdata points, which can be solved by averaging the values using accumarray. Another option is to calculate the linear regression of Y as a function of X.
  • #1
tbeta006
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.5 KB · Views: 781
Physics news on Phys.org
  • #2
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.
 
  • #3
Wouldn't polyfit require vectors for each data set? right now I have multiple y values for each x value.
 
  • #4
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
That should work since the figure is symmetrical, I'll give it a shot. Thanks
 
  • #6
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.
 

1. How do I import and read a binary image in MATLAB?

To import and read a binary image in MATLAB, you can use the imread function. This function takes in the path of the image file as an input and returns a matrix containing the pixel values of the image.

2. What is the line of best fit in a binary image?

The line of best fit in a binary image is a straight line that represents the most accurate trend or pattern in the data points of the image. It is used to summarize the relationship between two variables in a binary image.

3. How can I find the line of best fit in a binary image using MATLAB?

To find the line of best fit in a binary image using MATLAB, you can use the polyfit function. This function takes in the x and y coordinates of the data points in the image and returns the coefficients of the line of best fit.

4. Can I customize the line of best fit in a binary image?

Yes, you can customize the line of best fit in a binary image by specifying additional parameters in the polyfit function. These parameters include the degree of the polynomial, the type of fit (linear or nonlinear), and the weighting of the data points.

5. Is the line of best fit always accurate in a binary image?

No, the line of best fit is not always accurate in a binary image. It is dependent on the quality and quantity of data points in the image. If there are outliers or a small number of data points, the line of best fit may not accurately represent the trend in the image.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
15
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
818
  • General Math
Replies
2
Views
151
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
Back
Top