0

I need to detect some lines from a rasterized image. After having retrieved all the X and Y coordinates, I can manage to create the corresponding line equation for horizontal, vertical and barely diagonal lines perfectly.

But I've just stumbled upon one rare case:

rare case

I am giving the following X coordinates:

[[283.5], [283.5], [283.5], [283.5], [284.0], [284.5], [285.0], [285.5], [286.0],
[287.5], [287.5], [287.5], [288.0], [289.0], [289.0], [289.5], [289.5], [290.0],
[290.0], [291.0], [292.0], [292.0], [292.0], [293.0], [293.0], [294.0], [294.0],
[294.0], [294.0], [295.0], [296.0], [296.0], [296.0], [297.0], [297.0], [298.0],
[298.0], [298.0], [298.0], [299.0], [299.0], [300.0], [300.5], [300.5], [301.0],
[301.5], [302.0], [302.5], [302.5], [302.5], [302.5], [303.0], [303.5], [304.5],
[305.0], [305.5], [306.0], [306.5], [307.0], [307.0], [307.0], [307.0]]

With the following Y coordinates:

[215.0, 214.0, 213.0, 212.0, 211.0, 210.0, 209.0, 208.0, 207.0, 206.0, 205.0, 204.0,
203.0, 202.0, 201.0, 200.0, 199.0, 198.0, 197.0, 196.0, 195.0, 194.0, 193.0, 192.0,
191.0, 190.0, 189.0, 188.0, 187.0, 186.0, 185.0, 184.0, 183.0, 182.0, 181.0, 180.0,
179.0, 178.0, 177.0, 176.0, 175.0, 174.0, 173.0, 172.0, 171.0, 170.0, 169.0, 168.0,
167.0, 166.0, 165.0, 164.0, 163.0, 162.0, 161.0, 160.0, 159.0, 158.0, 157.0, 156.0,
155.0, 154.0]

And I get the following b value: 913.00073 With m being: -2.4654503

Giving the following result:

bad result

It seems that m should be positive instead.

I've already tried with sklearn's LinearRegression and doing it manually, giving the same equation results.

    def compute_line_equation_sklearn(self):
        X = []
        y = []

        for slice in self._slices:
            X.append([slice.center[1]])
            y.append(slice.center[0])

        lr = LinearRegression()
        reg = lr.fit(X, y)

        self._m = reg.coef_[0]
        self._b = reg.intercept_

    def compute_line_equation_manual(self):
        X = []
        pairs = []
        for current_slice in self._slices:
            X.append(current_slice.center[1])
            pairs.append([current_slice.center[1], current_slice.center[0]])

        sx = sy = sxx = sxy = syy = 0.0
        n = len(pairs)
        for x, y in pairs:
            sx = sx + x
            sy = sy + y
            sxx = sxx + x * x
            sxy = sxy + x * y
            syy = syy + y * y
        self._m = ((n * sxy) - (sx * sy)) / ((n * sxx) - sx ** 2)
        self._b = (sy - (self._m * sx)) / n
        self._r = ((n * sxy) - (sx * sy)) / (np.sqrt((n * sxx) - (sx ** 2)) *
                                             np.sqrt((n * syy) - (sy ** 2)))

Could you please give me any hints about what could be wrong in this situation ?

Axel Isouard
  • 1,498
  • 1
  • 24
  • 38

1 Answers1

2

In the image, the Y-axis increases downward, starting from 0 on the top. So, to get correct results flip the sign of Y-coordinates before calculating the slope.

Priyatham
  • 2,821
  • 1
  • 19
  • 33