reloc: VisualVerifier (ORB + PnP bang depth anh ban do) + vslam_pnp_eval -- trung khop ban Python 2430/2430 cap

This commit is contained in:
trung09122002
2026-09-30 15:09:53 +07:00
parent 2fc289db1c
commit c6c42d177f
5 changed files with 268 additions and 0 deletions

View 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