phpscriptcoder
phpscriptcoder

Reputation: 737

Finding 3D position of a camera used to take a 2D picture

Given a 3D scene and a 2D image of part of that scene, is it possible to find the position of the camera used to make the image?

Upvotes: 5

Views: 7225

Answers (4)

sumodds
sumodds

Reputation: 427

Yes, but it depends. If the camera does not distort the image too badly, the simple trigonometry by ninjagecko might work but again depends on your application. If you want to learn how to do this in a more cleaner way and more mathematical way, check this out http://www.ces.clemson.edu/~stb/projective/.

Upvotes: 1

Michael Lorton
Michael Lorton

Reputation: 44406

I'm going to assume from your question that you have four points in the 2-D space whose locations you know in the 3-D space. (If your real problem is identifying those point, I haven't the foggiest idea where to begin.)

Your question is therefore, where can the camera be to have produced this mapping from model to image?

The camera is performing an affine transformation of the form M x + T = y, where x is a vector in 3-space representing the point in the model and y is the 2-space vector representing the point in the image. Given four values for x and y, it's a straightforward matrix-arithmetic problem to solve for M and T (probably best to get MATLAB to help you here.)

Now, how do you translate M and T to position, location, and focal length of the camera? There it gets a little hairy but this page should help you (just remember, you are doing everything backwards).

Upvotes: 6

ninjagecko
ninjagecko

Reputation: 91122

You need more information, but not necessary much more information.

The simplest thing to do would be to find an object or distance in the scene you know the length of (e.g. draw a virtual line in the image, say what the distance is in whatever units you want). Then you also need to know the focal length (inversely proportional to field of view angle).

Given the angle of view, a line segment (or perhaps two) with known length, you can very closely approximate the exact location of the camera with a bit of trigonometry. e.g. for a rough distance estimate:

   / |
  /  |
 /_d_I-segment
 \   |
  \  |
   \ |

distance "d", field of view angle "FoV"

picture.physicalsize/d == tan(FoV/2)
picture.physicalsize/segment.physicalsize == picture.pixelsize/segment.pixelsize

thus
d = segment.physicalsize*(picture.pixelsize/segment.pixelsize)/tan(FoV/2)

If you want even more precision, you may need more than one line segment and more careful math.

Upvotes: 3

Vaughn Cato
Vaughn Cato

Reputation: 64308

If you take a random sampling of camera parameters, rendering the scene with each set of parameters, you can take the best matches as a starting point. You can then perform a local optimization of the camera parameters to find those parameters which reproduce the 2D image the closest. It isn't fast, but theoretically you could come up with very good guesses given enough time.

Upvotes: 5

Related Questions