1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| void calDLTParams(std::string objCornersFile,std::string imgCornersFile) { cv::vector<cv::Point3f> objCorner; cv::vector<cv::Point2f> imgCorner;
std::ifstream in(objCornersFile.c_str()); if(!in){ std::cout << "Load objCorners ERROR: can't open file " << objCornersFile << std::endl; return; } while(in){ cv::Point3f tmp; in >> tmp.x >> tmp.y >> tmp.z; objCorner.push_back(tmp); } in.close();
in.open(imgCornersFile.c_str()); if(!in){ std::cout << "Load imgCorners ERROR: can't open file " << imgCornersFile << std::endl; return; } while(in){ cv::Point2f tmp; in >> tmp.x >> tmp.y; imgCorner.push_back(tmp); } in.close();
int N = imgCorner.size(); cv::Mat C(2*N, 11, CV_64F); cv::Mat B(2*N, 1, CV_64F);
for(int i=0; i<2*N; i=i+2) { Utilities::matSet2D(C, 0, i, objCorner[i/2].x); Utilities::matSet2D(C, 1, i, objCorner[i/2].y); Utilities::matSet2D(C, 2, i, objCorner[i/2].z); Utilities::matSet2D(C, 3, i, 1);
for(int j=4; j<8; j++) Utilities::matSet2D(C, j, i, 0);
Utilities::matSet2D(C, 8, i, objCorner[i/2].x*imgCorner[i/2].x); Utilities::matSet2D(C, 9, i, objCorner[i/2].y*imgCorner[i/2].x); Utilities::matSet2D(C, 10, i, objCorner[i/2].z*imgCorner[i/2].x); } for(int i=1; i<2*N; i=i+2) { for(int j=0; j<4; j++) Utilities::matSet2D(C, j, i, 0);
Utilities::matSet2D(C, 4, i, objCorner[i/2].x); Utilities::matSet2D(C, 5, i, objCorner[i/2].y); Utilities::matSet2D(C, 6, i, objCorner[i/2].z); Utilities::matSet2D(C, 7, i, 1);
Utilities::matSet2D(C, 8, i, objCorner[i/2].x*imgCorner[i/2].y); Utilities::matSet2D(C, 9, i, objCorner[i/2].y*imgCorner[i/2].y); Utilities::matSet2D(C, 10, i, objCorner[i/2].z*imgCorner[i/2].y); }
for(int i=0; i<2*N; i=i+2) Utilities::matSet2D(B, 0, i, imgCorner[i/2].x); for(int i=1; i<2*N; i=i+2) Utilities::matSet2D(B, 0, i, imgCorner[i/2].y);
cv::Mat L = -(C.t()*C).inv()*C.t()*B; std::string folderPath=""; folderPath += "/DLT.txt";
std::ofstream out(folderPath.c_str()); if(!out){ std::cout << "DLT write ERROR: can't open file " << folderPath << std::endl; return; } for(int i=0; i<11; i++) out << Utilities::matGet2D(L, 0, i) << '\n'; out.flush(); }
|