Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Using Kinect input instead of a .pcd file and viewing results in RVIZ?

I am trying to apply some filters and euclidean clustering to input from a Kinect sensor. Currently I am using a piece of code that calls in the "table_scene_lms400.pcd" but I would like to know how I can use input from a Kinect sensor rather than this .pcd file? I would also like to view the results in RVIZ? I know this involves subscribers and publishers and despite numerous attempts using tutorials, etc. I have not been able to accomplish this so was wondering if anybody has any code to do the above or could edit my own code/give me some tips to help me? I have included my code below. (I have tried following most online tutorials and guides but I am still struggling so any help would be greatly appreciated)

include <ros ros.h="">

include <pcl modelcoefficients.h="">

include <pcl point_types.h="">

include <pcl io="" pcd_io.h="">

include <pcl filters="" extract_indices.h="">

include <pcl filters="" voxel_grid.h="">

include <pcl features="" normal_3d.h="">

include <pcl kdtree="" kdtree.h="">

include <pcl sample_consensus="" method_types.h="">

include <pcl sample_consensus="" model_types.h="">

include <pcl segmentation="" sac_segmentation.h="">

include <pcl segmentation="" extract_clusters.h="">

include <sensor_msgs pointcloud2.h="">

include <pcl_conversions pcl_conversions.h="">

include <pcl point_cloud.h="">

include <pcl point_types.h="">

ros::Publisher pub;

int main (int argc, char** argv) {

ros::init(argc, argv, "perception_node"); ros::NodeHandle nh;

// Read in the cloud data pcl::PCDReader reader; pcl::PointCloud<pcl::pointxyz>::Ptr cloud (new pcl::PointCloud<pcl::pointxyz>), cloud_f (new pcl::PointCloud<pcl::pointxyz>); reader.read ("table_scene_lms400.pcd", cloud); std::cout << "PointCloud before filtering has: " << cloud->points.size () << " data points." << std::endl; //

// Create the filtering object: downsample the dataset using a leaf size of 1cm pcl::VoxelGrid<pcl::pointxyz> vg; pcl::PointCloud<pcl::pointxyz>::Ptr cloud_filtered (new pcl::PointCloud<pcl::pointxyz>); vg.setInputCloud (cloud); vg.setLeafSize (0.01f, 0.01f, 0.01f); vg.filter (cloud_filtered); std::cout << "PointCloud after filtering has: " << cloud_filtered->points.size () << " data points." << std::endl; //

// Create the segmentation object for the planar model and set all the parameters pcl::SACSegmentation<pcl::pointxyz> seg; pcl::PointIndices::Ptr inliers (new pcl::PointIndices); pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients); pcl::PointCloud<pcl::pointxyz>::Ptr cloud_plane (new pcl::PointCloud<pcl::pointxyz> ()); pcl::PCDWriter writer; seg.setOptimizeCoefficients (true); seg.setModelType (pcl::SACMODEL_PLANE); seg.setMethodType (pcl::SAC_RANSAC); seg.setMaxIterations (200); seg.setDistanceThreshold (0.02);

int i=0, nr_points = (int) cloud_filtered->points.size (); while (cloud_filtered->points.size () > 0.3 * nr_points) { // Segment the largest planar component from the remaining cloud seg.setInputCloud (cloud_filtered); seg.segment (*inliers, *coefficients); if (inliers->indices.size () == 0) { std::cout << "Could not estimate a planar model for the given dataset." << std::endl; break; }

// Extract the planar inliers from the input cloud
pcl::ExtractIndices<pcl::PointXYZ> extract;
extract.setInputCloud (cloud_filtered);
extract.setIndices (inliers);
extract.setNegative (false);

// Get the points associated with the planar surface
extract.filter (*cloud_plane);
std::cout << "PointCloud representing the planar component: " << cloud_plane->points.size () << " data points." << std::endl;

// Remove the planar inliers, extract the rest
extract.setNegative (true);
extract.filter (*cloud_f);
*cloud_filtered = *cloud_f;

}

// Creating the KdTree object for the search method of the extraction pcl::search::KdTree<pcl::pointxyz>::Ptr tree (new pcl::search::KdTree<pcl::pointxyz>); tree->setInputCloud (cloud_filtered);

std::vector<pcl::pointindices> cluster_indices; pcl::EuclideanClusterExtraction<pcl::pointxyz> ec; ec.setClusterTolerance (0.02); // 2cm ec.setMinClusterSize (100); ec.setMaxClusterSize (25000); ec.setSearchMethod (tree); ec.setInputCloud (cloud_filtered); ec.extract (cluster_indices);

int j = 0; for (std::vector<pcl::pointindices>::const_iterator it = cluster_indices.begin (); it != cluster_indices.end (); ++it) { pcl::PointCloud<pcl::pointxyz>::Ptr cloud_cluster (new pcl::PointCloud<pcl::pointxyz>); for (std::vector<int>::const_iterator pit = it->indices.begin (); pit != it->indices.end (); pit++) cloud_cluster->points.push_back (cloud_filtered->points[pit]); // cloud_cluster->width = cloud_cluster->points.size (); cloud_cluster->height = 1; cloud_cluster->is_dense = true;

std::cout << "PointCloud representing the Cluster: " << cloud_cluster->points.size () << " data points." << std::endl;
std::stringstream ss;
ss << "cloud_cluster_" << j << ".pcd";
writer.write<pcl::PointXYZ> (ss.str (), *cloud_cluster, false); //*
j++;

}

return (0); }

click to hide/show revision 2
No.2 Revision

Using Kinect input instead of a .pcd file and viewing results in RVIZ?

I am trying to apply some filters and euclidean clustering to input from a Kinect sensor. Currently I am using a piece of code that calls in the "table_scene_lms400.pcd" but I would like to know how I can use input from a Kinect sensor rather than this .pcd file? I would also like to view the results in RVIZ? I know this involves subscribers and publishers and despite numerous attempts using tutorials, etc. I have not been able to accomplish this so was wondering if anybody has any code to do the above or could edit my own code/give me some tips to help me? I have included my code below. (I have tried following most online tutorials and guides but I am still struggling so any help would be greatly appreciated)

include <ros ros.h="">

include <pcl modelcoefficients.h="">

include <pcl point_types.h="">

include <pcl io="" pcd_io.h="">

include <pcl filters="" extract_indices.h="">

include <pcl filters="" voxel_grid.h="">

include <pcl features="" normal_3d.h="">

include <pcl kdtree="" kdtree.h="">

include <pcl sample_consensus="" method_types.h="">

include <pcl sample_consensus="" model_types.h="">

include <pcl segmentation="" sac_segmentation.h="">

include <pcl segmentation="" extract_clusters.h="">

include <sensor_msgs pointcloud2.h="">

include <pcl_conversions pcl_conversions.h="">

include <pcl point_cloud.h="">

include <pcl point_types.h="">

#include <ros/ros.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/features/normal_3d.h>
#include <pcl/kdtree/kdtree.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/segmentation/extract_clusters.h>
#include <sensor_msgs/PointCloud2.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>

ros::Publisher pub;

pub; int main (int argc, char** argv) {

{ ros::init(argc, argv, "perception_node"); ros::NodeHandle nh;

nh; // Read in the cloud data pcl::PCDReader reader; pcl::PointCloud<pcl::pointxyz>::Ptr pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::pointxyz>), pcl::PointCloud<pcl::PointXYZ>), cloud_f (new pcl::PointCloud<pcl::pointxyz>); pcl::PointCloud<pcl::PointXYZ>); reader.read ("table_scene_lms400.pcd", cloud); *cloud); std::cout << "PointCloud before filtering has: " << cloud->points.size () << " data points." << std::endl; //

//* // Create the filtering object: downsample the dataset using a leaf size of 1cm pcl::VoxelGrid<pcl::pointxyz> pcl::VoxelGrid<pcl::PointXYZ> vg; pcl::PointCloud<pcl::pointxyz>::Ptr pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::pointxyz>); pcl::PointCloud<pcl::PointXYZ>); vg.setInputCloud (cloud); vg.setLeafSize (0.01f, 0.01f, 0.01f); vg.filter (cloud_filtered); (*cloud_filtered); std::cout << "PointCloud after filtering has: " << cloud_filtered->points.size () << " data points." << std::endl; //

//* // Create the segmentation object for the planar model and set all the parameters pcl::SACSegmentation<pcl::pointxyz> pcl::SACSegmentation<pcl::PointXYZ> seg; pcl::PointIndices::Ptr inliers (new pcl::PointIndices); pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients); pcl::PointCloud<pcl::pointxyz>::Ptr pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_plane (new pcl::PointCloud<pcl::pointxyz> pcl::PointCloud<pcl::PointXYZ> ()); pcl::PCDWriter writer; seg.setOptimizeCoefficients (true); seg.setModelType (pcl::SACMODEL_PLANE); seg.setMethodType (pcl::SAC_RANSAC); seg.setMaxIterations (200); seg.setDistanceThreshold (0.02);

(0.02); int i=0, nr_points = (int) cloud_filtered->points.size (); while (cloud_filtered->points.size () > 0.3 * nr_points) { // Segment the largest planar component from the remaining cloud seg.setInputCloud (cloud_filtered); seg.segment (*inliers, *coefficients); if (inliers->indices.size () == 0) { std::cout << "Could not estimate a planar model for the given dataset." << std::endl; break; }

}

    // Extract the planar inliers from the input cloud
 pcl::ExtractIndices<pcl::PointXYZ> extract;
 extract.setInputCloud (cloud_filtered);
 extract.setIndices (inliers);
 extract.setNegative (false);

 // Get the points associated with the planar surface
 extract.filter (*cloud_plane);
 std::cout << "PointCloud representing the planar component: " << cloud_plane->points.size () << " data points." << std::endl;

 // Remove the planar inliers, extract the rest
 extract.setNegative (true);
 extract.filter (*cloud_f);
 *cloud_filtered = *cloud_f;

}

} // Creating the KdTree object for the search method of the extraction pcl::search::KdTree<pcl::pointxyz>::Ptr pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::pointxyz>); pcl::search::KdTree<pcl::PointXYZ>); tree->setInputCloud (cloud_filtered);

std::vector<pcl::pointindices> (cloud_filtered); std::vector<pcl::PointIndices> cluster_indices; pcl::EuclideanClusterExtraction<pcl::pointxyz> pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec; ec.setClusterTolerance (0.02); // 2cm ec.setMinClusterSize (100); ec.setMaxClusterSize (25000); ec.setSearchMethod (tree); ec.setInputCloud (cloud_filtered); ec.extract (cluster_indices);

(cluster_indices); int j = 0; for (std::vector<pcl::pointindices>::const_iterator (std::vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin (); it != cluster_indices.end (); ++it) { pcl::PointCloud<pcl::pointxyz>::Ptr pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster (new pcl::PointCloud<pcl::pointxyz>); pcl::PointCloud<pcl::PointXYZ>); for (std::vector<int>::const_iterator pit = it->indices.begin (); pit != it->indices.end (); pit++) cloud_cluster->points.push_back (cloud_filtered->points[pit]); // (cloud_filtered->points[*pit]); //* cloud_cluster->width = cloud_cluster->points.size (); cloud_cluster->height = 1; cloud_cluster->is_dense = true;

true;

    std::cout << "PointCloud representing the Cluster: " << cloud_cluster->points.size () << " data points." << std::endl;
 std::stringstream ss;
 ss << "cloud_cluster_" << j << ".pcd";
 writer.write<pcl::PointXYZ> (ss.str (), *cloud_cluster, false); //*
 j++;
  }

  return (0);
}

}

return (0); }