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.
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:
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.