5.4 实践:拼接点云

一、安装PCL库

sudo add-apt-repository ppa:v-launchpad-jochen-sprickerhof-de/pcl //加入源
按enter建
sudo apt-get update //更新,这一步可能报错,原因是国内源
————————————————————————————————
使用国内源按教材命令容易报错,解决见:

http://www.cnblogs.com/fudong071234/p/6359725.html

编译安装见:

http://www.linuxdiyf.com/linux/24123.html
————————————————————————————————

************************************分割线***************************************

——————————————————————————————
教材给出指令:
sudo add-apt-repository ppa:v-launchpad-jochen-sprickerhof-de/pcl

sudo apt-get update

sudo apt-get install libpcl-all

——————————————————————————————

其中最后一条指令可能报错,执行下列指令:

sudo add-apt-repository ppa:v-launchpad-jochen-sprickerhof-de/pcl

sudo apt-get update

sudo apt-get install libpcl1.7

二、程序测试
代码注释可以参考这个人的博客:
http://www.mamicode.com/info-detail-2180344.html
—————————————分割线—————————————
1.预备知识

首先是vector的用法:
http://www.cnblogs.com/wang7/archive/2012/04/27/2474138.html
——————————————————————————————
Eigen库中各种形式的表示如下:
旋转矩阵(3X3):Eigen::Matrix3d
旋转向量(3X1):Eigen::AngleAxisd
四元数(4X1):Eigen::Quaterniond
平移向量(3X1):Eigen::Vector3d
变换矩阵(4X4):Eigen::Isometry3d
——————————————————————————————
push_back 方法介绍:
vector::void push_back (const value_type& val);
vector::void push_back (value_type&& val);
该函数将一个新的元素加到vector的最后面,位置为当前最后一个元素的下一个元素,新的元素的值是val的拷贝(或者是移动拷贝)
——————————————————————————————
cv::imread( , )第二个参数含义:
https://blog.csdn.net/z914022466/article/details/52709981
——————————————————————————————
if(!a) //如果a=0,!a=1,继续操作
——————————————————————————————
for(auto& x:y):

//for循环中的每个迭代初始化一个新的引用
for (auto &c : s)   
    c = toupper(c); 
//相当于
for (auto it = s.begin(); it != s.end(); ++it)
{
    auto &c = *it;
    c = toupper(c);
}

—————————————分割线—————————————
2.代码及注释:

#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/visualization/pcl_visualizer.h>

int main( int argc, char** argv )
{
vector<cv::Mat> colorImgs, depthImgs;    // 彩色图和深度图
vector<Eigen::Isometry3d, Eigen::aligned_allocator<Eigen::Isometry3d>> poses;         // 相机位姿

ifstream fin("./pose.txt");
//cout<<"fin="<<fin<<endl;
if (!fin) //如果为真继续操作,fin未读到东西则继续操作
{
    cerr<<"请在有pose.txt的目录下运行此程序"<<endl;
    return 1;
}

for ( int i=0; i<5; i++ )
{
    boost::format fmt( "./%s/%d.%s" ); //图像文件格式不太懂
    colorImgs.push_back( cv::imread( (fmt%"color"%(i+1)%"png").str() ));
    depthImgs.push_back( cv::imread( (fmt%"depth"%(i+1)%"pgm").str(), -1 )); // 使用-1读取原始图像
    
    double data[7] = {0};
//cout<<"data="<<data[7]<<endl;
    for ( auto& d:data )
        fin>>d;                      //fin给data[]
    Eigen::Quaterniond q( data[6], data[3], data[4], data[5] );
    Eigen::Isometry3d T(q);          //4*4
    T.pretranslate( Eigen::Vector3d( data[0], data[1], data[2] ));
    poses.push_back( T );
//cout<<"pose="<<q<<endl;   //讲pose文件里的东西读入data数组中
}

// 计算点云并拼接
// 相机内参 
double cx = 325.5;
double cy = 253.5;
double fx = 518.0;
double fy = 519.0;
double depthScale = 1000.0;

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

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

// 新建一个点云
PointCloud::Ptr pointCloud( new PointCloud ); 
for ( int i=0; i<5; i++ )
{
    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;    //为什么要除以depthscale
            point[0] = (u-cx)*point[2]/fx;
            point[1] = (v-cy)*point[2]/fy; 
            Eigen::Vector3d pointWorld = T*point;   //点在像素坐标系的坐标
            
            PointT p ;
            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 ];
            pointCloud->points.push_back( p );
        }
}

pointCloud->is_dense = false;
cout<<"点云共有"<<pointCloud->size()<<"个点."<<endl;
pcl::io::savePCDFileBinary("map.pcd", *pointCloud );
return 0;
}

————————————————————————————————
会生成地图文件map.pcd
进入存储文件的目录后可以用如下指令读取:
pcl_viewer map.pcd
若需要安装软件,指令如下:
sudo apt install pcl-tools

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本文背景:在新时代的号召下,我们的图像分析和处理工作从2D发展到了3D,从Opencv的熟练使用进化到了PCL的探...
    宏阳HONGYANG阅读 7,949评论 9 4
  • 想到哪里说到哪里,或者再次遇到了就更新一下。 软件基本都是必备(尤其笔记本及移动硬盘安装系统的话) 电池管理软件由...
    乾荒姬阅读 2,940评论 1 13
  • 学弟好不容易进了一家心仪的企业,工作了两个月,被老板警告说,达不要求就很可能过不了试用期, 学弟过来跟我请教,问:...
    亦亦大阅读 617评论 3 3
  • 冬天的太阳总是奢侈的 近几日成都的天气简直好的不像话 真的好(想)难(去)得(玩)! 真想晒晒太阳,调节心情,感受...
    美邻共享阅读 394评论 0 0
  • 道理都懂,但是仍旧过不好这一生 一种常见的一类人群,他们自己自视甚高,在旁人看来眼高手低。谈起来,他们自己也感叹,...
    Yueboy阅读 179评论 0 0

友情链接更多精彩内容