C ++中矩阵透视投影的功能

时间:2009-10-28 16:23:42

标签: c++ matrix projection perspective

有没有人有一个函数可以返回C ++中3x3矩阵的透视投影?

Matrix Perspective()
{
   Matrix m(0, 0, 0);  // Creates identity matrix
   // Perspective projection formulas here
   return m;
}

2 个答案:

答案 0 :(得分:3)

这是使用OpenGL gluPerspective man page中的公式以4x4矩阵返回的:

static void my_PerspectiveFOV(double fov, double aspect, double near, double far, double* mret) {
    double D2R = M_PI / 180.0;
    double yScale = 1.0 / tan(D2R * fov / 2);
    double xScale = yScale / aspect;
    double nearmfar = near - far;
    double m[] = {
        xScale, 0, 0, 0,
        0, yScale, 0, 0,
        0, 0, (far + near) / nearmfar, -1,
        0, 0, 2*far*near / nearmfar, 0 
    };    
    memcpy(mret, m, sizeof(double)*16);
}

答案 1 :(得分:0)

使用OpenCV 2.0,您几乎可以实现伪代码。

有一个Mat类用于矩阵,perspectiveTransform用于透视投影。 Mat::eye返回一个单位矩阵。

我链接到的文档适用于OpenCV 1.1(在C中),但在手册中推断OpenCV 2.0(使用Mat类)的正确用法非常简单。