sim: camera ve bang GPU (lib tuy chon vslam_sim_gl, EGL + GLES3 + assimp) -- anh IR co texture that + depth khop tung pixel, vslam_node --sim-gl, tool vslam_render_preview

This commit is contained in:
trung09122002
2026-10-03 19:13:50 +07:00
parent 0dd2546ab1
commit f9fd18b5aa
17 changed files with 1329 additions and 22 deletions

View File

@@ -57,6 +57,16 @@ if(VSLAM_BUILD_SLAM)
endif()
endif()
# vslam_render_preview: ve MOT khung camera mo phong bang GPU ra PNG, de nhin
# texture/depth truoc khi tin. Chi khi co lib tuy chon vslam_sim_gl.
if(TARGET vslam_sim_gl)
find_package(OpenCV REQUIRED COMPONENTS core imgproc imgcodecs)
add_executable(vslam_render_preview vslam_render_preview.cc)
target_include_directories(vslam_render_preview SYSTEM PRIVATE ${OpenCV_INCLUDE_DIRS})
target_link_libraries(vslam_render_preview
PRIVATE vslam_sim_gl vslam_sim vslam_common ${OpenCV_LIBS})
endif()
# vslam_record: ghi .vsr tu bo mo phong theo kich ban co ten. Hat giong co
# dinh => cung kich ban cho cung file, byte giong byte -- dieu kien de
# regress.sh so ket qua duoc.

View File

@@ -0,0 +1,127 @@
// vslam_render_preview -- ve MOT khung camera mo phong bang GPU ra file PNG.
//
// vslam_render_preview --world W.world [--models DIR]... --x X --y Y --yaw R
// [--z 0.445] [--pitch 0] [--w 640] [--h 480]
// [--hfov-deg 69 | --fx F] --out PREFIX
//
// Ra: PREFIX_ir.png (anh xam 8 bit), PREFIX_depth.png (16 bit, mm -- dung
// quy uoc RealSense), PREFIX_depth_vis.png (to mau de nhin). In GPU dang dung,
// ti le diem co depth, do sang/do tuong phan anh IR.
//
// Dung de NHIN truoc khi tin: texture co len khong, depth co khop hinh khong,
// camera co dung huong khong. `--z` la do cao camera so voi z = 0 cua world
// (cong them san neu dung tren san cao). `--pitch` duong = chuc xuong.
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include "vslam/gazebo_world.h"
#include "vslam/sim_gl_renderer.h"
int main(int argc, char** argv) {
std::string world, out;
std::vector<std::string> models;
double x = 0, y = 0, z = 0.445, yaw = 0, pitch = 0, hfov_deg = 69.0, fx = 0;
int w = 640, h = 480;
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 == "--world") world = next();
else if (a == "--models") models.push_back(next());
else if (a == "--x") x = std::atof(next().c_str());
else if (a == "--y") y = std::atof(next().c_str());
else if (a == "--z") z = std::atof(next().c_str());
else if (a == "--yaw") yaw = std::atof(next().c_str());
else if (a == "--pitch") pitch = std::atof(next().c_str());
else if (a == "--w") w = std::atoi(next().c_str());
else if (a == "--h") h = std::atoi(next().c_str());
else if (a == "--hfov-deg") hfov_deg = std::atof(next().c_str());
else if (a == "--fx") fx = std::atof(next().c_str());
else if (a == "--out") out = next();
else {
std::fprintf(stderr, "tham so la: %s (xem dau file)\n", a.c_str());
return 2;
}
}
if (world.empty() || out.empty()) {
std::fprintf(stderr, "can --world va --out\n");
return 2;
}
vslam::GazeboWorld gw;
vslam::StatusResponse st = vslam::LoadGazeboWorld(world, models, &gw);
if (!st.ok()) {
std::fprintf(stderr, "world: %s\n", st.message.c_str());
return 1;
}
std::string err;
auto r = vslam::CreateGlCameraRenderer(gw, vslam::DefaultGlRendererConfig(), &err);
if (!r) {
std::fprintf(stderr, "renderer: %s\n", err.c_str());
return 1;
}
// Than: Rz(yaw) * Ry(pitch). Quang hoc: X = -Y than, Y = -Z than, Z = X than.
const double cy = std::cos(yaw), sy = std::sin(yaw), cp = std::cos(pitch), sp = std::sin(pitch);
const double fwd[3] = {cy * cp, sy * cp, -sp};
const double left[3] = {-sy, cy, 0.0};
const double up[3] = {cy * sp, sy * sp, cp};
vslam::SimCameraView v;
for (int i = 0; i < 3; ++i) {
v.R_wc[i * 3 + 0] = -left[i];
v.R_wc[i * 3 + 1] = -up[i];
v.R_wc[i * 3 + 2] = fwd[i];
}
v.t_wc[0] = x;
v.t_wc[1] = y;
v.t_wc[2] = z;
v.width = w;
v.height = h;
v.fx = fx > 0 ? fx : (w / 2.0) / std::tan(hfov_deg * M_PI / 360.0);
v.fy = v.fx;
v.cx = w / 2.0;
v.cy = h / 2.0;
std::vector<uint8_t> ir;
std::vector<float> depth;
if (!r->Render(v, &ir, &depth)) {
std::fprintf(stderr, "ve hong (xem log)\n");
return 1;
}
cv::Mat im_ir(h, w, CV_8UC1, ir.data());
cv::Mat im_d(h, w, CV_16UC1);
size_t valid = 0;
double dmin = 1e9, dmax = 0;
for (int rr = 0; rr < h; ++rr) {
for (int c = 0; c < w; ++c) {
const float d = depth[static_cast<size_t>(rr) * w + c];
im_d.at<uint16_t>(rr, c) = d > 0 ? static_cast<uint16_t>(std::min(65535.0f, d * 1000.0f)) : 0;
if (d > 0) { valid++; dmin = std::min<double>(dmin, d); dmax = std::max<double>(dmax, d); }
}
}
cv::Mat vis8, vis;
im_d.convertTo(vis8, CV_8UC1, 255.0 / std::max(1.0, dmax * 1000.0));
cv::applyColorMap(vis8, vis, cv::COLORMAP_JET);
vis.setTo(cv::Scalar(0, 0, 0), im_d == 0);
cv::imwrite(out + "_ir.png", im_ir);
cv::imwrite(out + "_depth.png", im_d);
cv::imwrite(out + "_depth_vis.png", vis);
cv::Scalar mean, sd;
cv::meanStdDev(im_ir, mean, sd, im_d > 0);
std::printf("%s\n", r->Describe().c_str());
std::printf("depth: %.1f%% diem co gia tri, %.3f..%.3f m | IR (vung co depth): "
"trung binh %.1f, do lech %.1f\n",
100.0 * valid / (static_cast<double>(w) * h), valid ? dmin : 0.0, dmax,
mean[0], sd[0]);
std::printf("ghi %s_ir.png, %s_depth.png, %s_depth_vis.png\n", out.c_str(), out.c_str(),
out.c_str());
return 0;
}