我一直在努力尝试正确获取opengl的透视投影矩阵。使用opencv,我能够完美地在立方体上覆盖立方体(红色线网)。但是,当我从帖子(http://www.morethantechnical.com/2015/02/17/augmented-reality-on-libqglviewer-and-opencv-opengl-tips-wcode/)应用opencv-opengl透视投影时,如下所示,标记上会叠加一个实心立方体,但是当我旋转标记时,实体立方体会慢慢移出标记(使用opencv绘制了https://www.youtube.com/watch?v=m87qAZcN6c4&feature=youtu.be).The红色立方体(wiremesh),并且它完全重叠。
// perspective projection matrix*************************
double fx = intrinsic_matrix.at<double>(0,0);
double fy = intrinsic_matrix.at<double>(1,1);
double cx = intrinsic_matrix.at<double>(0,2);
double cy = intrinsic_matrix.at<double>(1,2);
projection.at<double>(0,0) = fx/cx;
projection.at<double>(1,1) = fy/cy;
projection.at<double>(2,2) = -(far+near)/(far-near);
projection.at<double>(2,3) = -2.0*far*near / (far-near);
projection.at<double>(3,2) = -1.0;
projection = projection.t();
// modelview matrix **********************************************
modelview.at<double>(0,0) = rotation.at<double>(0,0);
modelview.at<double>(1,0) = rotation.at<double>(1,0);
modelview.at<double>(2,0) = rotation.at<double>(2,0);
modelview.at<double>(3,0) = 0;
modelview.at<double>(0,1) = rotation.at<double>(0,1);
modelview.at<double>(1,1) = rotation.at<double>(1,1);
modelview.at<double>(2,1) = rotation.at<double>(2,1);
modelview.at<double>(3,1) = 0;
modelview.at<double>(0,2) = rotation.at<double>(0,2);
modelview.at<double>(1,2) = rotation.at<double>(1,2);
modelview.at<double>(2,2) = rotation.at<double>(2,2);
modelview.at<double>(3,2) = 0;
modelview.at<double>(0,3) = tvecs.at<double>(0);
modelview.at<double>(1,3) = tvecs.at<double>(1);
modelview.at<double>(2,3) = tvecs.at<double>(2);
modelview.at<double>(3,3) = 1;
// This matrix corresponds to the change of coordinate systems.
static double changeCoordArray[4][4] = {{1, 0, 0, 0}, {0, -1, 0, 0}, {0, 0, -1, 0}, {0, 0, 0, 1}};
static Mat changeCoord(4, 4, CV_64FC1, changeCoordArray);
modelview = changeCoord*modelview;
modelview = modelview.t();
有人可以帮我解决这个问题吗?这是我的代码(http://pastebin.com/2MkQvkdN)。
答案 0 :(得分:0)
我认为原因是您要将每个帧与前一帧进行比较,以确定实体立方体应移动到的位置。如果是这种情况,我会尝试将每个帧与第一帧进行比较,以确定立方体应该如何移动。这应该摆脱这种数值漂移。或者,您可以尝试根据检测黑白标记的位置为每个单独的框架放置实体立方体。