当前位置:网站首页>PCL point cloud to depth image
PCL point cloud to depth image
2022-07-02 10:56:00 【AICVer】
Point cloud to depth image
#include <pcl/range_image/range_image.h> // Header file of depth image
int main(int argc, char** argv) {
pcl::PointCloud<pcl::PointXYZ> pointCloud; // Define the object of the point cloud
// Generate point cloud data circularly
for (float y = -0.5f; y <= 0.5f; y += 0.01f) {
for (float z = -0.5f; z <= 0.5f; z += 0.01f) {
pcl::PointXYZ point;
point.x = 2.0f - y;
point.y = y;
point.z = z;
pointCloud.points.push_back(point); // Add point data circularly to the point cloud object
}
}
pointCloud.width = (uint32_t)pointCloud.points.size();
pointCloud.height = 1; // Set the header information of the point cloud object
// Realize a rectangular point cloud
// We now want to create a range image from the above point cloud, with a 1deg angular resolution
//angular_resolution Is the angular resolution of the simulated depth sensor , That is, the angle corresponding to a pixel in the depth image
float angularResolution = (float)(1.0f * (M_PI / 180.0f)); // 1.0 degree in radians
//max_angle_width Is the maximum horizontal sampling angle of the analog depth sensor ,
float maxAngleWidth = (float)(360.0f * (M_PI / 180.0f)); // 360.0 degree in radians
//max_angle_height Is the maximum sampling angle in the vertical direction of the analog sensor All turn into radians
float maxAngleHeight = (float)(180.0f * (M_PI / 180.0f)); // 180.0 degree in radians
// The acquisition position of the sensor
Eigen::Affine3f sensorPose = (Eigen::Affine3f)Eigen::Translation3f(0.0f, 0.0f, 0.0f);
// The depth image follows the coordinate system
pcl::RangeImage::CoordinateFrame coordinate_frame = pcl::RangeImage::CAMERA_FRAME;
float noiseLevel = 0.00; //noise_level When getting depth image depth , The influence level of the nearest neighbor on the distance value of the query point
float minRange = 0.0f; //min_range Set the minimum acquisition distance , The position less than the minimum acquisition distance is the blind area of the sensor
int borderSize = 1; //border_size Get the width of the edge of the depth image
pcl::RangeImage rangeImage;
rangeImage.createFromPointCloud(pointCloud, angularResolution, maxAngleWidth, maxAngleHeight,
sensorPose, coordinate_frame, noiseLevel, minRange, borderSize);
std::cout << rangeImage << "\n";
}
Extract the boundary from the depth image
/* \author Bastian Steder */
#include <iostream>
#include <boost/thread/thread.hpp>
#include <pcl/range_image/range_image.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/range_image_visualizer.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/features/range_image_border_extractor.h>
#include <pcl/console/parse.h>
typedef pcl::PointXYZ PointType;
// --------------------
// ----- Parameters -----
// --------------------
float angular_resolution = 0.5f;
pcl::RangeImage::CoordinateFrame coordinate_frame = pcl::RangeImage::CAMERA_FRAME;
bool setUnseenToMaxRange = false;
// --------------
// ----- help -----
// --------------
void
printUsage (const char* progName)
{
std::cout << "\n\nUsage: "<<progName<<" [options] <scene.pcd>\n\n"
<< "Options:\n"
<< "-------------------------------------------\n"
<< "-r <float> angular resolution in degrees (default "<<angular_resolution<<")\n"
<< "-c <int> coordinate frame (default "<< (int)coordinate_frame<<")\n"
<< "-m Treat all unseen points to max range\n"
<< "-h this help\n"
<< "\n\n";
}
// --------------
// ----- The main function -----
// --------------
int
main (int argc, char** argv)
{
// --------------------------------------
// ----- Parsing command line arguments -----
// --------------------------------------
if (pcl::console::find_argument (argc, argv, "-h") >= 0)
{
printUsage (argv[0]);
return 0;
}
if (pcl::console::find_argument (argc, argv, "-m") >= 0)
{
setUnseenToMaxRange = true;
cout << "Setting unseen values in range image to maximum range readings.\n";
}
int tmp_coordinate_frame;
if (pcl::console::parse (argc, argv, "-c", tmp_coordinate_frame) >= 0)
{
coordinate_frame = pcl::RangeImage::CoordinateFrame (tmp_coordinate_frame);
cout << "Using coordinate frame "<< (int)coordinate_frame<<".\n";
}
if (pcl::console::parse (argc, argv, "-r", angular_resolution) >= 0)
cout << "Setting angular resolution to "<<angular_resolution<<"deg.\n";
angular_resolution = pcl::deg2rad (angular_resolution);
// ------------------------------------------------------------------
// ----- Read pcd file , If not given pcd File creates a sample point cloud -----
// ------------------------------------------------------------------
pcl::PointCloud<PointType>::Ptr point_cloud_ptr (new pcl::PointCloud<PointType>);
pcl::PointCloud<PointType>& point_cloud = *point_cloud_ptr;
pcl::PointCloud<pcl::PointWithViewpoint> far_ranges;
Eigen::Affine3f scene_sensor_pose (Eigen::Affine3f::Identity ());
std::vector<int> pcd_filename_indices = pcl::console::parse_file_extension_argument (argc, argv, "pcd");
if (!pcd_filename_indices.empty ())
{
std::string filename = argv[pcd_filename_indices[0]];
if (pcl::io::loadPCDFile (filename, point_cloud) == -1)
{
cout << "Was not able to open file \""<<filename<<"\".\n";
printUsage (argv[0]);
return 0;
}
scene_sensor_pose = Eigen::Affine3f (Eigen::Translation3f (point_cloud.sensor_origin_[0],point_cloud.sensor_origin_[1],point_cloud.sensor_origin_[2])) *Eigen::Affine3f (point_cloud.sensor_orientation_);
std::string far_ranges_filename = pcl::getFilenameWithoutExtension (filename)+"_far_ranges.pcd";
if (pcl::io::loadPCDFile(far_ranges_filename.c_str(), far_ranges) == -1)
std::cout << "Far ranges file \""<<far_ranges_filename<<"\" does not exists.\n";
}
else
{
cout << "\nNo *.pcd file given => Genarating example point cloud.\n\n";
for (float x=-0.5f; x<=0.5f; x+=0.01f)
{
for (float y=-0.5f; y<=0.5f; y+=0.01f)
{
PointType point; point.x = x; point.y = y; point.z = 2.0f - y;
point_cloud.points.push_back (point);
}
}
point_cloud.width = (int) point_cloud.points.size (); point_cloud.height = 1;
}
// -----------------------------------------------
// ----- Create a depth image from a point cloud -----
// -----------------------------------------------
float noise_level = 0.0;
float min_range = 0.0f;
int border_size = 1;
boost::shared_ptr<pcl::RangeImage> range_image_ptr (new pcl::RangeImage);
pcl::RangeImage& range_image = *range_image_ptr;
range_image.createFromPointCloud (point_cloud, angular_resolution, pcl::deg2rad (360.0f), pcl::deg2rad (180.0f), scene_sensor_pose, coordinate_frame, noise_level, min_range, border_size);
range_image.integrateFarRanges (far_ranges);
if (setUnseenToMaxRange)
range_image.setUnseenToMaxRange ();
// --------------------------------------------
// ----- Open the 3D browser and add a point cloud -----
// --------------------------------------------
pcl::visualization::PCLVisualizer viewer ("3D Viewer");
viewer.setBackgroundColor (1, 1, 1);
viewer.addCoordinateSystem (1.0f);
pcl::visualization::PointCloudColorHandlerCustom<PointType> point_cloud_color_handler (point_cloud_ptr, 0, 0, 0);
viewer.addPointCloud (point_cloud_ptr, point_cloud_color_handler, "original point cloud");
//PointCloudColorHandlerCustom<pcl::PointWithRange> range_image_color_handler (range_image_ptr, 150, 150, 150);
//viewer.addPointCloud (range_image_ptr, range_image_color_handler, "range image");
//viewer.setPointCloudRenderingProperties (PCL_VISUALIZER_POINT_SIZE, 2, "range image");
// -------------------------
// ----- Extract the boundary -----
// -------------------------
pcl::RangeImageBorderExtractor border_extractor (&range_image);
pcl::PointCloud<pcl::BorderDescription> border_descriptions;
border_extractor.compute (border_descriptions);
// ----------------------------------
// ----- Display point sets in the 3D browser -----
// ----------------------------------
pcl::PointCloud<pcl::PointWithRange>::Ptr border_points_ptr(new pcl::PointCloud<pcl::PointWithRange>), veil_points_ptr(new pcl::PointCloud<pcl::PointWithRange>), shadow_points_ptr(new pcl::PointCloud<pcl::PointWithRange>);
pcl::PointCloud<pcl::PointWithRange>& border_points = *border_points_ptr, & veil_points = * veil_points_ptr, & shadow_points = *shadow_points_ptr;
for (int y=0; y< (int)range_image.height; ++y)
{
for (int x=0; x< (int)range_image.width; ++x)
{
if (border_descriptions.points[y*range_image.width + x].traits[pcl::BORDER_TRAIT__OBSTACLE_BORDER])
border_points.points.push_back (range_image.points[y*range_image.width + x]);
if (border_descriptions.points[y*range_image.width + x].traits[pcl::BORDER_TRAIT__VEIL_POINT])
veil_points.points.push_back (range_image.points[y*range_image.width + x]);
if (border_descriptions.points[y*range_image.width + x].traits[pcl::BORDER_TRAIT__SHADOW_BORDER])
shadow_points.points.push_back (range_image.points[y*range_image.width + x]);
}
}
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointWithRange> border_points_color_handler (border_points_ptr, 0, 255, 0);
viewer.addPointCloud<pcl::PointWithRange> (border_points_ptr, border_points_color_handler, "border points");
viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 7, "border points");
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointWithRange> veil_points_color_handler (veil_points_ptr, 255, 0, 0);
viewer.addPointCloud<pcl::PointWithRange> (veil_points_ptr, veil_points_color_handler, "veil points");
viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 7, "veil points");
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointWithRange> shadow_points_color_handler (shadow_points_ptr, 0, 255, 255);
viewer.addPointCloud<pcl::PointWithRange> (shadow_points_ptr, shadow_points_color_handler, "shadow points");
viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 7, "shadow points");
//-------------------------------------
// ----- Show point sets in depth images -----
// ------------------------------------
pcl::visualization::RangeImageVisualizer* range_image_borders_widget = NULL;
range_image_borders_widget =
pcl::visualization::RangeImageVisualizer::getRangeImageBordersWidget (range_image, -std::numeric_limits<float>::infinity (), std::numeric_limits<float>::infinity (), false, border_descriptions, "Range image with borders" );
// -------------------------------------
//--------------------
// ----- Main circulation -----
//--------------------
while (!viewer.wasStopped ())
{
range_image_borders_widget->spinOnce ();
viewer.spinOnce ();
pcl_sleep(0.01);
}
}
Instance analysis
#include <pcl/range_image/range_image.h>
#include <pcl/range_image/range_image_planar.h>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/features/integral_image_normal.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/point_types.h>
#include <pcl/features/normal_3d.h>
#include <pcl/console/print.h>
#include <pcl/surface/organized_fast_mesh.h>
#include <pcl/console/time.h>
#include <Eigen/StdVector>
#include <Eigen/Geometry>
#include <iostream>
#include <pcl/surface/impl/organized_fast_mesh.hpp>
#include <boost/thread/thread.hpp>
#include <pcl/common/common_headers.h>
#include <pcl/visualization/range_image_visualizer.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/console/parse.h>
using namespace pcl::console;
int main(int argc, char** argv) {
// Generate the data
if (argc < 2)
{
print_error("Syntax is: %s input.pcd -w 640 -h 480 -cx 320 -cy 240 -fx 525 -fy 525 -type 0 -size 2\n", argv[0]);
print_info(" where options are:\n");
print_info(" -w X = width of detph iamge ");
return -1;
}
std::string filename = argv[1];
/************************************************************** Define the parameters required for converting from point cloud to depth image , And can accept the parameter settings of the user's command line . ***********************************************************/
int width = 640, height = 480, size = 2, type = 0;
float fx = 525, fy = 525, cx = 320, cy = 240;
parse_argument(argc, argv, "-w", width); // Depth image width
parse_argument(argc, argv, "-h", height);// Depth image height
parse_argument(argc, argv, "-cx", cx);// The position of the optical axis on the depth image x coordinate
parse_argument(argc, argv, "-cy", cy);// The position of the optical axis on the depth image y coordinate
parse_argument(argc, argv, "-fx", fx);// Horizontal focal length
parse_argument(argc, argv, "-fy", fy);// Vertical focal length
parse_argument(argc, argv, "-type", type);// The way of triangulation in surface reconstruction
parse_argument(argc, argv, "-size", size);// Patch size for surface reconstruction
/************************************************************** Load the original point cloud , Create at the same time RangeImagePlanar object , Use the function to change the object createFromPointCloudWithFixedSize() Proceed deep Degree image generation , The parameters here are in addition to those set by the user , It is also necessary to provide the pose of the camera , And the coordinate system that the imaging follows . ***********************************************************/
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::io::loadPCDFile(filename, *cloud);
print_info("Read pcd file successfully\n");
Eigen::Affine3f sensorPose;
sensorPose.setIdentity();
pcl::RangeImage::CoordinateFrame coordinate_frame = pcl::RangeImage::CAMERA_FRAME;
float noiseLevel = 0.00;// Simulate noise level during imaging
float minRange = 0.0f;// Consider the points beyond the threshold when imaging
pcl::RangeImagePlanar::Ptr rangeImage(new pcl::RangeImagePlanar);
rangeImage->createFromPointCloudWithFixedSize(*cloud, width, height, cx, cy, fx, fy, sensorPose, coordinate_frame);
std::cout << rangeImage << "\n";
//convert unorignized point cloud to orginized point cloud end
// Visual depth image
pcl::visualization::RangeImageVisualizer range_image_widget("range image");
range_image_widget.showRangeImage(*rangeImage);
range_image_widget.setWindowTitle("range image");
/************************************************************** After the generation of point cloud to depth image , We use this depth image as input , To use the simple triangulation class in the surface reconstruction module to generate the surface model . establish OrganizedFastMesh object , The input parameters of the algorithm are size, adopt setTrianglePixelSize( ) Function interface to change , Its control is very important The fineness of the surface . Another one Parameter is setTriangulationType( ) Method to set the type of triangulation , Is an enumeration variable , Contains triangles shape 、 Quadrilateral, etc . ***********************************************************/
//triangulation based on range image
pcl::OrganizedFastMesh<pcl::PointWithRange>::Ptr tri(new pcl::OrganizedFastMesh<pcl::PointWithRange>);
pcl::search::KdTree<pcl::PointWithRange>::Ptr tree(new pcl::search::KdTree<pcl::PointWithRange>);
tree->setInputCloud(rangeImage);
pcl::PolygonMesh triangles;
tri->setTrianglePixelSize(size);
tri->setInputCloud(rangeImage);
tri->setSearchMethod(tree);
tri->setTriangulationType((pcl::OrganizedFastMesh<pcl::PointWithRange>::TriangulationType)type);
tri->reconstruct(triangles);
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("range image"));
viewer->setBackgroundColor(0.5, 0.5, 0.5);
viewer->addPolygonMesh(triangles, "tin");
viewer->addCoordinateSystem();
while (!range_image_widget.wasStopped() && !viewer->wasStopped())
{
range_image_widget.spinOnce();
//pcl_sleep(0.01);
viewer->spinOnce();
}
}
边栏推荐
- JSP webshell免杀——JSP的基础
- Sus system availability scale
- Use WinDbg to statically analyze dump files (summary of practical experience)
- MySQL keyword
- UWA report uses tips. Did you get it? (the fourth bullet)
- 《MySQL 8 DBA基础教程》简介
- 华为游戏初始化init失败,返回错误码907135000
- Mongodb quickly get started with some simple operations of mongodb command line
- 使用sqlcipher打开加密的sqlite方法
- Kustomize user manual
猜你喜欢
随机推荐
Record attributeerror: 'nonetype' object has no attribute 'nextcall‘
SUS系统可用性量表
华为AppLinking中统一链接的创建和使用
1287_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析
LeetCode+ 76 - 80 暴搜专题
Flutter环境配置保姆级教程,让doctor一绿到底
js数组常用方法
Kustomize user manual
MySQL数据库远程访问权限设置
JSP webshell free -- webshell free
UVM - configuration mechanism
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
MongoDB-快速上手MongoDB命令行的一些简单操作
[SUCTF2018]followme
对话吴纲:我为什么笃信“大国品牌”的崛起?
02-taildir source
session-cookie与token
MySQL lethal serial question 3 -- are you familiar with MySQL locks?
VSCode工具使用
Solution of mysql8 forgetting password file in Windows Environment