Least Squares Methode im dreidimensionalen Raum

mit matplotlib, NumPy, pandas, SciPy, SymPy und weiteren mathematischen Programmbibliotheken.
Antworten
leaackermann007
User
Beiträge: 1
Registriert: Mittwoch 26. November 2025, 14:20

Hallo. Ich bin neu hier im Forum und habe eine Frage bezüglich Least Squares Method im dreidimensionalen Raum.

Ich habe 5 Punkte eines Balls mithilfe von reflektierenden Markern und einem Motion Capture Tracking System aufgenommen und möchte nun das Zentrum des Balls und dessen Radius anhand der 5 Oberflächenpunkte des Balls berechnen. Ich habe ein Youtube Video gefunden wie ich mit dem Least Squares Verfahren einen Kreis an gegebenen Punkten auf dem Umfang fitten kann.
Nun möchte ich das Ganze einfach aufs dreidimensionale erweitern um den Mittelpunkt eines Balls zu berechnen.

Ich habe folgenden Code geschrieben:

# import packages
import numpy as np

# functions
def least_squares_sphere_fit(points):
"""
Calculates the center and radius of a sphere with the least squares method.
"""
N = len(points)
if N < 4:
raise ValueError("You need at least 4 points.")

# Split up x,y, and z coordinates
x = points[:, 0]
y = points[:, 1]
z = points[:, 2]

# Designmatrix A
A = np.column_stack([-2*x, -2*y, -2*z, np.ones(N)])

# Vector b
b = -(x**2 + y**2 + z**2)

# Solution to the linear system A * [x_0, y_0, z_0, x_0^2 + y_0^2 + z_0^2 - r^2] = b
try:
X = np.linalg.lstsq(A, b, rcond=None)[0] # [x_0, y_0, z_0, x_0^2 + y_0^2 + z_0^2 - r^2]
x_0, y_0, z_0, c = X

# radius R
R_squared = x_0**2 + y_0**2 + z_0**2 - c # r^2 = x_c^2 + y_c^2 - c
R = np.sqrt(R_squared)
return (x_0, y_0, z_0), R
except np.linalg.LinAlgError:
print("Error: Couldn't solve the system")
return None, None

# --- main program ---
if __name__ == '__main__':
# example points
points = np.array([
[1.005, 0.01, -0.01], # P1
[0.700, 0.51, 0.50], # P2
[0.710, -0.50, -0.50], # P3
[0.400, 0.90, 0.02], # P4
[0.430, -0.02, -0.90] # P5
])

# call function to get center and radius of sphere
optimal_center_3d, radius_3d = least_squares_sphere_fit(points)

----------------------------------------------------------------------------------------------------------------------

Kann mir jemand sagen ob das richtig ist oder ob man das Kreisverfahren nicht einfach mit einer z Koordinate erweitern kann um das Ganze im Dreidimensionalen zu berechnen. Bzw kennt jemand vielleicht einen besseren Weg den Mittelpunkt eines Balls anhand von 5 Koordinaten an der Oberfläche zu bestimmen?

Besten Dank schonmal im Voraus, Lea :)
Antworten