reloc: VisualVerifier (ORB + PnP bang depth anh ban do) + vslam_pnp_eval -- trung khop ban Python 2430/2430 cap
This commit is contained in:
@@ -40,6 +40,7 @@ vslam_add_library(vslam_slam
|
||||
voxel_export.h
|
||||
global_localizer.h
|
||||
vpr_encoder.h
|
||||
visual_verifier.h
|
||||
FIND_DEPS
|
||||
vslam_common
|
||||
RTABMap
|
||||
|
||||
65
libs/vslam_slam/include/vslam/visual_verifier.h
Normal file
65
libs/vslam_slam/include/vslam/visual_verifier.h
Normal file
@@ -0,0 +1,65 @@
|
||||
#ifndef VSLAM_VISUAL_VERIFIER_H
|
||||
#define VSLAM_VISUAL_VERIFIER_H
|
||||
|
||||
// Xac minh ung vien VPR bang ANH: so khop diem dac trung (ORB) giua anh hoi va anh ban do,
|
||||
// nang diem ban do len 3D bang depth cua chinh anh do, roi PnP RANSAC -> pose robot.
|
||||
//
|
||||
// Ban C++ cua tools/reloc_pnp_eval.py -- CUNG tham so. Do 2026-09-30 (camera + LiDAR, chot
|
||||
// khi pose PnP trung khop LiDAR cuc bo quanh node anh VPR): 25/09 85 -> 159 dung, 28/09
|
||||
// 29 -> 40 dung, 0 chot SAI ca hai. Diem dac trung nho (goc cua, poster) nhin NGUOC lai thi
|
||||
// khac han -> pha duoc doi xung hanh lang ma LiDAR va hinh khoi depth deu bi lua.
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <opencv2/core.hpp>
|
||||
#include <opencv2/features2d.hpp>
|
||||
|
||||
namespace vslam {
|
||||
|
||||
struct VisualVerifierConfig {
|
||||
int orb_features = 1500;
|
||||
int fast_threshold = 7;
|
||||
double clahe_clip = 3.0; // anh IR toi: CLAHE cho x2.7 diem ORB
|
||||
int clahe_grid = 8;
|
||||
double ratio = 0.8; // ratio test Lowe
|
||||
double depth_min_m = 0.3;
|
||||
double depth_max_m = 5.0;
|
||||
int ransac_iters = 300;
|
||||
double reproj_px = 4.0;
|
||||
double confidence = 0.999;
|
||||
int min_points = 8;
|
||||
};
|
||||
|
||||
// Mot anh da trich dac trung, kem hinh hoc camera.
|
||||
struct VisualFrame {
|
||||
std::vector<cv::KeyPoint> kp;
|
||||
cv::Mat des;
|
||||
cv::Mat depth; // CV_16UC1 (mm) hoac CV_32FC1 (m); rong neu khong can
|
||||
cv::Matx33d K;
|
||||
cv::Matx44d T_base_optical; // base (z tinh tu san) <- optical
|
||||
};
|
||||
|
||||
struct PnpResult {
|
||||
int matches = 0; // cap qua ratio test
|
||||
int inliers = 0; // inlier PnP RANSAC; 0 = khong ra nghiem
|
||||
double x = 0.0, y = 0.0, yaw = 0.0; // base cua anh hoi trong map
|
||||
double dz = 0.0, tilt_deg = 0.0; // robot tren san: phai ~0 (lech = nghiem suy bien)
|
||||
};
|
||||
|
||||
class VisualVerifier {
|
||||
public:
|
||||
explicit VisualVerifier(const VisualVerifierConfig& cfg = VisualVerifierConfig());
|
||||
// CLAHE + ORB. `depth`, `K`, `T_base_optical` nguoi goi dien sau.
|
||||
VisualFrame Extract(const cv::Mat& gray) const;
|
||||
// Pose base cua anh hoi `q` trong map, tu diem khop voi anh ban do `m` (can depth) co pose
|
||||
// map <- base `T_map_base_m`.
|
||||
PnpResult Solve(const VisualFrame& q, const VisualFrame& m, const cv::Matx44d& T_map_base_m) const;
|
||||
|
||||
private:
|
||||
VisualVerifierConfig cfg_;
|
||||
cv::Ptr<cv::ORB> orb_;
|
||||
};
|
||||
|
||||
} // namespace vslam
|
||||
|
||||
#endif // VSLAM_VISUAL_VERIFIER_H
|
||||
75
libs/vslam_slam/src/visual_verifier.cc
Normal file
75
libs/vslam_slam/src/visual_verifier.cc
Normal file
@@ -0,0 +1,75 @@
|
||||
#include "vslam/visual_verifier.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
#include <opencv2/calib3d.hpp>
|
||||
#include <opencv2/imgproc.hpp>
|
||||
|
||||
namespace vslam {
|
||||
|
||||
VisualVerifier::VisualVerifier(const VisualVerifierConfig& cfg)
|
||||
: cfg_(cfg),
|
||||
orb_(cv::ORB::create(cfg.orb_features, 1.2f, 8, 31, 0, 2, cv::ORB::HARRIS_SCORE, 31,
|
||||
cfg.fast_threshold)) {}
|
||||
|
||||
VisualFrame VisualVerifier::Extract(const cv::Mat& gray) const {
|
||||
VisualFrame f;
|
||||
cv::Mat g = gray, eq;
|
||||
if (g.channels() == 3) cv::cvtColor(gray, g, cv::COLOR_BGR2GRAY);
|
||||
cv::createCLAHE(cfg_.clahe_clip, cv::Size(cfg_.clahe_grid, cfg_.clahe_grid))->apply(g, eq);
|
||||
orb_->detectAndCompute(eq, cv::noArray(), f.kp, f.des);
|
||||
return f;
|
||||
}
|
||||
|
||||
PnpResult VisualVerifier::Solve(const VisualFrame& q, const VisualFrame& m,
|
||||
const cv::Matx44d& T_map_base_m) const {
|
||||
PnpResult r;
|
||||
if (q.des.empty() || m.des.empty() || q.kp.size() < 8 || m.kp.size() < 8 || m.depth.empty()) {
|
||||
return r;
|
||||
}
|
||||
std::vector<std::vector<cv::DMatch>> knn;
|
||||
cv::BFMatcher(cv::NORM_HAMMING).knnMatch(q.des, m.des, knn, 2);
|
||||
const cv::Matx44d Twm = T_map_base_m * m.T_base_optical; // map <- optical cua anh ban do
|
||||
const double fx = m.K(0, 0), fy = m.K(1, 1), cx = m.K(0, 2), cy = m.K(1, 2);
|
||||
std::vector<cv::Point3d> obj;
|
||||
std::vector<cv::Point2d> img;
|
||||
for (const auto& p : knn) {
|
||||
if (p.size() < 2 || p[0].distance >= cfg_.ratio * p[1].distance) continue;
|
||||
++r.matches;
|
||||
const cv::Point2f uv = m.kp[p[0].trainIdx].pt;
|
||||
const int row = static_cast<int>(std::lround(uv.y)), col = static_cast<int>(std::lround(uv.x));
|
||||
if (row < 0 || col < 0 || row >= m.depth.rows || col >= m.depth.cols) continue;
|
||||
const double z = m.depth.type() == CV_16UC1 ? m.depth.at<uint16_t>(row, col) * 0.001
|
||||
: static_cast<double>(m.depth.at<float>(row, col));
|
||||
if (!(z > cfg_.depth_min_m && z < cfg_.depth_max_m)) continue;
|
||||
const cv::Vec4d pc((uv.x - cx) * z / fx, (uv.y - cy) * z / fy, z, 1.0);
|
||||
const cv::Vec4d pw = Twm * pc;
|
||||
obj.emplace_back(pw[0], pw[1], pw[2]);
|
||||
img.emplace_back(q.kp[p[0].queryIdx].pt);
|
||||
}
|
||||
if (static_cast<int>(obj.size()) < cfg_.min_points) return r;
|
||||
cv::Mat rvec, tvec;
|
||||
std::vector<int> inl;
|
||||
const bool ok = cv::solvePnPRansac(obj, img, cv::Mat(q.K), cv::noArray(), rvec, tvec, false,
|
||||
cfg_.ransac_iters, static_cast<float>(cfg_.reproj_px),
|
||||
cfg_.confidence, inl, cv::SOLVEPNP_EPNP);
|
||||
if (!ok || inl.empty()) return r;
|
||||
cv::Mat R;
|
||||
cv::Rodrigues(rvec, R);
|
||||
cv::Matx44d Tcw = cv::Matx44d::eye(); // optical <- map
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
for (int j = 0; j < 3; ++j) Tcw(i, j) = R.at<double>(i, j);
|
||||
Tcw(i, 3) = tvec.at<double>(i);
|
||||
}
|
||||
const cv::Matx44d Twb = Tcw.inv() * q.T_base_optical.inv(); // map <- base cua anh hoi
|
||||
r.inliers = static_cast<int>(inl.size());
|
||||
r.x = Twb(0, 3);
|
||||
r.y = Twb(1, 3);
|
||||
r.yaw = std::atan2(Twb(1, 0), Twb(0, 0));
|
||||
r.dz = Twb(2, 3);
|
||||
r.tilt_deg = std::acos(std::max(-1.0, std::min(1.0, Twb(2, 2)))) * 180.0 / M_PI;
|
||||
return r;
|
||||
}
|
||||
|
||||
} // namespace vslam
|
||||
Reference in New Issue
Block a user