PCL
PCD FILE文件格式概览
# unorganized
[In]
head table_scene_lms400.pcd
[Out]
# .PCD v0.7 - Point Cloud Data file format
VERSION 0.7
FIELDS x y z intensity distance sid
SIZE 4 4 4 4 4 4
TYPE F F F F F F
COUNT 1 1 1 1 1 1
WIDTH 460400
HEIGHT 1
VIEWPOINT 0 0 0 1 0 0 0 # sensor_orientation
POINTS 460400
# organized: cloud->isOrganized()
[In]
head table_scene_mug_stereo_textured.pcd
[Out]
# .PCD v0.7 - Point Cloud Data file format
VERSION 0.7
FIELDS x y z rgba
SIZE 4 4 4 4
TYPE F F F U
COUNT 1 1 1 1
WIDTH 640
HEIGHT 480
VIEWPOINT 0 0 0 0 1 0 0 # sensor_orientation
POINTS 307200
pcl cpp
CMakeLists.txt
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
project(pcl_visualizer_viewports)
find_package(PCL 1.2 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable (pcl_visualizer_demo pcl_visualizer_demo.cpp)
target_link_libraries (pcl_visualizer_demo ${PCL_LIBRARIES})
设置sensor_orientation
point_cloud_ptr->sensor_orientation_=Eigen::Quaternionf(1, 0, 0, 0);
Check fields
// fields: "rgb" "rgba"
template <typename T>
bool checkRGBFields(const std::string& fields)
{
std::vector<pcl::PCLPointField> fields_;
bool capable_=false;
int field_idx_ = pcl::getFieldIndex<T> (fields, fields_);
if (field_idx_ != -1)
{
capable_ = true;
}
return capable_;
}
//using
bool capable=M_UTILITY::checkRGBFields<pcl::PointXYZ>("rgb");
std::cout<<capable<<'\n';
capable=M_UTILITY::checkRGBFields<pcl::PointXYZRGB>("rgb");
std::cout<<capable<<'\n';
# output
0
1
viewer for PointXYZ and PointXYZRGB
template <typename T>
typename pcl::visualization::PCLVisualizer::Ptr simpleViewer(typename pcl::PointCloud<T>::ConstPtr cloud,
pcl::PointCloud<pcl::Normal>::ConstPtr normals=nullptr)
{
pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
viewer->setBackgroundColor (0, 0, 0);
bool capable_=false;
capable_= checkRGBFields<T>("rgb") || checkRGBFields<T>("rgba");
if(!capable_) // no rgb fields
{
viewer->addPointCloud<T> (cloud, "sample cloud");
}
else
{
pcl::visualization::PointCloudColorHandlerRGBField<T> rgb(cloud);
viewer->addPointCloud<T> (cloud, rgb, "sample cloud");
}
viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");
if(normals)
viewer->addPointCloudNormals<T, pcl::Normal> (cloud, normals, 10, 0.05, "normals");
viewer->addCoordinateSystem (1.0);
viewer->initCameraParameters ();
return (viewer);
}
// using: viewer cloud with rgb or without rgba
std::string filename="table_scene_mug_stereo_textured.pcd";
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGBA>);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud1(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PCDReader reader;
reader.read(filename, *cloud);
reader.read(filename, *cloud1);
pcl::visualization::PCLVisualizer::Ptr viewer=M_UTILITY::simpleViewer<pcl::PointXYZRGBA>(cloud);
viewer->spin();
pcl::visualization::PCLVisualizer::Ptr viewer2=M_UTILITY::simpleViewer<pcl::PointXYZ>(cloud1);
viewer2->spin();
// using: viewer cloud and normals
#include "viewer.h"
#include <pcl/io/pcd_io.h>
#include <pcl/features/integral_image_normal.h>
int main ()
{
// load point cloud
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGBA>);
pcl::io::loadPCDFile ("table_scene_mug_stereo_textured.pcd", *cloud);
// estimate normals
pcl::PointCloud<pcl::Normal>::Ptr normals (new pcl::PointCloud<pcl::Normal>);
pcl::IntegralImageNormalEstimation<pcl::PointXYZRGBA, pcl::Normal> ne;
ne.setNormalEstimationMethod (ne.AVERAGE_3D_GRADIENT);
ne.setMaxDepthChangeFactor(0.02f);
ne.setNormalSmoothingSize(10.0f);
ne.setInputCloud(cloud);
ne.compute(*normals);
// visualize normals
pcl::visualization::PCLVisualizer::Ptr viewer=M_UTILITY::simpleViewer<pcl::PointXYZRGBA>(cloud, normals);
viewer->spin();
return 0;
}
IO
#include <pcl/io/pcd_io.h>
#include <pcl/io/io.h>
#include <pcl/point_types.h>
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
pcl::PCDReader reader;
reader.read<pcl::PointXYZ> (filename, *cloud);
// pcl::io::loadPCDFile (filename, *cloud);
pcl::PCDWriter writer;
writer.write<pcl::PointXYZ> ("table_scene_lms400_inliers.pcd", *cloud_filtered, false); // binary = false
// pcl::io::savePCDFileASCII (filename, *cloud);
Transform
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_cloud.h>
#include <pcl/common/transforms.h>
#include <pcl/visualization/pcl_visualizer.h>
int main()
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr transformed_cloud(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile("1.pcd", *cloud) < 0)
{
PCL_ERROR("目标文件不存在!\n");
return -1;
}
/// 方式1:Matrix4f
// 创建矩阵对象transform_1,初始化为4×4单位阵
Eigen::Matrix4f transform_1 = Eigen::Matrix4f::Identity();
// 定义旋转矩阵,绕z轴
float theta = M_PI / 4; // 旋转弧度
transform_1(0, 0) = cos(theta);
transform_1(0, 1) = -sin(theta);
transform_1(1, 0) = sin(theta);
transform_1(1, 1) = cos(theta);
// 定义在x轴上的平移,2.5m
transform_1(0, 3) = 2.5;
// 打印平移、旋转矩阵
std::cout << "方式1: 使用Matrix4f\n";
std::cout << transform_1 << std::endl;
/// 方式2:Affine3f
// 创建矩阵对象transform_2.matrix(),初始化为4×4单位阵
Eigen::Affine3f transform_2 = Eigen::Affine3f::Identity();
// 定义在x轴上的平移,2.5m
transform_2.translation() << 2.5, 0.0, 0.0; // 三个数分别对应X轴、Y轴、Z轴方向上的平移
// 定义旋转矩阵,绕z轴
transform_2.rotate(Eigen::AngleAxisf(theta, Eigen::Vector3f::UnitZ())); //同理,UnitX(),绕X轴;UnitY(),绕Y轴.
// 打印平移、旋转矩阵
std::cout << "\n方式2: 使用Affine3f\n";
std::cout << transform_2.matrix() << std::endl; //注意:不是transform_2
/// 执行转换
// transform_1 或者 transform_2 都可以实现相同的转换
pcl::transformPointCloud(*cloud, *transformed_cloud, transform_2); //注意:不是transform_2.matrix()
// Visualization可视化
std::cout << "点云颜色:\n";
std::cout << "原始点云:白色\n" << "平移、旋转后点云:红色\n";
pcl::visualization::PCLVisualizer viewer("矩阵转换实例");
/// 定义点云RGB颜色
// 原始点云附色
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_color(cloud, 255, 255, 255);
viewer.addPointCloud(cloud, cloud_color, "original_cloud");
// 变换后点云附色
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> transformed_cloud_color(transformed_cloud, 250, 0, 0);
viewer.addPointCloud(transformed_cloud, transformed_cloud_color, "transformed_cloud");
viewer.addCoordinateSystem(1.0, "cloud", 0); //定义坐标系
viewer.setBackgroundColor(0.05, 0.05, 0.05, 0); // 设置背景颜色
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "original_cloud");
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "transformed_cloud");
//viewer.setPosition(800, 400); // 设置可视化窗口位置
while (!viewer.wasStopped())
{ // Display the visualiser until 'q' key is pressed
viewer.spinOnce();
}
return 0;
}
Filter
Extract the point cloud outside/inside the specified index
template <typename T>
void filterFromIndices(typename pcl::PointCloud<T>::Ptr cloud, pcl::PointIndices::ConstPtr indices)
{
pcl::ExtractIndices<T> extract;
extract.setInputCloud(cloud);
extract.setIndices(indices);
// set true to extract the point cloud outside the specified index
// set false to extract the point cloud inside the specified index
extract.setNegative(false);
extract.filter(*cloud);
}
// using
cloud_camera_->sampleAbovePlane();
std::vector<int> indices = cloud_camera_->getSampleIndices();
pcl::PointIndices::Ptr indices1(new pcl::PointIndices);
indices1->indices=indices;
M_UTILITY::filterFromIndices<pcl::PointXYZRGBA>(cloud_camera_->getCloudProcessed(), indices1);
Remove NAN
template <typename T>
void removeNans(typename pcl::PointCloud<T>::Ptr cloud_processed_)
{
pcl::PointIndices::Ptr inliers(new pcl::PointIndices());
pcl::removeNaNFromPointCloud(*cloud_processed_, *cloud_processed_, inliers->indices);
}
Removing outliers using a StatisticalOutlierRemoval
filter
#include <pcl/filters/statistical_outlier_removal.h>
// Create the filtering object
/*
The number of neighbors to analyze for each point is set to 50, and the standard deviation multiplier to 1. What this means is that all points who have a distance larger than 1 standard deviation of the mean distance to the query point will be marked as outliers and removed.
*/
pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;
sor.setInputCloud (cloud);
sor.setMeanK (50);
sor.setStddevMulThresh (1.0);
sor.filter (*cloud_filtered);
// utility.h
namespace M_UTILITY{
pcl::visualization::PCLVisualizer::Ptr simpleVis (pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud);
template <typename T>
typename pcl::PointCloud<T>::Ptr filter(typename pcl::PointCloud<T>::Ptr cloud)
{
pcl::StatisticalOutlierRemoval<T> sor;
sor.setInputCloud (cloud);
sor.setMeanK (50);
sor.setStddevMulThresh (1.0);
typename pcl::PointCloud<T>::Ptr cloud_filtered(new pcl::PointCloud<T>);
sor.filter (*cloud_filtered);
return cloud_filtered;
}
}
// utility.cpp
namespace M_UTILITY{
pcl::visualization::PCLVisualizer::Ptr simpleVis (pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud)
{
// --------------------------------------------
// -----Open 3D viewer and add point cloud-----
// --------------------------------------------
pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
viewer->setBackgroundColor (0, 0, 0);
viewer->addPointCloud<pcl::PointXYZ> (cloud, "sample cloud");
viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");
viewer->addCoordinateSystem (1.0);
viewer->initCameraParameters ();
return (viewer);
}
}
// using test pcl.cpp
std::string filename="table_scene_lms400.pcd";
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
pcl::PCDReader reader;
reader.read<pcl::PointXYZ> (filename, *cloud);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud1=M_UTILITY::filter<pcl::PointXYZ>(cloud);
pcl::visualization::PCLVisualizer::Ptr viewer = M_UTILITY::simpleVis(cloud);
viewer->spin();
pcl::visualization::PCLVisualizer::Ptr viewer1 = M_UTILITY::simpleVis(cloud1);
viewer1->spin();
// using CMakeLists.txt
add_library(${PROJECT_NAME}_viewer src/viewer.cpp)
target_link_libraries(${PROJECT_NAME}_viewer
${PCL_LIBRARIES})
add_executable(testpcl src/testpcl.cpp)
target_link_libraries(testpcl ${PCL_LIBRARIES} ${PROJECT_NAME}_viewer)
[ 98%] Linking CXX shared library /home/zhao/catkin_ws/devel/lib/libgpd_ros_viewer.so
[ 98%] Built target gpd_ros_viewer
Scanning dependencies of target testpcl
[ 98%] Building CXX object gpd_ros/CMakeFiles/testpcl.dir/src/testpcl.cpp.o
[100%] Linking CXX executable /home/zhao/catkin_ws/devel/lib/gpd_ros/testpcl
[100%] Built target testpcl
Calculate surface normals
Linking CXX shared library /home/zhao/catkin_ws/devel/lib/libgpd_ros_viewer.so#include <pcl/features/normal_3d.h>
// ----------------------------------------------------------------
// -----Calculate surface normals with a search radius of 0.05-----
// ----------------------------------------------------------------
pcl::NormalEstimation<pcl::PointXYZRGB, pcl::Normal> ne;
ne.setInputCloud (point_cloud_ptr);
pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZRGB> ());
ne.setSearchMethod (tree);
pcl::PointCloud<pcl::Normal>::Ptr cloud_normals1 (new pcl::PointCloud<pcl::Normal>);
ne.setRadiusSearch (0.05);
ne.compute (*cloud_normals1);
Normal Estimation Using Integral Images
#include <pcl/visualization/cloud_viewer.h>
#include <iostream>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/features/integral_image_normal.h>
int
main ()
{
// load point cloud
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
pcl::io::loadPCDFile ("table_scene_mug_stereo_textured.pcd", *cloud);
// estimate normals
pcl::PointCloud<pcl::Normal>::Ptr normals (new pcl::PointCloud<pcl::Normal>);
pcl::IntegralImageNormalEstimation<pcl::PointXYZ, pcl::Normal> ne;
ne.setNormalEstimationMethod (ne.AVERAGE_3D_GRADIENT);
ne.setMaxDepthChangeFactor(0.02f);
ne.setNormalSmoothingSize(10.0f);
ne.setInputCloud(cloud);
ne.compute(*normals);
// visualize normals
pcl::visualization::PCLVisualizer viewer("PCL Viewer");
viewer.setBackgroundColor (0.0, 0.0, 0.5);
viewer.addPointCloudNormals<pcl::PointXYZ,pcl::Normal>(cloud, normals);
while (!viewer.wasStopped ())
{
viewer.spinOnce ();
}
return 0;
}
visualizer
#include <pcl/visualization/pcl_visualizer.h>
pcl::visualization::PCLVisualizer::Ptr simpleVis (pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud)
{
// --------------------------------------------
// -----Open 3D viewer and add point cloud-----
// --------------------------------------------
pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
viewer->setBackgroundColor (0, 0, 0);
viewer->addPointCloud<pcl::PointXYZ> (cloud, "sample cloud");
viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");
viewer->addCoordinateSystem (1.0);
viewer->initCameraParameters ();
return (viewer);
}
pcl::visualization::PCLVisualizer::Ptr rgbVis (pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud)
{
pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
viewer->setBackgroundColor (0, 0, 0);
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud);
viewer->addPointCloud<pcl::PointXYZRGB> (cloud, rgb, "sample cloud");
viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
viewer->addCoordinateSystem (1.0);
viewer->initCameraParameters ();
return (viewer);
}
pcl::visualization::PCLVisualizer::Ptr customColourVis (pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud)
{
pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
viewer->setBackgroundColor (0, 0, 0);
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(cloud, 0, 255, 0);
viewer->addPointCloud<pcl::PointXYZ> (cloud, single_color, "sample cloud");
viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
viewer->addCoordinateSystem (1.0);
viewer->initCameraParameters ();
return (viewer);
}
pcl::visualization::PCLVisualizer::Ptr normalsVis (
pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud, pcl::PointCloud<pcl::Normal>::ConstPtr normals)
{
// --------------------------------------------------------
// -----Open 3D viewer and add point cloud and normals-----
// --------------------------------------------------------
pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
viewer->setBackgroundColor (0, 0, 0);
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud);
viewer->addPointCloud<pcl::PointXYZRGB> (cloud, rgb, "sample cloud");
viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
viewer->addPointCloudNormals<pcl::PointXYZRGB, pcl::Normal> (cloud, normals, 10, 0.05, "normals");
viewer->addCoordinateSystem (1.0);
viewer->initCameraParameters ();
return (viewer);
}
Other
Acquire the max/min point
#include <pcl/common/common.h>
pcl::PointXYZRGBA min_pt_pcl, max_pt_pcl;
pcl::getMinMax3D(*cloud_camera_->getCloudProcessed(), min_pt_pcl, max_pt_pcl);
const Eigen::Vector3f min_pt = min_pt_pcl.getVector3fMap();
const Eigen::Vector3f max_pt = max_pt_pcl.getVector3fMap();
ros PointCloud2 PointCloud
Convert PointCloud2 to PointCloud
#include <ros/ros.h>
#include <tf/transform_listener.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/io.h>
#include <pcl/point_types.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_types.h>
#include <pcl/PCLPointCloud2.h>
#include <pcl/conversions.h>
void cloud_cb(const boost::shared_ptr<const sensor_msgs::PointCloud2>& input){
pcl::PCLPointCloud2 pcl_pc2;
pcl_conversions::toPCL(*input,pcl_pc2);
pcl::PointCloud<pcl::PointXYZ>::Ptr temp_cloud(new pcl::PointCloud<pcl::PointXYZ>);
// or
// pcl::PointCloud<pcl::PointXYZRGBA>::Ptr temp_cloud(new pcl::PointCloud<pcl::PointXYZRGBA>);
pcl::fromPCLPointCloud2(pcl_pc2,*temp_cloud);
//do stuff with temp_cloud here
static bool bsave=true;
if(bsave)
{
bsave=false;
pcl::PCDWriter writer;
writer.write<pcl::PointXYZ> ("/home/zhao/table_scene.pcd", *temp_cloud, false); // binary = false
// writer.write<pcl::PointXYZRGBA> ("/home/zhao/table_scene_rgba.pcd", *temp_cloud, false);
// pcl::io::savePCDFileASCII (filename, *cloud);
ROS_INFO("Done");
}
}
int main(int argc, char** argv){
ros::init(argc, argv, "pc2_listener");
ros::NodeHandle node;
ros::Subscriber sub_handler=node.subscribe("/kinect_V2/depth_registered/points", 10, cloud_cb);
ROS_INFO("wait for msg...");
ros::spin();
return 0;
};
pcl python
fps using python pcl
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.utils.data
from torch.autograd import Variable
import numpy as np
import torch.nn.functional as F
import pcl
import pcl.pcl_visualization
def index_points(points, idx):
"""
Input:
points: input points data, [B, N, C]
idx: sample index data, [B, S]
Return:
new_points:, indexed points data, [B, S, C]
"""
device = points.device
B = points.shape[0]
view_shape = list(idx.shape)
view_shape[1:] = [1] * (len(view_shape) - 1)
repeat_shape = list(idx.shape)
repeat_shape[0] = 1
batch_indices = torch.arange(B, dtype=torch.long).to(device).view(view_shape).repeat(repeat_shape)
new_points = points[batch_indices, idx, :]
return new_points
def farthest_point_sample(xyz, npoint):
"""
Input:
xyz: pointcloud data, [B, N, 3]
npoint: number of samples
Return:
centroids: sampled pointcloud index, [B, npoint]
"""
device = xyz.device
B, N, C = xyz.shape
centroids = torch.zeros(B, npoint, dtype=torch.long).to(device)
distance = torch.ones(B, N).to(device) * 1e10
farthest = torch.randint(0, N, (B,), dtype=torch.long).to(device)
batch_indices = torch.arange(B, dtype=torch.long).to(device)
for i in range(npoint):
centroids[:, i] = farthest
centroid = xyz[batch_indices, farthest, :].view(B, 1, 3)
dist = torch.sum((xyz - centroid) ** 2, -1)
mask = dist < distance
distance[mask] = dist[mask]
farthest = torch.max(distance, -1)[1]
return centroids
def main():
cloud = pcl.load("/home/zhao/processed.pcd")
np_points = cloud.to_array()
t_points = torch.from_numpy(np_points)
t_points = t_points.repeat(1, 1, 1)
num_sample = 1000
idx = farthest_point_sample(t_points, num_sample)
sample_pts = index_points(t_points, idx)
sample_pts = sample_pts.squeeze(0)
np_sample_pts = sample_pts.numpy()
pc_sample = pcl.PointCloud(np_sample_pts)
viewer = pcl.pcl_visualization.PCLVisualizering()
viewer.AddPointCloud(pc_sample, b'scene_cloud', 0)
viewer.AddCoordinateSystem(0.1)
# viewer.RemovePointCloud(b'scene_cloud', 0)
while not viewer.WasStopped():
viewer.Spin()
if __name__ == '__main__':
main()
![](https://i-blog.csdnimg.cn/blog_migrate/98cac85397f8206f733816fa090bca7c.png)
![](https://i-blog.csdnimg.cn/blog_migrate/18503c90cb1ba41c2a3567435a86d9c8.png)
sample_and_group in pointnet2
def main():
begin_time = time.time()
cloud = pcl.load("/home/zhao/pcd_filepath/bunny.pcd")
np_points = cloud.to_array()
# np_points = pc_normalize(np_points)
t_points = torch.from_numpy(np_points)
t_points = t_points.repeat(1, 1, 1)
num_sample = 1000
new_xyz, new_points = sample_and_group(num_sample, 0.1, 40, t_points, None)
new_points = new_points.squeeze(0)
new_xyz = new_xyz.squeeze(0)
xyz_sample = pcl.PointCloud(new_xyz.numpy())
print(cloud.size, xyz_sample.size)
viewer = pcl.pcl_visualization.PCLVisualizering()
color = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0)]
color_w = (255, 255, 255)
color_handler = pcl.pcl_visualization.PointCloudColorHandleringCustom(xyz_sample, *color_w)
viewer.AddPointCloud_ColorHandler(xyz_sample, color_handler, b'cloud')
for i in range(len(color)):
id_str = 'pc_sample'+str(i)
pc_sample = pcl.PointCloud(new_points[i, :, :].numpy())
color_handler = pcl.pcl_visualization.PointCloudColorHandleringCustom(pc_sample, *color[i])
viewer.AddPointCloud_ColorHandler(pc_sample, color_handler, id_str)
viewer.AddCoordinateSystem(0.1)
timeit("runtime", begin_time)
while not viewer.WasStopped():
viewer.Spin()
new_xyz, new_points = sample_and_group
其中new_xyz
为采样得到的点,new_points
为基于采样点分组得到的点集合