简体   繁体   English

如何从openCV与triangulatePoints()获得良好的坐标

[英]how obtain good coordinate with triangulatePoints() from openCV

So I have one point, x:1168px, y:1140px on left image and x:1062px, y:1167px on the right image. 所以我有一点,左图为x:1168px,y:1140px,右图为x:1062px,y:1167px。 The real coordinates I got with my program are: 120.91, 4.94059, -175.609 (x, y and z in mm). 我在程序中得到的实际坐标是:120.91、4.94059,-175.609(x,y和z以毫米为单位)。 X and Y seems correct but not Z, the point is something like 1 meter away from my cameras. X和Y似乎是正确的,但Z却不正确,这个点离我的相机大约1米。

Right now I have this code: 现在我有以下代码:

FileStorage fsI("intrinsics.yml", FileStorage::READ);
Mat matI1, matI2, matD1, matD2;
fsI["M1"] >> matI1;
fsI["M2"] >> matI2;
fsI["D1"] >> matD1;
fsI["D2"] >> matD2;

CvMat camInt1 = matI1, camInt2 = matI2, camDist1 = matD1, camDist2 = matD2;

FileStorage fs("extrinsics.yml", FileStorage::READ);
Mat mat1, mat2;
fs["P1"] >> mat1;
fs["P2"] >> mat2;

CvMat projMat1 = mat1, projMat2 = mat2;

double pointImg1_a[2] = { 1168, 1140};
Mat pointImg1 = Mat(2,1, CV_64FC1, pointImg1_a);
CvMat _pointImg1 = cvMat(1,1,CV_64FC2,pointImg1_a);
double pointImg2_a[2] = { 1062, 1167};
Mat pointImg2 = Mat(2,1, CV_64FC1, pointImg2_a);
CvMat _pointImg2 = cvMat(1,1,CV_64FC2,pointImg1_a);

cvUndistortPoints(&_pointImg1,&_pointImg1,&camInt1,&camDist1);
cvUndistortPoints(&_pointImg2,&_pointImg2,&camInt2,&camDist2);

Mat point4D = Mat(4,1, CV_64FC1);
cv::triangulatePoints(mat1, mat2, pointImg1, pointImg2, point4D);   

double w = point4D.at<double>(3,0);
double x = point4D.at<double>(0,0)/w;
double y = point4D.at<double>(1,0)/w;
double z = point4D.at<double>(2,0)/w;

cout << x << ", " << y << ", " << z << endl;

return 0;

I tried without using cvUndistortPoints() and with other coordinates, the result are sometimes correct but not always. 我试过不使用cvUndistortPoints()和其他坐标,结果有时是正确的,但并非总是如此。 I really don't know from where could come the error. 我真的不知道错误从哪里来。 (Also I'm new with openCV) Thanks for the help. (我也是openCV的新手)感谢您的帮助。

Dont't you have a mistake at line: "CvMat _pointImg2 = cvMat(1,1,CV_64FC2,pointImg1_a);" 您在行上没有犯错:“ CvMat _pointImg2 = cvMat(1,1,CV_64FC2,pointImg1_a);”

Correct should be: "CvMat _pointImg2 = cvMat(1,1,CV_64FC2, pointImg2_a );" 正确的应该是:“ CvMat _pointImg2 = cvMat(1,1,CV_64FC2, pointImg2_a );”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM