bin文件夹下为生成的可执行文件generate_cloud,执行时和data文件放在同一文件夹下。
图像数据来自小觅相机。
src下的源码,包括generatePointCloud.cpp和CMakeLists.txt
// C++ 标准库
#include <iostream>
#include <string>
//#include <unistd.h>
using namespace std; // OpenCV 库
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
//#include <pcl/visualization/cloud_viewer.h> // PCL 库
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h> // 定义点云类型
typedef pcl::PointXYZRGBA PointT;
typedef pcl::PointCloud<PointT> PointCloud; //pcl::visualization::CloudViewer viewer("pcd viewer"); // 相机内参
const double camera_factor = ;
/*
const double camera_cx = 325.5;
const double camera_cy = 253.5; //nyuv2数据集:http://cs.nyu.edu/~silberman/datasets/
const double camera_fx = 518.0;
const double camera_fy = 519.0;
*/
const double camera_cx = 682.3;
const double camera_cy = 254.9;
const double camera_fx = 979.8; //小觅
const double camera_fy = 942.8; // 主函数
int main( int argc, char** argv )
{
// 读取./data/rgb.png和./data/depth.png,并转化为点云 // 图像矩阵
cv::Mat rgb, depth;
// 使用cv::imread()来读取图像
// API: http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#cv2.imread
rgb = cv::imread( "./data/rgb.png" );
// rgb 图像是8UC3的彩色图像
// depth 是16UC1的单通道图像,注意flags设置-1,表示读取原始数据不做任何修改
depth = cv::imread( "./data/depth.png", - ); // 点云变量
// 使用智能指针,创建一个空点云。这种指针用完会自动释放。
PointCloud::Ptr cloud ( new PointCloud );
// 遍历深度图
for (int m = ; m < depth.rows; m++)
for (int n=; n < depth.cols; n++)
{
// 获取深度图中(m,n)处的值
ushort d = depth.ptr<ushort>(m)[n];
// d 可能没有值,若如此,跳过此点
if (d == || d >= )
continue; //获得一个点的位置与颜色
// d 存在值,则向点云增加一个点
PointT p;
// 计算这个点的空间坐标
p.z = double(d) / camera_factor;
p.x = (n - camera_cx) * p.z / camera_fx;
p.y = (m - camera_cy) * p.z / camera_fy;
// 从rgb图像中获取它的颜色
// rgb是三通道的BGR格式图,所以按下面的顺序获取颜色
p.b = rgb.ptr<uchar>(m)[n*];
p.g = rgb.ptr<uchar>(m)[n*+];
p.r = rgb.ptr<uchar>(m)[n*+]; // 把p加入到点云中
cloud->points.push_back( p );
}
/*
// viewer.showCloud(cloud);
// sleep(100);////#include <unistd.h>
// return 0;
*/
// 设置并保存点云
cloud->height = ;
cloud->width = cloud->points.size();
cout<<"point cloud size = "<<cloud->points.size()<<endl;
cloud->is_dense = false;
pcl::io::savePCDFile( "./pointcloud.pcd", *cloud );
// 清除数据并退出
cloud->points.clear();
cout<<"Point cloud saved."<<endl;
return ;
}
CMakeLists.txt
# 增加PCL库的依赖
FIND_PACKAGE( PCL REQUIRED ) list(REMOVE_ITEM PCL_LIBRARIES "vtkproj4") # use this in Ubuntu 16.04
# 增加opencv的依赖
FIND_PACKAGE( OpenCV REQUIRED ) # 添加头文件和库文件
ADD_DEFINITIONS( ${PCL_DEFINITIONS} )
INCLUDE_DIRECTORIES( ${PCL_INCLUDE_DIRS} )
LINK_LIBRARIES( ${PCL_LIBRARY_DIRS} ) ADD_EXECUTABLE( generate_cloud generatePointCloud.cpp )
TARGET_LINK_LIBRARIES( generate_cloud ${OpenCV_LIBS}
${PCL_LIBRARIES} )
和src文件夹在同一文件夹下的CMakeLists.txt
CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )
PROJECT( slam ) SET(CMAKE_CXX_COMPILER "g++")
SET( CMAKE_BUILD_TYPE Debug )
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib) INCLUDE_DIRECTORIES( ${PROJECT_SOURCE_DIR}/include )
LINK_DIRECTORIES( ${PROJECT_SOURCE_DIR}/lib) ADD_SUBDIRECTORY( ${PROJECT_SOURCE_DIR}/src )
编译后可执行文件在bin中。