我正在尝试使用 scipy 复制相机校准优化。
该算法在没有雅可比的情况下工作正常(优化收敛于最优结果)。 然而,当添加雅可比函数时,最小二乘函数几乎没有迭代并在不收敛的情况下停止。
import numpy as np
from scipy.optimize import least_squares
# Camera projection function
def project(params,points_2D, points_3D):
"""
This function takes in 3D points and camera parameters,
and returns the projected 2D points.
"""
fx, fy, cx, cy = params
X, Y, Z = points_3D
x = fx * X / Z + cx
y = -fy * Y / Z + cy
return np.array([x, y])
# Jacobian function
def jacobian(params,points_2D, points_3D):
fx, fy, cx, cy = params
X, Y, Z = points_3D
nb_pts = points_3D.shape[1]
J = np.zeros((2*nb_pts,4))
for i in range(nb_pts):
J[2*i:2*i+2, :] = np.array([
[X[i]/Z[i], 0, 1, 0],
[0, Y[i]/Z[i], 0, 1]
])
return J
# Residual function for least_squares
def residuals(params, points_2D, points_3D):
"""This function computes the residuals, i.e., the difference between the observed 2D points and the projected 2D points."""
projected_points = project(params,points_2D,points_3D)
return ((projected_points - points_2D.T)**2).flatten()
# Initial guess for the camera parameters
params0 = np.array([1050, 1000, 320, 240]) # Optimal parameters are [1000, 1000, 320, 240]
# Example 3D points
points_3D = np.array([
[0, 0, 3],
[20/100, 0, 2],
[0, -1/100, 1]
])
# Example 2D points
points_2D = np.array([
[320, 240],
[420, 240],
[320, 250]
])
pts = residuals(params0,points_2D,points_3D.T)
print(pts)
# Perform optimization
result = least_squares(residuals, params0, jac=jacobian, args=(points_2D, points_3D.T), verbose=1)
# Print the optimized camera parameters
print(result.x)
pts2 = residuals(result.x,points_2D,points_3D.T)
print(pts2)
知道我做错了什么吗?