开发者

Function for perspective projection of a matrix in C++

Does anyone have a function that returns the perspective projection of a 3x3 matrix in C++?

Matrix Perspective()
{
   Matrix m(0, 0, 0);  // Creates identity matrix
   // P开发者_JS百科erspective projection formulas here
   return m;
}


Here's one that returns it in a 4x4 matrix, using the formula from the OpenGL gluPerspective man page:

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


With OpenCV 2.0 you can almost implement your pseudocode.

There's a Mat class for matrices and perspectiveTransform for perspective projection. And Mat::eye returns an identity matrix.

The documentation I've linked to is for OpenCV 1.1 (which is in C) but it's quite simple to infer the correct usage in OpenCV 2.0 (with the Mat class) from the manual.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜