适应形状围绕数据点

huangapple go评论105阅读模式
英文:

Fit a shape enclosing data points

问题

我有一些数据点,如图所示:
适应形状围绕数据点

我想拟合一个包围所有这些数据点的曲线,例如椭圆或圆。我应该如何做?

英文:

I have some data points as shown in the image:
适应形状围绕数据点

I want to fit a curve that encloses all these data points, e.g. an ellipse or a circle. How do I do this?

答案1

得分: 1

你寻找的是一个凸包。你可以使用scipy模块中的ConvexHull

  1. import matplotlib.pyplot as plt
  2. from scipy.spatial import ConvexHull
  3. hull = ConvexHull(points)
  4. plt.scatter(points[:, 0], points[:, 1])
  5. for simplex in hull.simplices:
  6. plt.plot(points[simplex, 0], points[simplex, 1], 'k-')
  1. >>> points
  2. # x y
  3. array([[ 1.85804999, -0.05672655],
  4. [-1.26377587, -0.71670609],
  5. [ 1.20926884, -0.90475264],
  6. ...,
  7. [-0.90567066, -0.84883138],
  8. [-0.43970639, 1.84684422],
  9. [ 0.19636511, 1.93878809]])
  10. >>> hull.vertices
  11. array([413, 129, 655, 254, 841, 793, 321, 526, 570, 695, 668, 601, 824,
  12. 97, 137, 711, 764, 880, 645, 78, 170, 767, 242, 400, 847, 279,
  13. 693], dtype=int32)
  14. >>> points[hull.vertices]
  15. array([[-1.41519105, 1.40697935],
  16. [-1.82809455, 0.76082381],
  17. [-1.96558532, 0.33289305],
  18. [-1.98560143, -0.01191735],
  19. [-1.94680891, -0.44858819],
  20. [-1.92640504, -0.50545949],
  21. [-1.82801696, -0.72696817],
  22. [-1.62541354, -1.10389154],
  23. [-0.81276609, -1.81667984],
  24. [-0.5230336 , -1.91088401],
  25. [-0.16741267, -1.9816704 ],
  26. [ 0.60681346, -1.85569449],
  27. [ 0.97014715, -1.73968032],
  28. [ 1.34722226, -1.46198943],
  29. [ 1.80922099, -0.83972869],
  30. [ 1.86933042, -0.62334995],
  31. [ 1.94523674, -0.29988429],
  32. [ 1.88851458, 0.51313387],
  33. [ 1.78142205, 0.85649355],
  34. [ 1.71284948, 0.99370767],
  35. [ 1.2998368 , 1.51082726],
  36. [ 0.88852111, 1.74722282],
  37. [ 0.50725689, 1.8652817 ],
  38. [ 0.17148352, 1.95374732],
  39. [ 0.04487085, 1.98223443],
  40. [-0.74854329, 1.8364107 ],
  41. [-1.05242057, 1.69470583]])

适应形状围绕数据点

英文:

What you are looking for is a convex hull. You can use ConvexHull from scipy module.

  1. import matplotlib.pyplot as plt
  2. from scipy.spatial import ConvexHull
  3. hull = ConvexHull(points)
  4. plt.scatter(points[:, 0], points[:, 1])
  5. for simplex in hull.simplices:
  6. plt.plot(points[simplex, 0], points[simplex, 1], 'k-')
  1. >>> points
  2. # x y
  3. array([[ 1.85804999, -0.05672655],
  4. [-1.26377587, -0.71670609],
  5. [ 1.20926884, -0.90475264],
  6. ...,
  7. [-0.90567066, -0.84883138],
  8. [-0.43970639, 1.84684422],
  9. [ 0.19636511, 1.93878809]])
  10. >>> hull.vertices
  11. array([413, 129, 655, 254, 841, 793, 321, 526, 570, 695, 668, 601, 824,
  12. 97, 137, 711, 764, 880, 645, 78, 170, 767, 242, 400, 847, 279,
  13. 693], dtype=int32)
  14. >>> points[hull.vertices]
  15. array([[-1.41519105, 1.40697935],
  16. [-1.82809455, 0.76082381],
  17. [-1.96558532, 0.33289305],
  18. [-1.98560143, -0.01191735],
  19. [-1.94680891, -0.44858819],
  20. [-1.92640504, -0.50545949],
  21. [-1.82801696, -0.72696817],
  22. [-1.62541354, -1.10389154],
  23. [-0.81276609, -1.81667984],
  24. [-0.5230336 , -1.91088401],
  25. [-0.16741267, -1.9816704 ],
  26. [ 0.60681346, -1.85569449],
  27. [ 0.97014715, -1.73968032],
  28. [ 1.34722226, -1.46198943],
  29. [ 1.80922099, -0.83972869],
  30. [ 1.86933042, -0.62334995],
  31. [ 1.94523674, -0.29988429],
  32. [ 1.88851458, 0.51313387],
  33. [ 1.78142205, 0.85649355],
  34. [ 1.71284948, 0.99370767],
  35. [ 1.2998368 , 1.51082726],
  36. [ 0.88852111, 1.74722282],
  37. [ 0.50725689, 1.8652817 ],
  38. [ 0.17148352, 1.95374732],
  39. [ 0.04487085, 1.98223443],
  40. [-0.74854329, 1.8364107 ],
  41. [-1.05242057, 1.69470583]])

适应形状围绕数据点

huangapple
  • 本文由 发表于 2023年4月20日 01:28:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057301.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定