我需要从光栅化的图像中检测出一些线条。检索完所有X和Y坐标后,我可以完美地为水平,垂直和几乎对角线创建相应的线方程。
但是我偶然发现了一种罕见的情况:
我给出以下X坐标:
[[283.5],[283.5],[284.0],[284.5],[285.0],[285.5],[286.0],[287.5],[288.0],[289.0],[289.5],[290.0],[291.0],[292.0],[293.0],[294.0],[295.0],[296.0],[297.0],[298.0],[299.0],[300.0],[300.5],[301.0],[301.5],[302.0],[302.5],[303.0],[303.5],[304.5],[305.0],[305.5],[306.0],[306.5],[307.0],[307.0]]
具有以下Y坐标:
[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]
然后我得到以下b
值:913.00073
m
为:-2.4654503
给出以下结果:
似乎m
应该是肯定的。
我已经尝试过使用sklearn的LinearRegression并手动进行操作,给出相同的方程结果。
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)))
您能给我任何提示在这种情况下可能出什么问题吗?