终于等到你

众里寻他千百度,蓦然回首,那人却在灯火阑珊处。

处理txt文件,保存为yml和cal文件

xn提供的标定优化后的内外参格式是txt,不满足生成模板的yml和标定测量的cal,故转换

void deal_yml_file() {
    string datapath = "C:/Users/gzr2018/Desktop/xuening'data/0224";
    std::string outpath;
    outpath = datapath + "/yaml";
    boost::filesystem::path diryaml(outpath.c_str());
    boost::filesystem::create_directory(diryaml);
 //   outpath = datapath + "/camera_para(distort)";
 //   boost::filesystem::path diryaml1(outpath.c_str());
 //   boost::filesystem::create_directory(diryaml1);
    outpath = datapath + "/camera_para";
    boost::filesystem::path diryaml2(outpath.c_str());
    boost::filesystem::create_directory(diryaml2);
    for (int i = 0; i <66; ++i)
    {
        std::ifstream in(datapath+"/2-24标定优化/CCD" + std::to_string(i + 1) + ".txt");
        std::ifstream in2(datapath+"/2-24/" + std::to_string(i + 1) + ".txt");
        double temp;
        char buffer[256];
        double kc1, kc2, kc3, kc4;
        double fx, cx, fy, cy;
        std::ofstream out_para(datapath + "/camera_para/CCD" + std::to_string(i + 1) + ".cal");

        double R[9], T[3];
       for (int i = 0; i < 4; i++)in.getline(buffer, 100);
        in >> R[0]; in >> R[1]; in >> R[2]; in >> T[0];
        in >> R[3]; in >> R[4]; in >> R[5]; in >> T[1];
        in >> R[6]; in >> R[7]; in >> R[8]; in >> T[2];
        for (int i = 0; i < 4; i++)
        in2.getline(buffer, 100); 
        in2 >> fx; in2 >> temp; in2 >> cx;
        in2 >> temp; in2 >> fy; in2 >> cy;
        in2.getline(buffer, 100);
        in2.getline(buffer, 100);
        in2 >> kc1 >> kc2 >> kc3 >> kc4;

    
        outpath = datapath + "/yaml";
        //将优化后的相机位姿和工件位姿输出
        cv::FileStorage fs(outpath + "/camera-CCD" + std::to_string(i + 1) + ".yml", cv::FileStorage::WRITE);
        in >> R[0]; in >> R[1]; in >> R[2]; in >> T[0];
        in >> R[3]; in >> R[4]; in >> R[5]; in >> T[1];
        in >> R[6]; in >> R[7]; in >> R[8]; in >> T[2];
        cv::Mat rvec(3, 3, cv::DataType<double>::type);
        cv::Mat tvec(3, 1, cv::DataType<double>::type);
        rvec = (cv::Mat_<double>(3, 3) <<
            R[0], R[1], R[2], R[3], R[4], R[5], R[6], R[7], R[8]);
        tvec = (cv::Mat_<double>(3, 1) << T[0], T[1], T[2]);
        cv::Mat second_rt(4, 4, cv::DataType<double>::type);
        second_rt = (cv::Mat_<double>(4, 4) <<
            1, 0, 0, 0,
            0,1,0, 0,
            0, 0, 1, 0,
            0, 0, 0, 1
            );
        cv::Mat rt(4, 4, cv::DataType<double>::type);
        rt = second_rt;

        cv::Mat cameraMatrix = (cv::Mat_<double>(3, 3) << fx, 0, cx, 0, fy, cy, 0, 0, 1);
        cv::Mat distCoeffs = (cv::Mat_<double>(4, 1) << kc1, kc2, kc3, kc4);

        fs << "trans_mat" << rt << "intrinsic_matrix" << cameraMatrix << "extrinsic_matrix_r" << rvec
            << "extrinsic_matrix_t" << tvec << "distCoeffs" << distCoeffs;
        out_para<< R[0] << " " << R[1] << " " << R[2] <<" "<< T[0] << endl;
        out_para << R[3] << " " << R[4] << " " << R[5] <<" "<< T[1] << endl;
        out_para << R[6] << " " << R[7] << " " << R[8] <<" "<< T[2] << endl;
        out_para << 0 << " " << 0 << " " << 0<<" "<< 1 << endl;
        out_para << endl;
        out_para << fx << " " << 0 << " " << cx << endl;
        out_para << 0 << " " << fy << " " << cy << endl;
        out_para << 0 << " " << 0 << " " << 1 << endl;
        out_para << endl;
        out_para << kc1 << " " << kc2 << " " << kc3 << " " << kc4 << endl;
        out_para.close();
        fs.release();
    }   

}
posted @ 2021-02-24 10:08  gzr2018  阅读(596)  评论(0编辑  收藏  举报