0

Let's say that you have a camera and take a snapshot of a rectangular paper lying on a table. We get that picture, use as a flat background and need to set up an OpenGL scene in the way that the paper it's rendered with a quad with coordinates, let's say (0,0,0) (1,0,0) (1,2,0) (0,2,0) with a specific camera and view settings.

In other words, we need to reproduce the same algorithms behind photoshop's "vanishing point" function.

The mathematical solution to the problem needs for sure some more constants to be defined to give one solution (distance of the observer, etc). There should be no problem to fix those data at build time.

Mathematical solution would be appreciated but better a working C/C++ code with reference to any external math framework for geometrical transformation stuff and so on, to enter coherent data to OpenGL camera model parameters.

Both NyArToolkit and OpenCV have interesting and complex functions but the input refers to "training" not needed. The Photoshop's "Vanishing point" function works in realtime, giving a result with only 4 2d points in input.

The function we need is something like

BOOL calculate_3D_scene_From_2D_Rectangle(
       point2d* pointsList, // in: array of the four corners of the quad rectangle
                            // in 2d coordinates, in clockwise order
       rect2d   sceneSize,  // in: view area in 2d coordinates, points are contained
                            // within this area
       float    eyeDistance // in: distance from observer eye to the object
       point3d* vertexList  // out: four coordinates of the point in 3d space
       float*   mvMatrix    // out: 4x4 matrix to use as model view matrix in openGl
                            // return: true if found a correct result
);

Sample obvious usage

point2d pointsList[] = { { -5, -5 }, { 10, -5 }, { 10, 10 }, { -5, 10 } };
rect2d sceneSize = { -20,-20,20,20 };
float eyeDistance = 20;

point3d vertexList[4];
float mvMatrix[16];

calculate_3D_scene_From_2D_Rectangle(
    pointsList,sceneSize,eyeDistance,vertexList,mvMatrix);

[...]
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLoadMatrixf(mvMatrix);
[...]
draw the polygon with vertexList[0..3];
[...]

Any really applicable implementation for a correct calculate_3D_scene_From_2D_Rectangle() would be appreciated.

Lucio
  • 21
  • 2
  • Kind of hard to understand what it is that you really want here. Is it some kind of augmented reality thing? Is [camera tracking/match moving](http://www.thepixelart.com/breakdown-best-matchmoving-and-tracking-applications/) what you want? – Zecc Jun 06 '11 at 10:44
  • If that's the case, maybe [this link](http://www.morethantechnical.com/2009/06/28/augmented-reality-with-nyartoolkit-opencv-opengl/) will get you started. – Zecc Jun 06 '11 at 10:54
  • This is very close to what we need, except we dont even need to do it in realtime. Indeed this would be a quite interesting evolution. Going around the code of the library could take us to the couple of functions needed. – Lucio Jun 06 '11 at 13:04
  • How is this different than applying a perspective projection? – luke Jun 08 '11 at 17:06

2 Answers2

1

This sounds like camera calibration, a common task in computer vision. OpenCV offers functions for this:

http://opencv.willowgarage.com/documentation/c/calib3d_camera_calibration_and_3d_reconstruction.html#calibratecamera2

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • Great toolbox indeed, we're trying now to find the correct function with four 2D points input and 3D camera output. There's a lot of methods in the camera calibration area. – Lucio Jun 06 '11 at 13:13
0

take a look into findhomography. that might already sufficient for your task. If you want to do a full camera calibration you need more than 4 points (minimum is 6), but for planar mappings, the homography approach works fine.

agga
  • 1
  • Already did tests with findHomography. As far as i understand, the functions takes 2D inputs and generates a 3x3 matrix. To fully apply perspective, a 4x4 transformation matrix is needed. In fact our scene points could be considered 3d points with (x,y,z=0). – Lucio Jun 09 '11 at 08:39