Search code examples
c#.net3dmatrix-multiplicationcoordinate-systems

How to calculate camera position/orientation from the picture of a known object?


I have a known object (square) in the 3D space and I know the exact position of its corners*. I take a photo of the object, and I am already able to precisely identify which pixel on the photo corresponds to which corner of the square. (I also know the camera's sensor resolution and the lens' focal length**).

How do I calculate the position and orientation of the camera? I want to implement the solution in C#. This sounds like rather basic matrix operation used in 3D game engines all the time, just executing the opposite direction. I hope it truly is. :)

*All position and length info is expressed in [meter] in a local coordinate system. No lat/lons are used.

**Focal length is not expressed in 35mm equivalent, but by the width and height of the viewport that is 1 meter away from the focal point.


Solution

  • The term for what you're trying to do is 'Homography'.

    The OpenCV library provides a variety of functions to achieve this - you can read about some of the maths here:

    http://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html?highlight=findhomography

    In particular, the findHomography function will utilise a list of point correspondences between a sample image and a camera image to calculate a matrix for the camera position.