title: 3D matrix perspective transform
body: <p>I am using shape from shading to generate a Digital Terrain Model (DTM) of an image taken using a camera mounted on a mobile platform. The algorithm written in Python seems to work reasonably well however the output is at an incline and a bit spherical so I'm suspecting that I need to remove perspective distortion and barrelling from the DTM. </p>
  
  <p>The data is available <a href="https://dl.dropboxusercontent.com/u/21471588/SFS_DTM.txt" rel="nofollow noreferrer">here</a> in case anyone is interested in having a go at this.</p>
  
  <p>The camera is mounted at an inclination of 41 degrees and has the following camera and distortion matrices:</p>
  
  <pre><code>    cam_matrix = numpy.matrix([[246.00559,0.00000,169.87374],[0.00000,247.37317,132.21396],[0.00000,0.00000,1.00000]])
      distortion_matrix = numpy.matrix([0.04674, -0.11775, -0.00464, -0.00346, 0.00000])
  </code></pre>
  
  <p>How can I apply perspective transform and remove the barreling distortion from this matrix to obtain a flattened DTM?</p>
  
  <p>I have attempted this using OpenCV but it doesn't work as OpenCv is expecting an image and the transforms simply move pixels around rather than manipulate their value. I have also researched Numpy and Scipy but haven't arrived to a conclusion or a solution yet. I am somewhat familiar with the theory behind these transforms but have mostly worked on 2D versions.</p>
  
  <p>Any ideas?</p>