The original linear fit is probably inaccurate for any set of points with duplicate x values, this is most prominent in those with many similar x values. My understanding of this can be illustrated by considering a vertical perimeter segment. Say we have pixels all along the line x=0
, through y values {1,2,3,4,5}
; what average value of y does a line through these points take at x=0
?, 3. Thus we yield the constant function F[x]=3
, but this line is a tangent to the fit we want!
As I say, this occurs for any multiple valued x axis data points, and thus influences the majority of our fits. This can be seen when the gradient by average division (my last post) is compared to the original fits. Most results are similar, but not identical!
If you extract and rewrite the average division gradient algorithm, trying to coax the trigonometry towards the data (at present arctan is one of the last things to be applied), I expect that the division by zero errors can be navigated safely. One solution would indeed be to express the vector for each line as the average of the vectors between the constituent points, and then pass the vectors to the vectorangle function and let it worry about all the zero division and infinities.
Once again, I doubt even a well implemented linear fit is what you ultimately want to use given the curvature of these shapes. I'd encourage you to take the cornerpoints data and try algorithms on a small set until you find something you're happy with. My original work was mostly concerned with extracting the correct data, turning pixel data into perimeter cycles.