已知两个相机的位姿,求解两者的相对位姿

int main()
{
    int cam_num = 1;
    //读取所有图像
    vector<string> imgLs;
    vector<string> imgRs;

    char imgLName[255] = "/home/Gradute/3dReconstruction/datasets/0602/0601left/%04d.jpg";
    char imgRName[255] = "/home/Gradute/3dReconstruction/datasets/0602/0601right/%04d.jpg";
    char calibpath[255] = "/home/Gradute/3dReconstruction/datasets/0602/%04d.xml";
    vector<std::string> leftCalibColmap;
    vector<std::string> rightCalibColmap;
    for(int i = 0; i < 1; i++){
        char imgLNameTmp[255];
        char imgRNameTmp[255];
	char imgLXmlTmp[255];
        char imgRxmlTmp[255];
        sprintf(imgLNameTmp,imgLName,i);
	sprintf(imgLXmlTmp,calibpath,i);
        // Mat imgL = imread(imgLNameTmp);
	// imgL

        sprintf(imgRNameTmp,imgRName,i);
	sprintf(imgRxmlTmp,calibpath,i+cam_num);
        // Mat imgR = imread(imgRNameTmp);
        imgLs.push_back(imgLNameTmp);
        imgRs.push_back(imgRNameTmp);

	leftCalibColmap.push_back(imgLXmlTmp);
        rightCalibColmap.push_back(imgRxmlTmp);
    }
    for(int i=0;i<1;++i){

	string imgLpath = imgLs[i];
	string imgRpath = imgRs[i];

	string xmlLPath = leftCalibColmap[i];
	string xmlRPath = rightCalibColmap[i];
	cout << xmlLPath<<endl;
	cout << xmlRPath<<endl;
	cv::Mat imgL = cv::imread(imgLpath);

	cv::Mat tranRL, transRR, transtL,transtR;
	cv::Mat T_Lw, T_Rw, T_wL, T_RL;
	cv::Mat Matrix1,Matrix2, dist1,dist2;
	cv::Mat last_row = (cv::Mat_<double>(1,4)<<0,0,0,1);
	FileStorage xmlLfile(xmlLPath, FileStorage::READ);
	FileStorage xmlRfile(xmlRPath, FileStorage::READ);

	xmlLfile["R"]>>tranRL;
	xmlLfile["t"]>>transtL;
	xmlLfile["IntrinsicCam"]>>Matrix1;
	xmlLfile["DistortionCam"]>>dist1;
	xmlLfile.release();

	tranRL.convertTo(tranRL, CV_64F);
	transtL.convertTo(transtL, CV_64F);
	Matrix1.convertTo(Matrix1, CV_64F);
	dist1.convertTo(dist1, CV_64F);
		

	xmlRfile["R"]>>transRR;
	xmlRfile["t"]>>transtR;
	xmlRfile["IntrinsicCam"]>>Matrix2;
	xmlRfile["DistortionCam"]>>dist2;
	xmlRfile.release();

	transRR.convertTo(transRR, CV_64F);
	transtR.convertTo(transtR, CV_64F);
	Matrix2.convertTo(Matrix2, CV_64F);
	dist2.convertTo(dist2, CV_64F);


	cv::Mat RtL_34, RtR_34;
	cv::hconcat(tranRL, transtL, RtL_34); // 3x4	hconcat??B,C??A??; // ?????A=[B  C]		
	cv::vconcat(RtL_34, last_row, T_Lw); // 4x4	 vconcat??B,C??A??; // ?????A=[B ;C]
		
	cv::hconcat(transRR, transtR, RtR_34); // 3x4	hconcat??B,C??A??; // ?????A=[B  C]		
	cv::vconcat(RtR_34, last_row, T_Rw); // 4x4	 vconcat??B,C??A??; // ?????A=[B ;C]

	T_wL = T_Lw.inv();

	T_RL =  T_Rw * T_wL;		// T_rl = T_rw * T_wl

	cv::Mat R = T_RL(Rect(0,0,3,3));
	// cv::Mat T = T_l2r.colRange(3,4).clone();
	cv::Mat T = T_RL(Rect(3,0,1,3));


	// https://blog.csdn.net/TimeRiverForever/article/details/118487001
	// cv::Mat R21 = transRR * tranRL.inv();
	// cv::Mat T21 = transtR - transRR * tranRL.inv() * transtL;

	// cout << T_l2r<<endl;
	// cout << R<<endl;
	// cout << T<<endl;
	// cout << R21<<endl;
	// cout << T21<<endl;


	return 0;
}


posted @ 2022-06-20 18:11  小小灰迪  阅读(345)  评论(0编辑  收藏  举报