PCL은 RANSAC법으로 점 구름 평면을 구하고 평면 점을 표시한다

15275 단어 PCL평면
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

void main()
{
     
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());//    
	if (pcl::io::loadPCDFile("save.pcd", *cloud))
	{
     
		std::cerr << "ERROR: Cannot open file " << std::endl;
		return;
	}
	//          ,      
	pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients);
	//inliers                   
	pcl::PointIndices::Ptr inliers(new pcl::PointIndices);
	//        
	pcl::SACSegmentation<pcl::PointXYZ> seg;
	// Optional,                              。
	seg.setOptimizeCoefficients(true);
	// Mandatory-        
	seg.setModelType(pcl::SACMODEL_PLANE);
	//    :     
	seg.setMethodType(pcl::SAC_RANSAC);
	//        ,         
	seg.setDistanceThreshold(0.01);
	//    
	seg.setInputCloud(cloud);
	//    ,        
	seg.segment(*inliers, *coefficients);
	//      
	std::cout << "x:" << coefficients->values[0] << endl;
	std::cout << "y:" << coefficients->values[1] << endl;
	std::cout << "z:" << coefficients->values[2] << endl;
	//     
	pcl::PointCloud<pcl::PointXYZ>::Ptr clicked_points_3d(new pcl::PointCloud<pcl::PointXYZ>);
	for (int i = 0; i < inliers->indices.size(); ++i)
	{
     
		clicked_points_3d->points.push_back(cloud->points.at(inliers->indices[i]));
	}
	//      
	boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("viewer"));
	//      
	viewer->addPointCloud(cloud, "data");
	//          
	pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> red(clicked_points_3d, 255, 0, 0);
	std::string cloudName="plane";
	viewer->addPointCloud(clicked_points_3d, red, cloudName);
	viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 5, cloudName);
	//    ,        
	while (!viewer->wasStopped())
	{
     
		viewer->spinOnce(100);
		boost::this_thread::sleep(boost::posix_time::microseconds(100000));
	}
}

좋은 웹페이지 즐겨찾기