SciPy's non-linear least square
I tried to do bundle adjustment by python. So I'm test non-linear least square module. Then I wrote code like below. I want to get right Pmat represents camera projection matrix for three cameras. But I have an error,"ValueError: object too deep for desired array".
Anyone who can give clue to solve this issue?
Regards, 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.38开发者_开发百科25, 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]
my first guess: leastsq doesn't like matrices,
use arrays and np.dot, or convert np.asarray(err) before the return, And maybe convert p to matrix inside your residual function.
Mixing matrices and arrays can be a pain to keep track of.
A couple of small things:
- use np.array if you can
- Do not import *
I have changed the code to use np.array to demonstrate what user333700 means. Also I convert the projection matrix into a 12 dimensional vector since most optimizer expects your variable to optimize in vector form.
The error that you will get running the edited code below is TypeError: Improper input parameters. I believe that this is because you are trying to perform linear least square to find 12 parameters but you only have 8 constraints.
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]
精彩评论