# fitting rectangle through data points

38 views (last 30 days)
ha ha on 25 May 2019
Commented: darova on 31 May 2019
Let's say I have the data as shown in OXY plane(Here, I show 2 examples):
How can I fit the rectangle through data:

darova on 26 May 2019
If your data always has V form (look like triangle). Tree corners can be found. Find farthest corners using pdist2()
Use wikipedia formula to find the 3rd corner of a triangle (wiki)
Divide points into 2 groups. If distance point-corner smaller than radius:
Use polyfit() to calculate coeffiecients of line. What if angle between calculated lines is not 90 degree?
darova on 31 May 2019
Use pdist2() to find farthest corners. Draw a line from those points. Find another corners
Divide points into 4 sets using some tolerance distance to lines
Apply polyfit() to each set
It's the best i can

Image Analyst on 25 May 2019
What I would try first is to use principal components analysis ("PCA") to identify the two axes. Use pca() if you have the stats toolbox. Then you can fit lines with polyfit() going through the points along each PC. Use the points at the limits to determine where the lines start and stop. Sorry, no I don't have code that does this already - I'd have to develop it just as you would.
Another method you might also try is RANSAC.
ha ha on 25 May 2019
Thanks @Image Analyst for your kind.
Can you show example code of rectangle fitting using pca? I try to follow your advice but I am not totally understand.