How can I find the 3D coordinates of a projected rectangle?

回眸只為那壹抹淺笑 提交于 2019-12-04 15:56:40

You need to calculate the inverse of your projection matrix. (your matrix cannot be singular)

I'm going to give a fairly brief answer here, but I think you'll get my general drift. I'm assuming you have a 3x4 projection matrix (P), so you should be able to get the camera centre by finding the right null vector of P: call it C.

Once you have C, you'll be able to compute rays with the same direction as vectors CK,CL,CM and CN (i.e. the cross product of C and K,L,M or N, e.g. CxK)

Now all you have to do is compute 3 points (u1,u2,u3) which satisfies the following 6 constraints (arbitrarily assuming KL and KN are adjacent and ||KL|| >= ||KN|| if d1 >= d2):

  1. u1 lies on CK, i.e. u1.CK = 0
  2. u2 lies on CL
  3. u3 lies on CN
  4. ||u1-u2|| = d1
  5. ||u1-u3|| = d2
  6. (u1xu2).(u1xu3) = 0 (orthogonality)

where, A.B = dot product of vectors A and B ||A|| = euclidean norm of A AxB = cross product of A and B

Skizz

I think this problem will generate a set of possible solutions, at least in 2D it does. For the 2D case:

           |   
-----------+-----------
          /|\
         / | \
        /  |  \
       /---+---\VP
      /    |    \
     /     |     \
    /      |      \
   /       |       \
  /        |   --   \
 /         |    |    \
/          |    |     \

In the above diagram, the vertical segment and the horizontal segment would project to the same line on the view plane (VP). If you drew this out to scale you'd see that there are two rays from the eye passing through each end point of the unprojected line. This line can be in many positions and rotations - imagine dropping a stick into a cone, it can get stuck in any number of positions.

So, in 2D space there are an infinite number of solutions within a well defined set.

Does this apply to 3D?

The algorithm would be along the lines of:

  1. Invert the projection matrix
  2. Calculate the four rays that pass through the vertices of the rectangle, effectively creating a skewed pyramid
  3. Try and fit your rectangle into the pyramid. This is the tricky bit and I'm trying to mentally visualise rectangles in pyramids to see if they can fit in more than one way.

EDIT: If you knew the distance to the object it would become trivial.

EDIT V2:

OK, let Rn be the four rays in world space, i.e. transformed via the inverse matrix, expressed in terms of m.Rn, where |Rn| is one. The four points of the rectange are therefore:

P1 = aR1
P2 = bR2
P3 = cR3
P4 = dR4

where P1..P4 are the points around the circumference of the rectangle. From this, using a bit of vector maths, we can derive four equations:

|aR1 - bR2| = d1
|cR3 - dR4| = d1
|aR1 - cR3| = d2
|bR2 - dR4| = d2

where d1 and d2 are the lengths of the sides of the rectangle and a, b, c and d are the unknowns.

Now, there may be no solution to the above in which case you'd need to swap d1 with d2. You can expand each line to:

(a.R1x - b.R2x)2 + (a.R1y - b.R2y)2 + (a.R1z - b.R2z)2 = d12

where R1? and R2? are the x/y/z components of rays 1 and 2. Note that you're solving for a and b in the above, not x,y,z.

m_oLogin is right. If I understand your goal, the image the camera snaps is the plane P, right? If so, you're measuring K,L,M,N off the 2D image. You need the inverse of the projection matrix to reconstruct A,B,C, and D.

Now I've never done this before, but it ocurrs to me that you might run into the same problem GPS does with only 3 satellite fixes - there are two possible solutions, one 'behind' P and one 'in front' of it, right?

The projection matrix encapsulates both the perspective and scale, so the inverse will give you the solution you are after. I think you are assuming that it only encapsulates the perspective, and you need something else to choose the correct scale.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!