title: Scipy.optimize.minpack.leastsq with 2 dimensional args
body: <p>I am trying to calculate the 3x3 calibration matrix P of a camera based on 2D to 3D point correspondences as described in this paper <a href="http://cronos.rutgers.edu/~meer/TEACHTOO/PAPERS/zhang.pdf" rel="nofollow">http://cronos.rutgers.edu/~meer/TEACHTOO/PAPERS/zhang.pdf</a> (section 2.3) using python 2.7. I have been able to find an initial estimate of P, but now need to refine it using the Levenberg-Marquardt Algorithm (2.3.4). It appears to me that this can be done with scipy.optimize.minpack.leastsq. However, my attemps at implementing this function have failed. Here is a simplified version of what I have (M is a numpy array of homogenized 3d points in the format (x,y,z,1) with a shape of (18,4) and m is a numpy array of homogenized 2d points in the format (u,v,1) with a shape of (18,3)):</p>
  
  <pre><code>import numpy as N
  from scipy.optimize.minpack import leastsq
  
  def e(P,M,m):
      a = P.dot(M.T) 
      print a.shape
      b = m.T-a
      b1 = b[0]
      b2 = b[1]
      b3 = b[2]
      dist = sqrt((b1**2)+(b2**2)+(b3**2))
      return dist
  
  
  
  P = N.array( [ [4.66135353e+01,1.24341518e+02,-9.07923056e+00,9.59292826e+02],
                 [-3.60062368e+01,3.56319152e+01,1.14245572e+02,2.32061401e-02],
                 [-4.04188199e-02,4.00793699e-02,-9.48804649e-03,1.00000e+00] ] )
  
  m = []
  M = []
  #define m list and M list
  for i in range(0,len(uv)):        
      uv[i].append(1) #uv is unhomogenized uv coordinate list (source left out to      simplify)
      xyz[i].append(1) #xyz is unhomogenized xyz coordinate list (source left out to simplify)
      m.append(N.array( [ [uv[i][0]],[uv[i][1]],[uv[i][2]] ] ))    
  m = N.array( uv )
  M = N.array( xyz )
  #the shape of m is (18,3) and the shape of M is (18,4)
  
  P_new, success = leastsq(e, P, args=(M,m))
  </code></pre>
  
  <p>I think the problem is with the M and m variables, the arrays of vectors. I looked at an example for the scipy.optimize.lstsq function and I could get that to work but it had args with only one dimension. </p>
  
  <p>Does anyone know what I am doing wrong here? I am fairly new to programming so take it easy on me if this is idiotic haha. Thanks so much to all who read this and let me know if I can provide anymore info</p>