示例#1
1
void init(const OpenROVmessages::LaserMsg msg){

	/********************* INITIALIZE CLOUDS *********************/
	// Mandatory: Init he sizes of the point clouds
	temp_cloud->width = msg.n_rois;
	temp_cloud->height = 2;
	temp_cloud->resize(temp_cloud->width * temp_cloud->height);

	nr_points = temp_cloud->points.size();

	/**************** INITIALIZE SEGMENTER ****************/
	// Mandatory: use perpendicular plane to use setAxis() function
	seg.setModelType (pcl::SACMODEL_PERPENDICULAR_PLANE);
	
//	seg.setOptimizeCoefficients (true);
	// Set method and threshold
	seg.setMethodType (pcl::SAC_RANSAC);
//	seg.setDistanceThreshold (msg.ransac_threshold);
	seg.setDistanceThreshold (5);
	
	// Set the axis for which to search for perpendicular planes
	Eigen::Vector3f axis = Eigen::Vector3f(1.0,1.0,0.0);
	
	// Set a allowed deviation angle from vector axis
	seg.setEpsAngle((60*CV_PI)/180);
	seg.setAxis(axis);
}
示例#2
0
int find_planes(const OpenROVmessages::LaserMsg msg){
	ROS_INFO("Entered find_planes");
	// Fill cloud with data
	fill_cloud(msg);

	// Find planes in the cloud
	while(temp_cloud->points.size() > 0.1 * nr_points){

		seg.setModelType (pcl::SACMODEL_PERPENDICULAR_PLANE);

		seg.setOptimizeCoefficients (true);
		// Set method and threshold
		seg.setMethodType (pcl::SAC_RANSAC);
		seg.setDistanceThreshold (msg.ransac_threshold);

		// Set the axis for which to search for perpendicular planes
		Eigen::Vector3f axis = Eigen::Vector3f(1.0,0.0,0.0);

		// Set a allowed deviation angle from vector axis
		seg.setEpsAngle((60*CV_PI)/180);
		seg.setAxis(axis);
		// Segment the planes and insert the coefficients and inliers in vectors
		seg.setInputCloud (temp_cloud);
		seg.segment (*inliers, *coefficients);

		// Check that we found a plane from the cloud
		if (inliers->indices.size () == 0)
		{
			std::cerr << "Could not estimate a planar model for the given dataset." << std::endl;
			break;
		}
		// push coeffients onto stack
		coef_vect.push_back(*coefficients);

		// Extract the inliers
		extract.setInputCloud (temp_cloud);
		extract.setIndices (inliers);

		// Extract found inliers from cloud
		extract.setNegative(true);
		extract.filter(*temp_cloud);
	}
	return 0;
}
void depthPointsCallback(const sensor_msgs::PointCloud2ConstPtr& cloud_msg) {

    // Instantiate various clouds
    pcl::PCLPointCloud2* cloud_intermediate = new pcl::PCLPointCloud2;
    pcl::PCLPointCloud2ConstPtr cloudPtr(cloud_intermediate);
    pcl::PointCloud<pcl::PointXYZ> cloud;

    // Convert to PCL data type
    pcl_conversions::toPCL(*cloud_msg, *cloud_intermediate);

    // Apply Voxel Filter on PCLPointCloud2
    vox.setInputCloud (cloudPtr);
    vox.setInputCloud (cloudPtr);
    vox.filter (*cloud_intermediate);

    // Convert PCL::PointCloud2 to PCL::PointCloud<PointXYZ>
    pcl::fromPCLPointCloud2(*cloud_intermediate, cloud);
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_p = cloud.makeShared();

    // Apply Passthrough Filter
    pass.setFilterFieldName ("z");
    pass.setFilterLimits (0.3, 1);
    pass.setInputCloud (cloud_p);
    //pass.setFilterLimitsNegative (true);
    pass.filter (*cloud_p);

    // Apply Passthrough Filter
    pass.setFilterFieldName ("x");
    pass.setFilterLimits (-0.2, 0.2);
    pass.setInputCloud (cloud_p);
    pass.setFilterFieldName ("z");
    pass.setFilterLimits (0.0, 3.0);
    //pass.setFilterLimitsNegative (true);
    pass.filter (*cloud_p);

    // Apply Statistical Noise Filter
    sor.setInputCloud (cloud_p);
    sor.filter (*cloud_p);

    // Planar segmentation: Remove large planes? Or extract floor?
    pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
    pcl::PointIndices::Ptr inliers (new pcl::PointIndices);

    int nr_points = (int) cloud_p->points.size ();

    Eigen::Vector3f lol (0, 1, 0);
    seg.setEpsAngle(  30.0f * (3.14f/180.0f) );
    seg.setAxis(lol);
    //while(cloud_p->points.size () > 0.2 * nr_points) {
    sor.setInputCloud (cloud_p);
    sor.filter (*cloud_p);
    // Create the segmentation object
    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_p);
    seg.segment (*inliers, *coefficients);

    if (inliers->indices.size () == 0)
    {
        //break;
    }
    else {
        /*std::cout << "Model coefficients: " << coefficients->values[0] << " "
                      << coefficients->values[1] << " "
                      << coefficients->values[2] << " "
                      << coefficients->values[3] << "\n";*/
        pcl::ExtractIndices<pcl::PointXYZ> extract;
        extract.setInputCloud(cloud_p);
        extract.setIndices(inliers);
        extract.setNegative(true);
        extract.filter(*cloud_p);
    }
    //}

    Eigen::Vector3f lol_p (0.5f, 0, 0.5f);
    seg.setAxis(lol_p);
    while(cloud_p->points.size () > 0.1 * nr_points) {

        seg.setInputCloud (cloud_p);
        seg.segment (*inliers, *coefficients);

        if (inliers->indices.size () == 0)
        {
            break;
        }
        else {
            /*std::cout << "Model coefficients: " << coefficients->values[0] << " "
                      << coefficients->values[1] << " "
                      << coefficients->values[2] << " "
                      << coefficients->values[3] << "\n";*/
            pcl::ExtractIndices<pcl::PointXYZ> extract;
            extract.setInputCloud(cloud_p);
            extract.setIndices(inliers);
            extract.setNegative(true);
            extract.filter(*cloud_p);
        }
    }

    // Apply Statistical Noise Filter
    sor.setInputCloud (cloud_p);
    sor.filter (*cloud_p);

    if(cloud_p->points.size() > 0) {
        std::vector<pcl::PointIndices> cluster_indices;
        tree->setInputCloud (cloud_p);
        ec.setInputCloud (cloud_p);
        ec.extract (cluster_indices);

        std::cout << "Clusters detected: " << cluster_indices.size() << "\n";
        // Convert to ROS data type
    }
    // Convert to ROS data type
    sensor_msgs::PointCloud2 output;
    pcl::toROSMsg(*cloud_p, output);


    // Publish the data
    downsample_pub.publish(output);

}