我试图通过python进行bundle调整。所以我测试了非线性最小二乘模块。 然后我编写了如下代码。我想要正确Pmat代表三个相机的相机投影矩阵。但是我有一个错误,“ValueError:对象太深了所需的数组”。
任何可以提供解决此问题的线索的人?
此致 Jinho Yoo。
from math import* from numpy import *
import pylab as p from scipy.optimize
import leastsq
Projected_x = \ mat([[ -69.69 , 255.3825, 1. ],
[ -69.69 , 224.6175, 1. ],
[-110.71 , 224.6175, 1. ],
[-110.71 , 255.3825, 1. ],
[ 709.69 , 224.6175, 1. ],
[ 709.69 , 255.3825, 1. ],
[ 750.71 , 255.3825, 1. ],
[ 750.71 , 224.6175, 1. ]])
Projected_x = Projected_x.transpose()
Pmat = \ mat( [[ 5.79746167e+02, 0.00000000e+00, 3.20000000e+02, 0.00000000e+00],
[ 0.00000000e+00, 4.34809625e+02, 2.40000000e+02, 0.00000000e+00],
[ 0.00000000e+00, 0.00000000e+00, 1.00000000e+00, 0.00000000e+00] ] )
reconst_X = \ mat([[-0.95238194, -0.58146697, 0.61506506, 0.00539229],
[-0.99566105, -0.76178453, 0.72451719, 0.00502341],
[-1.15401215, -0.81736486, 0.79417098, 0.00546999],
[-1.11073304, -0.6370473 , 0.68471885, 0.00583888],
[ 2.71283058, 2.34190758, -1.80448545, -0.00612243],
[ 2.7561097 , 2.52222514, -1.91393758, -0.00575354],
[ 2.9144608 , 2.57780547, -1.98359137, -0.00620013],
[ 2.87118168, 2.39748791, -1.87413925, -0.00656901]])
def residuals(p, y, x):
err = y - p*x.transpose()
err = err * err.transpose()
return err
p0 = Pmat
plsq = leastsq(residuals, p0, args=(Projected_x, reconst_X ) )
print plsq[0]
答案 0 :(得分:3)
我的第一个猜测:minimalsq不喜欢矩阵,
使用数组和np.dot,或者在返回之前转换np.asarray(err),并且可能在剩余函数内将p转换为矩阵。
混合矩阵和数组可能很难跟踪。
答案 1 :(得分:1)
一些小事:
我已将代码更改为使用np.array来演示user333700的含义。此外,我将投影矩阵转换为12维向量,因为大多数优化器都希望您的变量以向量形式进行优化。
您将运行下面编辑过的代码的错误是TypeError:输入参数不正确。我相信这是因为你试图执行线性最小二乘法来找到12个参数,但你只有8个约束。
import numpy as np
import pylab as p
from scipy.optimize import leastsq
Projected_x = np.array([[ -69.69 , 255.3825, 1. ],
[ -69.69 , 224.6175, 1. ],
[-110.71 , 224.6175, 1. ],
[-110.71 , 255.3825, 1. ],
[ 709.69 , 224.6175, 1. ],
[ 709.69 , 255.3825, 1. ],
[ 750.71 , 255.3825, 1. ],
[ 750.71 , 224.6175, 1. ]])
Projected_x = Projected_x.transpose()
Pmat = np.array( [ 5.79746167e+02, 0.00000000e+00, 3.20000000e+02, 0.00000000e+00,
0.00000000e+00, 4.34809625e+02, 2.40000000e+02, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 1.00000000e+00, 0.00000000e+00] )
reconst_X = np.array([[-0.95238194, -0.58146697, 0.61506506, 0.00539229],
[-0.99566105, -0.76178453, 0.72451719, 0.00502341],
[-1.15401215, -0.81736486, 0.79417098, 0.00546999],
[-1.11073304, -0.6370473 , 0.68471885, 0.00583888],
[ 2.71283058, 2.34190758, -1.80448545, -0.00612243],
[ 2.7561097 , 2.52222514, -1.91393758, -0.00575354],
[ 2.9144608 , 2.57780547, -1.98359137, -0.00620013],
[ 2.87118168, 2.39748791, -1.87413925, -0.00656901]])
def residuals(p, y, x):
err = y - np.dot(p.reshape(3,4),x.T)
print p
return np.sum(err**2, axis=0)
p0 = Pmat
plsq = leastsq(residuals, p0, args=(Projected_x, reconst_X ) )
print plsq[0]