opencv triangulatePoints,奇怪的结果

时间:2015-05-27 22:05:34

标签: c++ opencv

我在opencv中使用triagulatePoints函数。最后,我在绝望之后让它工作,但结果看起来不对。我已经阅读了其他一些问题,但我仍然不理解!

我正在跑步:

cv::Mat Q,P1,P2,LP,RP,D1,D2,M1,M2;
    char *CALIB_FILE = (char *)"extrinsics.xml";
    FileStorage fs(CALIB_FILE, FileStorage::READ);
    fs["Q"] >> Q;
    fs["P1"] >> P1;
    fs["P2"] >> P2;


    cv::Mat cam0pnts(1, 5, CV_64FC2);  //681 432 479    419 550 320 682 274 495 254
    cv::Mat cam1pnts(1, 5, CV_64FC2); //800 466 587 451 657 352 791 311 592 283

    cv::Mat points_3D(1, 5, CV_64FC4);   


    cv::triangulatePoints(P1, P2, cam0pnts, cam1pnts, points_3D);

P1和P2是来自stereo_calib函数的计算外部函数。

有5个点,一个粗糙的正方形,每个角落有一个点,中间有一个点。

结果矩阵是:

 [-0.6620691274599629, 0.6497615623177577, -0.6585234150236594, 0.6529909432980171, -0.6604373884239706;
  -0.7091492226203088, 0.7208075295879011, -0.7119285643550911, 0.7174438199266364, -0.710244308941275;
  0.242429054072024, -0.2413429417514131, 0.2439357048056051, -0.2426462227979475, 0.2436708320163396;
  -6.52928664505207e-005, -4.348043360405063e-005, -5.515313727475824e-005, -6.149577656504346e-005, -5.668087253108842e-005]

当以3d绘制时,如果完全缩放错误,则会给出两个看起来几乎正确的位置,然后是这两个位置的三个副本。

我在哪里错了?我是否需要对生成的矩阵执行某些操作才能获得xyz坐标?或者我是否错误地实现了该功能?

1 个答案:

答案 0 :(得分:2)

取消,我设法通过忽略cv :: triangulate函数并在此处使用此方法来实现:

http://www.morethantechnical.com/2012/01/04/simple-triangulation-with-opencv-from-harley-zisserman-w-code/

通过一些小修改来修复错误位置的某些代码...

Mat_<double> LinearLSTriangulation(Point3d u,       //homogenous image point (u,v,1)
    Matx34d P,       //camera 1 matrix
    Point3d u1,      //homogenous image point in 2nd camera
    Matx34d P1       //camera 2 matrix
    )
{
    //build matrix A for homogenous equation system Ax = 0
    //assume X = (x,y,z,1), for Linear-LS method
    //which turns it into a AX = B system, where A is 4x3, X is 3x1 and B is 4x1
    Matx43d A(u.x*P(2, 0) - P(0, 0), u.x*P(2, 1) - P(0, 1), u.x*P(2, 2) - P(0, 2),
        u.y*P(2, 0) - P(1, 0), u.y*P(2, 1) - P(1, 1), u.y*P(2, 2) - P(1, 2),
        u1.x*P1(2, 0) - P1(0, 0), u1.x*P1(2, 1) - P1(0, 1), u1.x*P1(2, 2) - P1(0, 2),
        u1.y*P1(2, 0) - P1(1, 0), u1.y*P1(2, 1) - P1(1, 1), u1.y*P1(2, 2) - P1(1, 2)
        );
    Mat_<double> B = (Mat_<double>(4, 1) << -(u.x*P(2, 3) - P(0, 3)),
        -(u.y*P(2, 3) - P(1, 3)),
        -(u1.x*P1(2, 3) - P1(0, 3)),
        -(u1.y*P1(2, 3) - P1(1, 3)));

    Mat_<double> X;
    solve(A, B, X, DECOMP_SVD);

    return X;
}

和此:

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
Log.v("TIMETIME", "schedule reminder: " + hour + ":" + minute);

Intent notificationIntent = new Intent(context, AlarmReceiver.class);
notificationIntent.putExtra(AlarmReceiver.NOTIFICATION_ID, 1);
notificationIntent.putExtra(AlarmReceiver.NOTIFICATION, getNotification(context));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);