1. 程式人生 > 其它 >點雲投影到引數幾何模型(平面、圓柱等)

點雲投影到引數幾何模型(平面、圓柱等)

技術標籤:PCL

點雲資料:bunny.txt

(1)投影到平面

SACMODEL_PLANE(三維平面)

used to determine plane models. Thefourcoefficients 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 fourthHessian componentof 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)投影到圓柱

SACMODEL_CYLINDER(柱)

used to determine cylinder models. Thesevencoefficients 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