检查 2d 点是否在曲线上
Check if 2d points are on a curved line
我的形象:
我正在尝试检测图像中的曲线 - 图中有堆叠的硬币。我想数平行曲线。大多数线条是不连续的。
假设我将 5 个点与 numpy.polyfit 一起使用,并获得描述直线的函数。
什么是最好的方法来搜索线并说这些点在第一行,那些点在第二行等。
我正在考虑尝试最小二乘法并将直线上下移动。我认为曲线是抛物线( ax^2 + bx + c ) - 移动它意味着移动顶点 x=-b/2a => y=a*(-b/2a)^2 + b*(-b/2a)+c.
import numpy as np
data = np.array([[0,0], [1,-1], [2, -2], [3,-1], [4,0]])
data_x = [k[0] for k in data ]
data_y = [k[1] for k in data ]
p = np.poly1d(np.polyfit(data_x, data_y, 2))
可以请谁帮我举例说明如何将图像中的点拟合到我刚刚找到的 p 中。我如何在这里应用最小二乘法?
提前致谢!
在互联网上阅读和挖掘了很多天之后,我找到了一个使用 lmfit 的非常优雅的解决方案。 https://lmfit.github.io/lmfit-py/ 我感谢这个模块的创作者,感谢他们所做的出色工作。
现在是将数据拟合到曲线的解决方案。
当我们有一个多项式 p
>>> p
poly1d([ 0.42857143, -1.71428571, 0.05714286])
使用这些参数创建一个 python 函数
def fu(x,a=0.4285,b=-1.71,c=0.057):
return x*x*a + x * b + c
现在我们可以使用该函数创建一个 lmfit 模型
>>> gmodel = Model(fu)
>>> gmodel.param_names
['a', 'c', 'b']
>>> gmodel.independent_vars
['x']
你可以看到它标识了自变量和参数。它将尝试更改参数,以便函数最适合数据。
>>> result = gmodel.fit(y_test, x=x_test)
>>> print(result.fit_report())
[[Model]]
Model(fu)
[[Fit Statistics]]
# function evals = 11
# data points = 8
# variables = 3
chi-square = 2.159
reduced chi-square = 0.432
Akaike info crit = -4.479
Bayesian info crit = -4.241
[[Variables]]
a: 0.12619047 +/- 0.050695 (40.17%) (init= 0.4285)
c: -0.55833335 +/- 0.553020 (99.05%) (init= 0.057)
b: -0.52857141 +/- 0.369067 (69.82%) (init=-1.71)
[[Correlations]] (unreported correlations are < 0.100)
C(a, b) = -0.962
C(c, b) = -0.793
C(a, c) = 0.642
完整 python 脚本:
import matplotlib.pyplot as plt
from lmfit import Model
import numpy as np
def fu(x,a=0.4285,b=-1.71,c=0.057):
return x*x*a + x * b + c
gmodel = Model(fu)
print "Params" , gmodel.param_names
print "Independent var", gmodel.independent_vars
params = gmodel.make_params()
print " Params prop", params
data_test = np.array([[0,0], [1,-1.2], [2, -2], [3,-1.3], [4,0], [5,0.5], [6,0.9], [7, 1.5]])
x_test = data_test[:,0]
y_test = data_test[:,1]
result = gmodel.fit(y_test, x=x_test)
print(result.fit_report())
plt.plot(x_test, y_test, 'bo')
plt.plot(x_test, result.init_fit, 'k--')
plt.plot(x_test, result.best_fit, 'r-')
plt.show()
我的形象:
我正在尝试检测图像中的曲线 - 图中有堆叠的硬币。我想数平行曲线。大多数线条是不连续的。
假设我将 5 个点与 numpy.polyfit 一起使用,并获得描述直线的函数。
什么是最好的方法来搜索线并说这些点在第一行,那些点在第二行等。
我正在考虑尝试最小二乘法并将直线上下移动。我认为曲线是抛物线( ax^2 + bx + c ) - 移动它意味着移动顶点 x=-b/2a => y=a*(-b/2a)^2 + b*(-b/2a)+c.
import numpy as np
data = np.array([[0,0], [1,-1], [2, -2], [3,-1], [4,0]])
data_x = [k[0] for k in data ]
data_y = [k[1] for k in data ]
p = np.poly1d(np.polyfit(data_x, data_y, 2))
可以请谁帮我举例说明如何将图像中的点拟合到我刚刚找到的 p 中。我如何在这里应用最小二乘法?
提前致谢!
在互联网上阅读和挖掘了很多天之后,我找到了一个使用 lmfit 的非常优雅的解决方案。 https://lmfit.github.io/lmfit-py/ 我感谢这个模块的创作者,感谢他们所做的出色工作。
现在是将数据拟合到曲线的解决方案。 当我们有一个多项式 p
>>> p
poly1d([ 0.42857143, -1.71428571, 0.05714286])
使用这些参数创建一个 python 函数
def fu(x,a=0.4285,b=-1.71,c=0.057):
return x*x*a + x * b + c
现在我们可以使用该函数创建一个 lmfit 模型
>>> gmodel = Model(fu)
>>> gmodel.param_names
['a', 'c', 'b']
>>> gmodel.independent_vars
['x']
你可以看到它标识了自变量和参数。它将尝试更改参数,以便函数最适合数据。
>>> result = gmodel.fit(y_test, x=x_test)
>>> print(result.fit_report())
[[Model]]
Model(fu)
[[Fit Statistics]]
# function evals = 11
# data points = 8
# variables = 3
chi-square = 2.159
reduced chi-square = 0.432
Akaike info crit = -4.479
Bayesian info crit = -4.241
[[Variables]]
a: 0.12619047 +/- 0.050695 (40.17%) (init= 0.4285)
c: -0.55833335 +/- 0.553020 (99.05%) (init= 0.057)
b: -0.52857141 +/- 0.369067 (69.82%) (init=-1.71)
[[Correlations]] (unreported correlations are < 0.100)
C(a, b) = -0.962
C(c, b) = -0.793
C(a, c) = 0.642
完整 python 脚本:
import matplotlib.pyplot as plt
from lmfit import Model
import numpy as np
def fu(x,a=0.4285,b=-1.71,c=0.057):
return x*x*a + x * b + c
gmodel = Model(fu)
print "Params" , gmodel.param_names
print "Independent var", gmodel.independent_vars
params = gmodel.make_params()
print " Params prop", params
data_test = np.array([[0,0], [1,-1.2], [2, -2], [3,-1.3], [4,0], [5,0.5], [6,0.9], [7, 1.5]])
x_test = data_test[:,0]
y_test = data_test[:,1]
result = gmodel.fit(y_test, x=x_test)
print(result.fit_report())
plt.plot(x_test, y_test, 'bo')
plt.plot(x_test, result.init_fit, 'k--')
plt.plot(x_test, result.best_fit, 'r-')
plt.show()