# Robust line regression: fails with horizontal lines?

**URL:** <https://forums.openmv.io/t/robust-line-regression-fails-with-horizontal-lines/9667>\
**Category:** OpenMV Boards\
**Created:** [June 6, 2024, 12:32pm UTC](https://forums.openmv.io/t/robust-line-regression-fails-with-horizontal-lines/9667 "2024-06-06T12:32:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![calocedrus](https://avatars.discourse-cdn.com/v4/letter/c/bbe5ce/32.png) [@calocedrus](https://forums.openmv.io/u/calocedrus)\
**Post date:** [June 6, 2024, 12:32pm UTC](https://forums.openmv.io/t/robust-line-regression-fails-with-horizontal-lines/9667/1 "2024-06-06T12:32:56Z")

</div>

Hi,  
The issue is mentioned in a reply [here](https://forums.openmv.io/t/detecting-and-navigating-in-a-matrix/1887/13),  
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](https://github.com/openmv/openmv/blob/master/src/omv/imlib/stats.c#L1118):

`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.

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [June 6, 2024, 6:31pm UTC](https://forums.openmv.io/t/robust-line-regression-fails-with-horizontal-lines/9667/2 "2024-06-06T18:31:55Z")

</div>

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)](https://github.com/openmv/openmv/issues/886)

---

<div class="post-metadata">

**Author:** ![calocedrus](https://avatars.discourse-cdn.com/v4/letter/c/bbe5ce/32.png) [@calocedrus](https://forums.openmv.io/u/calocedrus)\
**Post date:** [June 8, 2024, 2:41pm UTC](https://forums.openmv.io/t/robust-line-regression-fails-with-horizontal-lines/9667/3 "2024-06-08T14:41:25Z")

</div>

Note that I wasn’t complaining at all 🙂 (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?

```auto
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`  
).

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [June 8, 2024, 7:01pm UTC](https://forums.openmv.io/t/robust-line-regression-fails-with-horizontal-lines/9667/4 "2024-06-08T19:01:43Z")

</div>

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