点云数据:bunny.txt
(1)投影到平面
SACMODEL_PLANE(三维平面)
used to determine plane models. The four coefficients of the plane are itsHessian Normal form: [normal_x normal_y normal_z d]
- a : the X coordinate of the plane's normal (normalized)
- b : the Y coordinate of the plane's normal (normalized)
- c : the Z coordinate of the plane's normal (normalized)
- d : the fourth Hessian component of the plane's equation
#include <fstream>
#include <sstream>
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/filters/project_inliers.h>
int main(int argc, char** argv) {
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_projected(new pcl::PointCloud<pcl::PointXYZ>);
std::ifstream fin("/tmp/bunny.txt");
std::string line;
pcl::PointXYZ point;
while (getline(fin, line)) {
std::stringstream ss(line);
ss >> point.x;
ss >> point.y;
ss >> point.z;
point.x *= 100;
point.y *= 100;
point.z *= 100;
cloud->push_back(point);
}
fin.close();
pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients());
coefficients->values.resize(4);
coefficients->values[0] = 0;
coefficients->values[1] = 0;
coefficients->values[2] = 1;
coefficients->values[3] = 0;
pcl::ProjectInliers<pcl::PointXYZ> proj;
proj.setModelType(pcl::SACMODEL_PLANE);
proj.setInputCloud(cloud);
proj.setModelCoefficients(coefficients);
proj.filter(*cloud_projected);
pcl::io::savePCDFile<pcl::PointXYZ>("/tmp/cloud_projected.pcd", *cloud_projected);
return 0;
}
(2)投影到圆柱
used to determine cylinder models. The seven coefficients of the cylinder are given by a point on its axis, the axis direction, and a radius, as: [point_on_axis.x point_on_axis.y point_on_axis.z axis_direction.x axis_direction.y axis_direction.z radius]
- point_on_axis.x : the X coordinate of a point located on the cylinder axis
- point_on_axis.y : the Y coordinate of a point located on the cylinder axis
- point_on_axis.z : the Z coordinate of a point located on the cylinder axis
- axis_direction.x : the X coordinate of the cylinder's axis direction
- axis_direction.y : the Y coordinate of the cylinder's axis direction
- axis_direction.z : the Z coordinate of the cylinder's axis direction
- radius : the cylinder's radius
#include <fstream>
#include <sstream>
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/filters/project_inliers.h>
int main(int argc, char** argv) {
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_projected(new pcl::PointCloud<pcl::PointXYZ>);
std::ifstream fin("/tmp/bunny.txt");
std::string line;
pcl::PointXYZ point;
while (getline(fin, line)) {
std::stringstream ss(line);
ss >> point.x;
ss >> point.y;
ss >> point.z;
point.x *= 100;
point.y *= 100;
point.z *= 100;
cloud->push_back(point);
}
fin.close();
pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients());
coefficients->values.resize(7);
coefficients->values[0] = 0;
coefficients->values[1] = 0;
coefficients->values[2] = 0;
coefficients->values[3] = 0;
coefficients->values[4] = 0;
coefficients->values[5] = 1;
coefficients->values[6] = 1;
pcl::ProjectInliers<pcl::PointXYZ> proj;
proj.setModelType(pcl::SACMODEL_CYLINDER);
proj.setInputCloud(cloud);
proj.setModelCoefficients(coefficients);
proj.filter(*cloud_projected);
pcl::io::savePCDFile<pcl::PointXYZ>("/tmp/test_filter.pcd", *cloud_projected);
return 0;
}
其他参数几何模型参考:https://pointclouds.org/documentation/group__sample__consensus.html