问题描述
我正在尝试使用 aruco 和 opencv 用 Unity 做一些 AR 实验。
我正在获取相机姿势和 aruco 标记的两个角的位置。现在我想在这两个角上放置两个轴,但结果是这样的:
如您所见,两个轴的中心都应该在两个下角,但它们的位置似乎非常糟糕。 而且,当我移动相机时,例如,远离标记,定位更糟。
我正在采取措施和其他措施来确保坐标正常,所以我在想问题可能是视锥矩阵、场或视图或类似的东西。
这是我所做的:
piece.transform.position= new Vector3((float)c0cords[0],-(float)c0cords[1],(float) c0cords[2]);
在这一行中,我将一个轴放在标记的角 0 中,我反转了“y”。
这是我如何制作截头体矩阵:
bool readMatrixIntrinsic () {
IntPtr Intrinsic = QTNative.getIntrinsicMatrix(pathCalibration);
if (Intrinsic != IntPtr.Zero) {
float[] matrix = new float[9];
Marshal.Copy (Intrinsic,matrix,9);
float fx = matrix[0];
float fy = matrix[4];
float cx = matrix[2];
float cy = matrix[5];
Debug.LogWarning ("*************** Matrix Intrinsic ***************");
Debug.Log ("Vector: fx: " + fx + ",fy: " + fy);
Debug.Log ("Vector: cx: " + cx + ",cy: " + cy);
cameraMain.projectionMatrix = LoadProjectionMatrix (fx,fy,cx,cy);
return true;
}
return false;
}
在这里,我读取带有相机参数的 YML 文件,然后调用 loadProjectionMatrix 来获取该矩阵:
private Matrix4x4 LoadProjectionMatrix (float fx,float fy,float cx,float cy) {
// https://github.com/kylemcdonald/ofxCv/blob/88620c51198fc3992fdfb5c0404c37da5855e1e1/libs/ofxCv/src/Calibration.cpp
//float w = _mainCamera.pixelWidth;
//float h = _mainCamera.pixelHeight;
//float nearDist = _mainCamera.nearClipPlane;
//float farDist = _mainCamera.farClipPlane;
float w = 1280;
float h = 720;
float nearDist = cameraMain.nearClipPlane;
float farDist = cameraMain.farClipPlane;
return MakeFrustumMatrix (
nearDist * (-cx) / fx,nearDist * (w - cx) / fx,nearDist * (cy) / fy,nearDist * (cy - h) / fy,nearDist,farDist);
}
private Matrix4x4 MakeFrustumMatrix (float left,float right,float bottom,float top,float zNear,float zFar) {
// https://github.com/openframeworks/openFrameworks/blob/master/libs/openFrameworks/math/ofMatrix4x4.cpp
// note transpose of ofMatrix4x4 wr.t OpenGL documentation,since the OSG use post multiplication rather than pre.
// NB this has been transposed here from the original openframeworks code
float A = (right + left) / (right - left);
float B = (top + bottom) / (top - bottom);
float C = -(zFar + zNear) / (zFar - zNear);
float D = -2.0f * zFar * zNear / (zFar - zNear);
var persp = new Matrix4x4 ();
persp[0,0] = 2.0f * zNear / (right - left);
persp[1,1] = 2.0f * zNear / (top - bottom);
persp[2,0] = A;
persp[2,1] = B;
persp[2,2] = C;
persp[2,3] = -1.0f;
persp[3,2] = D;
var rhsToLhs = new Matrix4x4 ();
rhsToLhs[0,0] = 1.0f;
rhsToLhs[1,1] = -1.0f; // Flip Y (RHS -> LHS)
rhsToLhs[2,2] = 1.0f;
rhsToLhs[3,3] = 1.0f;
return rhsToLhs * persp.transpose; // see comment above
}
在这个问题中,标记应该总是在同一个位置,我只想在它周围移动相机。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)