Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

graphics - Transformation of 3D objects related to vanishing points and horizon line

I'm trying to computing the exact prospective transformation of a 3D object starting from a vanishing points and horizon line of a picture.

Start Image

What I want is, fixed the vanishing points and horizontal line of a picture, I want rotate and skew an 3D object according with vanishing points and horizontal lines that I set starting from the picture

Below the final result that I expected.

End Image

How can I obtain this result?

What kind of transformation can I use?

In this video is possibile to see the result that I want.

https://www.youtube.com/watch?v=EsSerR-AjEk

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is nowhere near my cup of tea so handle with extreme prejudice and also far form solution just some start point hints...

First of all we need to define some constraints/assumptions in order to make this to work.

  • user selects 4 lines representing 2 perpendicular planes and these 2 QUADs have the same height and altitude. Also the object height axis is almost the same as camera y axis (not rotated images).
  • perspective is centered around image center so central pixel represents view direction
  • pixels are squares

So what you want to obtain is a 4x4 homogenous matrix that converts from some global 3D coordinates into 2D image coordinates + the perspective division.

|x'|    | Xx Yx Zx Ox |  |x|
|y'| =  | Xy Yy Zy Oy | .|y|
|z'|    | Xz Yz Zz Oz |  |z|
|w'|    | a  b  c  1  |  |1| 

where (x,y,z) represents some 3D position and (x'/z',y'/z') represents 2D position on screen (image). To make this simple let assume that image center is (0,0).

To construct your perspective matrix you need to know the FOV angles of camera and its focal length znear. Without it you can estimate it from known stuff on the image...

Another option is to fit this matrix until the points match. But as it is 15 unknowns it would be very slow (even if many of the parameters are dependent and can be computed from the others).

[complete reedit] Simple C++ approach example

  1. Two QUADs

    I would start with computing quad per each of the planes:

    quads&reper

    To ease up the code later on the points should have a specific order. I programaticaly sort them so they are CCW and first point of each QUAD is in top right corner. First QUAD is on the right (representing Z axis of building or YZ plane) and second is on the left (representing X axis of building or XY plane).

    I also compute the mid point (avg point) for each quad and sort the points by angle between screen x axis this point and sorted point. After this its needed to make a correction of position (shift all points by 1 in case screen x axis is colliding with quad horizontal axis) so the first point of quad is the to right corner.

    Now we need to turn our lines into QUAD. For that we need the building y axis direction ... At first I was casting a 2D normal from each of the 4 lines and average them together. Beware they should all be in the same direction ... so if added normal has negative dot product with the average negate it before adding. This averaged normal is the UP vector projection onto XY plane.

    normals and intersection

    But later on I changed this I computed 2 intersection points between corresponding left and right QUAD horizontal lines (obtaining the UP vector/direction of the building edge between QUADs). This prove more accurate and also easier to compute.

    Now to convert your lines into QUADS simply find intersections between the lines and normal casted from endpoints of one of the lines per plane. After this the intersection will be aligned exactly as the QUAD corners so we can use that from now...

  2. Perspective

    As our building is most likely a box with right angles between its plane so our 2 QUADs should be also perpendicular to each other in 3D. We can use this ... as if we connect their vanishing points with their mid points the lines in 3D should be also with 90deg right angle. So we can directly obtain the FOVx angle from this...

    vanishing points XZ vanishing points XY

    So the ratio between FOVx and 90.0deg is the same as ratio between screen x resolution and the 2 vanishing points distance in pixels... So from that:

    FOVx = 90.0*deg * image_x_resolution / intersections_x_distance
    

    As we also know the screen resolution than the znear is also directly computable. for example I use coordinates <-1,+1> in OpenGL for screen so:

    znear = 1.0/tan(0.5*FOVx)
    

    Of coarse this will affect the overall scale of the result so do not expect meters units...

    The zfar should be chosen wisely so the building is actually in the viewing frustrum. For example:

    zfar = 1000.0*znear
    

    It only affects the view depth relative to znear ... but it does not affect perspective itself.

  3. building 3D coordinates

    The QUADs vertical line sizes gives us the scale depended on depth. This can be used to compute Z coordinate for each point we have ... But for that we need to know original height of our QUADs. Luckily for us the unprojected 2D screen coordinates of the QUADs into 3D should form right angles. So if we use 3 points (the QUAD midpoints and midpoint of the edge between them) and do a dot product of the unprojected lines direction the result should be zero. So we got 4 equations and 4 unknowns which is algebraically solvable...

    The depth relation is as follows:

    scale(z) = znear/z
    

    so if we compute the height of QUAD at place where our point in question is we can get the scale relative to original QUAD height l... As we have 3 points then:

    z0 = znear*l0/l
    z1 = znear*l1/l
    z2 = znear*l2/l
    dot(pnt1-pnt0,pnt2-pnt0)=0
    

    where unprojected points: pnt0(x0,y0,z0) is the mid point of the edge between QUADs and pnt1(x1,y1,z1) and pnt2(x2,y2,z2) are the midpoints of the QUADs. The l0,l1,l2 are the corresponding height sizes. So the only unknonws here are z0,z1,z2,l ...

    btw these unprojected points give us 2 basis vectors and position of the buildings coordinate system directly. So we can compose its matrix too... The third can be also unprojected or use cross product instead ...

    Here a debug rendered cube with the reversed perspective overlay:

    debug cube

As you can see the fit is not perfect that is due some bug in my 3D view related to viewing window aspect ratio. If the window is square (not the image just the GL window) fit is perfect. If I add aspect ratio to the 3D view (scale) the fit is perfect but the basis vectors of the coordinate system are not visually the same size... Need to think about it some more to repair... its most likely some silly simple thing not related to the reversing perspective at all... Here square view screen shots:

perfect fits

Here my actual C++/GL code for this... but beware I am using some stuff from my rendering engine (like vector math etc...)

//---------------------------------------------------------------------------
#ifndef _ReversePespective_h
#define _ReversePespective_h
//---------------------------------------------------------------------------
class ReversePerspective
    {
public:
    double FOVx;        // [rad] perspective parameters
    double znear,zfar;
    double per[16];     // perspective projection matrix used
    reper  rep;         // selected coordinate system
    double asp,_asp;    // screen ys/xs
    double zoom,_zoom;  // view zoom
    double panx,pany;   // view position
    double ms[3],mw[3]; // mouse position [screen] , [world]

    enum _p2D_enum
        {
        _p2D_quad0= 0,  // 2x guad points (same altitude and perpendicular planes)
        _p2D_quad1= 8,  //           10   8 | A | 2  0
        _p2D_qmid0=16,  // V1          18   |   |  16              V0
        _p2D_qmid1=18,  //           12  14 | B | 4  6
        _p2D_A    =20,
        _p2D_B    =22,
        _p2D_V0   =24,  // quad0 vanishing point (right)
        _p2D_V1   =26,  // quad1 vanishing point (left)
        _p2Ds     =36,
        };
    double p2D[_p2Ds];

    enum _p3D_enum
        {
        _p3D_O    = 0,  //           Y
        _p3D_X    = 3,  //     X     O     Z
        _p3D_Y    = 6,  //
        _p3D_Z    = 9,
        _p3Ds     =12,
        };
    double p3D[_p3Ds];

    int sel;            // mouse selected p2D point
    bool _redraw;       // App need redraw?

    ReversePerspective() { asp=1.0; _asp=1.0; reset(); }
    ReversePerspective(ReversePerspective& a) { *this=a; }
    ~ReversePerspective() {}
    ReversePerspective* operator = (const ReversePerspective *a) { *this=*a; return this; }
    //ReversePerspective* operator = (const ReversePerspective &a) { ...copy... return this; }

    void reset()        // init points
        {
        sel=-1; _redraw=true;
        zoom=1.0; _zoom=1.0;
        panx=0.0; pany=0.0;
        matrix_one(per);
        FOVx=60.0*deg;
        znear=0.1; zfar=1.0;
        vector_ld(ms,0.0,0.0,0.0);
        vector_ld(mw,0.0,0.0,0.0);
        p2D[ 0]=-0.5; p2D[ 1]=-0.5;
        p2D[ 2]=-0.5; p2D[ 3]=+0.5;
        p2D[ 4]=-0.9; p2D[ 5]=+0.5;
        p2D[ 6]=-0.9; p2D[ 7]=-0.5;
        p2D[ 8]=+0.5; p2D[ 9]=-0.5;
        p2D[10]=+0.5; p2D[11]=+0.5;
        p2D[12]=+0.9; p2D[13]=+0.5;
        p2D[14]=+0.9; p2D[15]=-0.5;
        compute();
        }
    void view2D()       // set 2D mode view
        {
        glDisable(GL_CULL_FACE);
        glDisable(GL_DEPTH_TEST);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glScaled(zoom*asp,zoom,1.0);
        glTranslated(panx,pany,0.0);
        }
    void view3D()       // set 3D mode view
        {
        glClear(GL_DEPTH_BUFFER_BIT);
        glDisable(GL_CULL_FACE);
        glEnable(GL_DEPTH_TEST);
        glMatrixMode(GL_PROJECTION);
        glLoadMatrixd(per);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glScaled(zoom,zoom,1.0);
        glTranslated(panx,pany,0.0);
        }
    void draw2D()       // render 2D mode
        {
        int i; double c[3]; _redraw=false;
        // up axis
        // quads vanishing points/lines
        glColor3f(0.3,0.7,0.3); glBegin(GL_LINES);
        glVertex2dv(p2D+_p2D_V0); glVertex2dv(p2D+ 0);
        glVertex2dv(p2D+_p2D_V0); glVertex2dv(p2D+ 6);
        glVertex2dv(p2D+_p2D_V1); glVertex2dv(p2D+10);
        glVertex2dv(p2D+_p2D_V1); glVertex2dv(p2D+12);
        glColor3f(1.0,1.0,0.0);
        glVertex2dv(p2D+_p2D_V0); glVertex2dv(p2D+_p2D_V1);
        glColor3f(0.0,1.0,0.0);
        glVertex2dv(p2D+_p2D_A); glVertex2dv(p2D+_p2D_B);
        glEnd();
        // quads circumference
        glColor3f(1.0,1.0,1.0);
        glBegin(GL_LINE_LOOP); for (i=0;i< 8;i+=2) glVertex2dv(p2D+i); glEnd();
        glBegin(GL_LINE_LOOP); for (   ;i<16;i+=2) glVertex2dv(p2D+i); glEnd();
        // quads fill
        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...