// Ban C++ cua tools/reloc_pnp_eval.py, de doi chieu VisualVerifier voi ban Python da do. // // vslam_pnp_eval --db DB --in logs/reloc_pnp_R3.csv --out logs/reloc_pnp_R3_cpp.csv // // --in la CSV cua reloc_pnp_eval.py (da co truth_wall_overlap). Tinh lai PnP bang C++ cho moi // dong (node hoi, ung vien VPR), GHI DE cac cot pnp_*, giu nguyen cot khac -> cham bang // tools/reloc_fusion_analyze.py y nhu ban Python. #include #include #include #include #include #include #include #include #include #include "vslam/visual_verifier.h" namespace { std::vector Split(const std::string& s) { std::vector out; std::stringstream ss(s); std::string f; while (std::getline(ss, f, ',')) out.push_back(f); return out; } cv::Matx44d ToMatx(const rtabmap::Transform& t) { return cv::Matx44d(t.r11(), t.r12(), t.r13(), t.x(), t.r21(), t.r22(), t.r23(), t.y(), t.r31(), t.r32(), t.r33(), t.z(), 0, 0, 0, 1); } } // namespace int main(int argc, char** argv) { std::string db, in, out; for (int i = 1; i < argc; ++i) { const std::string a = argv[i]; auto next = [&]() -> std::string { return i + 1 < argc ? argv[++i] : ""; }; if (a == "--db") db = next(); else if (a == "--in") in = next(); else if (a == "--out") out = next(); } if (db.empty() || in.empty() || out.empty()) { std::fprintf(stderr, "cach dung: %s --db DB --in PNP_CSV --out CSV\n", argv[0]); return 2; } std::unique_ptr driver(rtabmap::DBDriver::create()); if (!driver->openConnection(db, false)) { std::fprintf(stderr, "khong mo duoc %s\n", db.c_str()); return 1; } const std::map opt = driver->loadOptimizedPoses(); vslam::VisualVerifier vv; std::map cache; auto frame = [&](int id, bool need_depth) -> const vslam::VisualFrame& { auto it = cache.find(id); if (it != cache.end() && (!need_depth || !it->second.depth.empty())) return it->second; rtabmap::SensorData d; driver->getNodeData(id, d, true, false, false, false); d.uncompressData(); vslam::VisualFrame f; if (!d.imageRaw().empty() && !d.cameraModels().empty()) { f = vv.Extract(d.imageRaw()); const rtabmap::CameraModel& cm = d.cameraModels()[0]; f.K = cv::Matx33d(cm.fx(), 0, cm.cx(), 0, cm.fy(), cm.cy(), 0, 0, 1); f.T_base_optical = ToMatx(cm.localTransform()); if (need_depth) f.depth = d.depthRaw().clone(); } return cache[id] = f; }; std::ifstream fin(in); std::ofstream fout(out); std::string line; if (!std::getline(fin, line)) return 1; const std::vector head = Split(line); std::map col; for (size_t i = 0; i < head.size(); ++i) col[head[i]] = i; for (const char* k : {"query_id", "rank", "vpr_node", "pnp_nmatch", "pnp_inl", "pnp_x", "pnp_y", "pnp_th", "pnp_dz", "pnp_tilt"}) { if (!col.count(k)) { std::fprintf(stderr, "%s thieu cot %s -- can CSV cua reloc_pnp_eval.py\n", in.c_str(), k); return 1; } } fout << line << "\n"; int rows = 0, solved = 0, both = 0, py_only = 0, cpp_only = 0; while (std::getline(fin, line)) { std::vector f = Split(line); if (f.size() != head.size()) continue; const int qid = std::stoi(f[col["query_id"]]), mid = std::stoi(f[col["vpr_node"]]); const bool py_ok = std::stoi(f[col["pnp_inl"]]) >= 12; vslam::PnpResult r; if (std::stoi(f[col["rank"]]) > 0 && opt.count(mid)) { r = vv.Solve(frame(qid, false), frame(mid, true), ToMatx(opt.at(mid))); } char b[64]; f[col["pnp_nmatch"]] = std::to_string(r.matches); f[col["pnp_inl"]] = std::to_string(r.inliers); std::snprintf(b, sizeof(b), "%.3f", r.x); f[col["pnp_x"]] = b; std::snprintf(b, sizeof(b), "%.3f", r.y); f[col["pnp_y"]] = b; std::snprintf(b, sizeof(b), "%.4f", r.yaw); f[col["pnp_th"]] = b; std::snprintf(b, sizeof(b), "%.3f", r.dz); f[col["pnp_dz"]] = b; std::snprintf(b, sizeof(b), "%.2f", r.tilt_deg); f[col["pnp_tilt"]] = b; for (size_t i = 0; i < f.size(); ++i) fout << (i ? "," : "") << f[i]; fout << "\n"; ++rows; if (r.inliers > 0) ++solved; const bool cpp_ok = r.inliers >= 12; both += py_ok && cpp_ok; py_only += py_ok && !cpp_ok; cpp_only += !py_ok && cpp_ok; } driver->closeConnection(false); std::printf("%d dong, C++ PnP ra nghiem %d; >= 12 inlier: ca hai %d, chi Python %d, chi C++ %d -> %s\n", rows, solved, both, py_only, cpp_only, out.c_str()); return 0; }