使用pcl绘制空间点云

1.前言

临近毕业,估计老板看我工作量不够,特地又加点工作也是醉了。以前使用matlab写过三维点重构的程序,然后使用plot函数可以非常方便的显示出点云效果,但是现在老板要求要用C++写,于是就蛋疼了

2. 基本思路

三维重构部分使用opencv的Mat可以非常方便的计算,但是三维点的显示工作就有些费劲了,于是我们想到了pcl, pcl是一种点云处理的开源库,相关配置过程 可以参考: http://blog.csdn.net/zhyh1435589631/article/details/54584058

3. 实现代码

显示效果如下:
这里写图片描述
配置完成之后,编写如下代码即可显示出我们的点云数据了, 上面展示的数据是我们重构的一个平面靶标上的数据点

#include <pcl/visualization/cloud_viewer.h>  
#include <iostream>  
#include <pcl/io/io.h>  
#include <pcl/io/pcd_io.h>  
#include <fstream>
#include <string>

using namespace std;


int main()
{
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    //pcl::io::loadPCDFile(R"(C:\Users\Administrator\Desktop\rabbit_gra.pcd)", *cloud);

    ifstream ifs("W3D_f.txt");
    string line;
    int count = 0;
    while (getline(ifs, line)){
        count++;
    }
    ifs.close();

    cloud->width = 1;
    cloud->height = count;
    cloud->points.resize(cloud->width * cloud->height);
    ifs.open("W3D_f.txt");
    double code;
    for (int i = 0; i < cloud->points.size(); i++){     
        ifs >> cloud->points[i].x >> cloud->points[i].y >> cloud->points[i].z >> code;
    }
    ifs.close();

    pcl::visualization::CloudViewer viewer("Cloud Viewer");

    //blocks until the cloud is actually rendered  
    viewer.showCloud(cloud);

    while (!viewer.wasStopped())    {
        //you can also do cool processing here  
        //FIXME: Note that this is running in a separate thread from viewerPsycho  
        //and you should guard against race conditions yourself... 

    }
    return 0;
}

其中, W3D_f.txt文件内容如下:

             4.79378                 112.682                 800.851                       1    
             2.34964                 85.7016                 813.907                       2    
          -0.0686548                 58.7965                 826.984                       3    
            -2.08487                  31.868                 840.269                       4    
            -4.76845                 4.84557                 853.482                       5    
            -7.23033                 -22.188                  866.84                       6    
            -9.75721                -49.4221                 880.426                       7    
             34.0102                 113.086                 808.633                       8    
             31.9597                 86.1826                 821.716                       9    
             30.0499                 58.9642                 835.079                      10    
             27.3361                 30.9711                 848.695                      11    
             25.1392                 4.09195                 861.985                      12    
             22.7087                -22.8296                 875.322                      13    
             20.1849                -50.0868                 888.908                      14    
             63.8499                 114.626                 815.851                      15    
             61.6308                 87.2765                   829.1                      16    
             59.2992                 59.5761                 842.595                      17    
             57.0298                 31.9039                 856.156                      18    
             54.2401                 4.01155                 869.774                      19    
             51.5545                -22.8885                 883.014                      20    
             48.6675                -49.7647                 896.333                      21    
             46.1166                -77.0323                 910.016                      22    
             93.1154                 116.935                 822.467                      23    
             91.7324                 89.5031                 835.937                      24    
             89.9262                 62.2198                 849.325                      25    
             81.1688                -24.5759                 891.683                      26    
             77.0333                -51.4245                 904.711                      27    
              73.961                -77.8344                 917.825                      28    
             103.513                -78.7856                 926.212                      29    
             107.145                -51.4038                 912.678                      30    
             119.425                 63.2437                 856.619                      31    
             120.568                 90.2559                 843.224                      32    
             123.164                 118.046                 829.891                      33    
               109.5                -24.8608                 899.354                      34    
             152.173                 118.578                 837.237                      35    
             150.652                 91.4819                 850.494                      36    
             148.921                 64.4987                 863.724                      37    
             149.255                 9.48509                 892.076                      38    
             151.254                 37.0656                 878.353                      39    
             145.678                -23.6144                 908.361                      40    
             136.653                -50.9064                 920.364                      41    
             133.241                -77.6465                 933.635                      42    
             186.572                  119.28                 845.679                      43    
             185.158                 92.4509                 858.825                      44    
             183.057                 65.2305                 872.137                      45    
              180.81                 38.2439                 885.384                      46    
              178.94                 11.2468                 898.914                      47    
              176.74                -15.8386                 912.457                      48    
             174.612                -43.1031                 926.344                      49    
             173.103                -70.0914                 940.331                      50    
             208.163                 12.5531                 905.712                      51    
             206.327                -14.3002                 919.308                      52    
             204.391                -42.0086                 933.503                      53    
             202.091                -68.3246                 947.055                      54    
             84.4952                 17.6834                  870.68                      55    
             121.221                 20.3434                 879.038                      56    

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值