调用pcl计算法向量,并将法向量可视化

在ubuntu 14.04上,利用pcl库处理双边滤波后的640*480深度图(DEPTH_METRIC_FILTERED.txt),计算得到法向量并进行可视化。输出包括二维法向量图像和三维点云数据。该过程借助qtCreator 4.8和opencv2.4.9完成。
摘要由CSDN通过智能技术生成

编译环境: ubuntu 14.04, qtCreator 4.8, pcl 1.8, opencv2.4.9

input: 双边滤波处理后的深度图(图像大小640*480,深度值范围0~2.226)生成的txt文件("DEPTH_METRIC_FILTERED.txt")

txt文件链接:(明明上传到我的资源里了,可是就是不显示)

output: 法向量可视化二位图和三维点云数据


main.cpp

#include <pcl/PCLPointCloud2.h>
#include <pcl/io/pcd_io.h>
#include <pcl/features/normal_3d.h>
#include <pcl/features/integral_image_normal.h>
#include <pcl/console/print.h>
#include <pcl/console/parse.h>
#include <pcl/console/time.h>
#include <pcl/visualization/pcl_visualizer.h>

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>

using namespace std;
using namespace cv;

//using namespace pcl;
//using namespace pcl::io;
//using namespace pcl::console;

typedef pcl::PointXYZRGB PointT;

int main()
{
    ifstream fin_depth("DEPTH_METRIC_FILTERED.txt");//DEPTH_METRIC_FILTERED DEPTH_RAW
    ifstream fin_color("colorBuff.txt");
    assert(fin_depth.is_open());//if open file failure,then stop
    assert(fin_color.is_open());//if open file failure,then stop

    float depth[480][640];
    //Mat depth_map(480,640,CV_8UC1);
    float color[480][640*3];
    Mat color_map(480,640,CV_8UC3);


    for(int i=0 ; i<480 ; i++)
    {
        for(int j=0;j<640;j++)
        {
            fin_depth>>depth[i][j];

            fin_color>>color[i][j*3];
            fin_color>>color[i][j*3+1];
            fin_color>>color[i][j*3+2];
        }
    }
    fin_depth.close();
    fin_color.close();

       // 点云变量
        // 使用智能指针,创建一个空点云。这种指针用完会自动释放。
    pcl::PointCloud<PointT>::Ptr cloud (new pcl::PointCloud<PointT>);
    int ifvalid[640*480]={0};
        // 遍历深度图
        for (int i = 0; i < 480; i++)
        {
            for (int j=0; j < 640; j++)
            {
                // 获取深度图中(m,n)处的值
//                float d = depth[.ptr<ushort>[i][j];
                // d 可能没有值,若如此,跳过此点
                if (fabs(depth[i][j])< 0.001)
                {
                    ifvalid[640*i+j]=0;
                    continue;
                }
               else
                {
                    ifvalid[640*i+j]=1;

                    // d 存在值,则向点云增加一个点
                    PointT p;

                    // 计算这个点的空间坐标
                    p.z = double(depth[i][j]) ;///1000.0;
                    p.y = (i-241.0)*p.z/558.0 ;
                    
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
计算向量可以使用 pcl::NormalEstimation 类,可视化可以使用 pcl::visualization::PCLVisualizer 类。 以下是一个简单的示例代码: ```cpp #include <iostream> #include <pcl/point_types.h> #include <pcl/features/normal_3d.h> #include <pcl/io/pcd_io.h> #include <pcl/visualization/pcl_visualizer.h> int main(int argc, char** argv) { // Load point cloud data from file pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); pcl::io::loadPCDFile<pcl::PointXYZ>("cloud.pcd", *cloud); // Estimate normals pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne; ne.setInputCloud(cloud); pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>()); ne.setSearchMethod(tree); pcl::PointCloud<pcl::Normal>::Ptr cloud_normals(new pcl::PointCloud<pcl::Normal>); ne.setRadiusSearch(0.03); // set the radius of the sphere for normal estimation ne.compute(*cloud_normals); // Visualize the point cloud and its normals pcl::visualization::PCLVisualizer viewer("Point Cloud Viewer"); viewer.setBackgroundColor(0.0, 0.0, 0.0); viewer.addPointCloud<pcl::PointXYZ>(cloud, "cloud"); viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud"); viewer.addPointCloudNormals<pcl::PointXYZ, pcl::Normal>(cloud, cloud_normals, 10, 0.05, "normals"); viewer.spin(); return 0; } ``` 在这个例子中,我们首先从文件中加载点云数据,然后使用 pcl::NormalEstimation 类估计点云的向量。接着,我们使用 pcl::visualization::PCLVisualizer 类将点云和向量可视化。在可视化中,我们使用 addPointCloud() 函数将点云添加到可视化窗口中,并使用 setPointCloudRenderingProperties() 函数设置点云的大小。然后,我们使用 addPointCloudNormals() 函数将向量添加到点云中,并设置向量的长度和大小。最后,我们使用 spin() 函数显示可视化窗口并等待用户交互。 注意,在使用 PCL 可视化库时,可能需要在编译时链接对应的库文件。例如,在 Ubuntu 系统中,可以使用以下命令编译上述代码: ``` g++ -o main main.cpp -I/usr/include/pcl-1.8 -lpcl_visualization -lboost_system ``` 其中 -I 指定了 PCL 库的头文件路径,-l 指定了需要链接的库文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值