2D多边形到3D对象和场景



假设您有一台相机,可以拍摄一张放在桌子上的矩形纸的快照。我们得到了这张照片,用作平面背景,并需要设置OpenGL场景,就像用带有坐标的四边形渲染纸张一样,比如说(0,0,0)(1,0,0)(1,2,0),(0,2,0),使用特定的相机和视图设置。

换句话说,我们需要在photoshop的"消失点"功能背后重现相同的算法。

这个问题的数学解肯定需要定义更多的常数来给出一个解(观察者的距离等)。在构建时修复这些数据应该没有问题。

数学解决方案是值得赞赏的,但最好是参考任何外部数学框架的C/C++代码,用于几何变换等,以将相干数据输入OpenGL相机模型参数。

NyArToolkit和OpenCV都有有趣而复杂的功能,但输入指的是不需要的"培训"。Photoshop的"消失点"功能可以实时工作,只需输入4个二维点即可得出结果。

我们需要的功能类似

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
);

示例明显使用

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];
[...]

任何真正适用于正确计算的实现_3D_scene_From_2D_Rectangle()都将不胜感激。

这听起来像是相机校准,这是计算机视觉中的一项常见任务。OpenCV提供了以下功能:

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

来看看findhomography。这可能已经足够你完成任务了。如果你想进行全相机校准,你需要超过4个点(最小值为6),但对于平面映射,单应性方法可以很好地工作。

最新更新