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 wi开发者_C百科th 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.
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
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.
精彩评论