Robust line regression: fails with horizontal lines?

Hi,
The issue is mentioned in a reply here,
but I’m not sure if it has been solved, I still find the “robust” line regression to be unreliable with perfectly horizontal lines so I thought I’d start a new topic.

Looking at the C code which does robust line regression, in particular this part:

float rotation = (mdx ? fast_atan2f(mdy, mdx) : 1.570796f) + 1.570796f;

Am I correct to say that for horizontal lines, mdx isn’t 0, but mdy would be close to 0 so the result of the above would be close to 1.57 which is 90°?

I don’t know yet how to test a change in the C code so that it reflects in the micropython code of OpenMV but I’ll start studying that topic.

Yeah, that’s had a bug forever for me to fix. No one’s complained though for a while so I didn’t prioritize:

Robust Linear Regression not stable with horizontal line. · Issue #886 · openmv/openmv (github.com)

Note that I wasn’t complaining at all :slight_smile: (you should be the one to complain, given all the questions and requests you get from everyone!).
The robust version of line regression works better in some situations.

How about rotation being something along these lines?

float rotation = 0f;
if (mdx == 0)
    rotation = 1.570796f; // case of a vertical line, pi/2 
else{
    if (mdy == 0)
        rotation = 0.0f; // mdy =0 is a horizontal line 
    else
        fast_atan2f(mdy, mdx);
}

There’s a pi/2 added in the original ternary expression and it may be needed above

(this is the original rotation:
float rotation = (mdx ? fast_atan2f(mdy, mdx) : 1.570796f) + 1.570796f; // PI/2
).

Yeah, it’s probably not that simple… since it collapses near the horizontal line. But, yeah, it’s something like that.