TUM数据集构建动态的稠密点云

目录

构建点云要用到PCL库

 实现难点

1. 稠密重建的原理是什么

 2. 怎么实现rgb图、深度图、位姿图的对齐

3. 相机的内参是什么

4. 怎么实现实时图片和点云图的显示

总代码


本文的目的是通过TUM数据集构建一个实时变化的动态点云。

构建点云要用到PCL库 

本文的TUM数据集下载的是fr1xyz

pcl::PointCloud()函数,

pcl::PointXYZRGB类型(pcl::PointXYZRGB定义的量由六个参数组成,前三个是世界坐标,后三个是rgb)

pcl::visualization::CloudViewer viewer ("Viewer")函数,可视化 

 实现难点

1. 稠密重建的原理是什么

2. 怎么实现rgb图、深度图、位姿图的对齐 

3. 相机的内参是什么

4. 怎么实现实时图片和点云图的显示

1. 稠密重建的原理是什么

目的:得到世界坐标并染色。

A. 首先遍历深度图的所有像素,将2维点装换到3维的空间坐标。再通过u=fx*X/Z+cx的变形。这里的Z是已知的,因此,通过这个可以得到相机坐标,再通过位姿的R、t变化得到世界坐标

注意:v在数组里是纵坐标,u是横坐标。rgb三个对应。

pcl::PointXYZRGB定义的量由六个参数组成,前三个是世界坐标,后三个是rgb。因此实现了染色

B.本程序还用了三个滤波器来优化地图:
        1. 统计滤波器去除孤立点
        2. 深度滤波器去除深度无效的点

        PointCloud::Ptr tmp(new PointCloud);
        pcl::StatisticalOutlierRemoval<PointT> statistical_filter;
        //设置参数
        statistical_filter.setMeanK(50);
        statistical_filter.setStddevMulThersh(1.0);
        //设置输入点云
        statistical_filter.setInputClould(current);
        //开始滤波
        statistical_filter.filter(*tmp);
        //点云拼合
        (*pointCloud)+=*tmp

         3. 体素滤波器去除一个小空间内重复的点。

    pcl::VoxelGrid<PointT> voxel_filter;
    double resolution = 0.03;
     // resolution:分辨率
    voxel_filter.setLeafSize(resolution, resolution, resolution);      
    PointCloud::Ptr tmp(new PointCloud);       
    //输入要滤波的点云                                         
    voxel_filter.setInputCloud(pointCloud);
    //tmp为滤波输出
    voxel_filter.filter(*tmp);
    //复制到pointCloud
    tmp->swap(*pointCloud);

注意这里的resolution是分辨率,单位是米,可以改的。数值越大最后得到的点越少。

resolution=0.003,最清晰

 resolution=0.03

 resolution=0.3几乎看不见

 2. 怎么实现rgb图、深度图、位姿图的对齐

需要用到一个python脚本。一次能够对两个.txt文件进行对齐,因此进行两次操作。

python associate.py rgb.txt depth.txt > associate.txt
python associate.py associate.txt groundtruth.txt > associations.txt

 得到的associations.txt就是我们要的文件

3. 相机的内参是什么

 (图来自高翔博士的blog)

4. 怎么实现实时图片和点云图的显示

A.实现图片的显示

这个很简单,在读取循环图片时,加入

cv::imshow("img",colorImgs[i]);
cv::waitKey(30);//30s表示持续时间

B.实现点云实时的显示

这个要用到pcl库的viewer,在读取循环图片时,加入

viewer.showCloud(pointCloud);

可以在循环之后加入下面,使窗口不会直接关闭

    while (!viewer.wasStopped ())
    {
        
    }

总代码

本文使用了tum数据集的79帧。

也可以到笔者的GitHub上面下载dense4

#include <iostream>
#include <fstream>

using namespace std;
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <Eigen/Geometry>
#include <boost/format.hpp>  // for formating strings
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/visualization/cloud_viewer.h>
int main(int argc, char **argv) {
    int N=79;
    vector<cv::Mat> colorImgs, depthImgs;    // 彩色图和深度图
    vector<Eigen::Isometry3d> poses;         // 相机位姿

    ifstream fin("../associations.txt");
    if (!fin) {
        cerr << "cannot find pose file" << endl;
        return 1;
    }

    for (int i = 0; i < N; i++) {
        string data[12] ;
        for (int i = 0; i < 12; i++) {
            fin >> data[i];
        }
        colorImgs.push_back(cv::imread("/home/wood/data_for_orbslam2/rgbd_dataset_freiburg1_xyz/"+data[1]));
        depthImgs.push_back(cv::imread("/home/wood/data_for_orbslam2/rgbd_dataset_freiburg1_xyz/"+data[3], -1)); // 使用-1读取原始图像
        double double_data[7];
        for(int j=5;j<12;j++)
            double_data[j-5]=atof(data[j].c_str());
        Eigen::Quaterniond q(double_data[6], double_data[3], double_data[4], double_data[5]);
        Eigen::Isometry3d T(q);
        T.pretranslate(Eigen::Vector3d(double_data[0], double_data[1], double_data[2]));
        poses.push_back(T);//位姿
    }
    // 计算点云并拼接
    // 相机内参 
    double cx = 318.6;
    double cy = 255.3;
    double fx = 517.3;
    double fy = 516.5;
    double depthScale = 5000.0;

    cout << "正在将图像转换为点云..." << endl;

    // 定义点云使用的格式:这里用的是XYZRGB
    typedef pcl::PointXYZRGB PointT;
    typedef pcl::PointCloud<PointT> PointCloud;

    // 新建一个点云
    PointCloud::Ptr pointCloud(new PointCloud);
    //viewer
    pcl::visualization::CloudViewer viewer ("Viewer");
    for (int i = 0; i < N; i++) {
        PointCloud::Ptr current(new PointCloud);
        cout << "转换图像中: " << i + 1 << endl;
        cv::Mat color = colorImgs[i];
        cv::Mat depth = depthImgs[i];
        Eigen::Isometry3d T = poses[i];
        for (int v = 0; v < color.rows; v++)
            for (int u = 0; u < color.cols; u++) {
                unsigned int d = depth.ptr<unsigned short>(v)[u]; // 深度值
                if (d == 0) continue; // 为0表示没有测量到
                Eigen::Vector3d point;
                point[2] = double(d) / depthScale;
                point[0] = (u - cx) * point[2] / fx;//相机坐标
                point[1] = (v - cy) * point[2] / fy;
                Eigen::Vector3d pointWorld = T * point;//世界坐标

                PointT p;//the fisrt 3 dimensions of p is directions and the next 3 is rgb
                p.x = pointWorld[0];
                p.y = pointWorld[1];
                p.z = pointWorld[2];
                p.b = color.data[v * color.step + u * color.channels()];
                p.g = color.data[v * color.step + u * color.channels() + 1];
                p.r = color.data[v * color.step + u * color.channels() + 2];
                current->points.push_back(p);
            }
        // depth filter and statistical removal 
        PointCloud::Ptr tmp(new PointCloud);
        pcl::StatisticalOutlierRemoval<PointT> statistical_filter;
        statistical_filter.setMeanK(50);
        statistical_filter.setStddevMulThresh(1.0);
        statistical_filter.setInputCloud(current);
        statistical_filter.filter(*tmp);
        
        (*pointCloud) += *current;
        cv::imshow("img",colorImgs[i]);
        cv::waitKey(30);
        viewer.showCloud(pointCloud);
    }
    while (!viewer.wasStopped ())
    {
        
    }
    pointCloud->is_dense = false;
    cout << "点云共有" << pointCloud->size() << "个点." << endl;

    // voxel filter 
    pcl::VoxelGrid<PointT> voxel_filter;
    double resolution = 0.03;
    voxel_filter.setLeafSize(resolution, resolution, resolution);       // resolution
    PointCloud::Ptr tmp(new PointCloud);
    voxel_filter.setInputCloud(pointCloud);
    voxel_filter.filter(*tmp);
    tmp->swap(*pointCloud);
    cout << "滤波之后,点云共有" << pointCloud->size() << "个点." << endl;

    pcl::io::savePCDFileBinary("map.pcd", *pointCloud);
    return 0;
}

效果图

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
点云的表面生成是将稀疏的无结构点云重新生成为表面mesh的过程。一种Pytorch实现方法是通过学习高效的点云生成方法用于稠密三维物体重建。这种方法利用二维卷积运算从多个角度预测三维结构,并将几何推理与二维投影优化相结合。同时,引入伪渲染器这一可微模块来近似真实的渲染操作,以合成新的深度图进行优化。实验结果表明,这种方法在形状相似性和预测密度方面优于最新的方法。\[2\] 该方法的主要原理是通过二维卷积运算的深网络来生成密集的点云,从而塑造三维物体的表面。通过引入伪渲染管道作为真实渲染的可微近似,利用伪渲染深度图像进行二维投影优化,从而学习生成密集的三维形状。这种方法有效地将二维信息转化为三维形状,提高了表面重建的效果。\[3\] #### 引用[.reference_title] - *1* [『OPEN3D』1.4 点云表面重建](https://blog.csdn.net/qq_41366026/article/details/129330287)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [3D点云重建原理及Pytorch实现](https://blog.csdn.net/jacke121/article/details/129312222)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值