ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Novinho's profile - activity

2016-08-31 08:59:19 -0500 received badge  Enthusiast
2016-08-29 12:48:48 -0500 asked a question How can I use the Euclidean Cluster Extraction PCL Method using ROS?

Hi there, I am new here... So, I need to write a ROS Node to solve a problem using the Euclidean Cluster Extractionn PCL, but I don't know whats exactly is wrong. Please, help me to understand it. And thank you a lot for that!

#include <ros ros.h=""> #include <nodelet nodelet.h=""> #include <sensor_msgs pointcloud2.h=""> #include <pcl_conversions pcl_conversions.h=""> #include <pcl point_cloud.h=""> #include <pcl point_types.h=""> #include <boost foreach.hpp=""> #include <sstream> #include "novinho_laser/Vai.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=""> ros::Publisher pub;

using namespace pcl; using namespace std;

typedef pcl::PointXYZ PointT; typedef pcl::PointCloud<pointt> PointCloudT;

void cloud_cb ( const sensor_msgs::PointCloud2 input ) { pcl::PCLPointCloud2 pcl_pc; pcl_conversions::toPCL ( input, pcl_pc ); pcl::PointCloud<pcl::pointxyz> cloud, cloud_f; pcl::fromPCLPointCloud2 ( pcl_pc, cloud );

//!< 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_f ); // ...cloud -> cloud_f vg.setLeafSize ( 0.01f, 0.01f, 0.01f ); vg.filter ( *cloud_filtered );

//!< 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> ( ) );

seg.setOptimizeCoefficients ( true ); seg.setModelType ( pcl::SACMODEL_PLANE ); seg.setMethodType ( pcl::SAC_RANSAC ); seg.setMaxIterations ( 100 ); 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 ) 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 );

//!< 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 );

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; }

float x = 0.0, y = 0.0, z = 1e6; unsigned int n = 0; //!< - Number of points observed

BOOST_FOREACH ( const pcl::PointXYZ& pt, cloud_cluster ...
(more)
2016-04-04 12:34:29 -0500 answered a question Navigation of turtlebot (Moving 1m forward, 1m left)

Hi, dylankc. Did you solve this problem?... If yes, how could you do that? Thanks

2016-02-15 20:45:17 -0500 received badge  Popular Question (source)
2016-02-15 13:06:43 -0500 commented question Kd-tree to search on ROS

Sorry, and thank you for your comment :)

2016-02-15 12:58:28 -0500 received badge  Editor (source)
2016-02-15 12:24:49 -0500 asked a question Kd-tree to search on ROS

Hi there! I'm sorry if the question is very simple, but it's the first time that I try to use ROS and PCL too. I'd like to adapt the code of this link:

http://pointclouds.org/documentation/...

to use on ROS and visualize the result on Rviz.

What I need to change of the code below?...

#include <ros/ros.h>
#include <sensor_msgs/PointCloud2.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/PCLPointCloud2.h>
#include <pcl/conversions.h>
#include <pcl_ros/transforms.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <iostream>
#include <vector>
#include <ctime>

ros::Publisher pub;

void 
cloud_cb (const sensor_msgs::PointCloud2ConstPtr& cloud_msg)
{

  pcl::PCLPointCloud2* cloud = new pcl::PCLPointCloud2; 
  pcl::PCLPointCloud2ConstPtr cloudPtr(cloud);
  pcl::PCLPointCloud2 cloud_filtered;

  pcl_conversions::toPCL(*cloud_msg, *cloud);

  srand (time (NULL));

  cloud->width = 1000;
  cloud->height = 1;
  cloud->points.resize (cloud->width * cloud->height);

  for (size_t i = 0; i < cloud->points.size (); ++i)
  {
    cloud->points[i].x = 1024.0f * rand () / (RAND_MAX + 1.0f);
    cloud->points[i].y = 1024.0f * rand () / (RAND_MAX + 1.0f);
    cloud->points[i].z = 1024.0f * rand () / (RAND_MAX + 1.0f);
  }

  pcl::KdTreeFLANN<pcl::PointXYZ> kdtree;

  kdtree.setInputCloud (cloud);

  pcl::PointXYZ searchPoint;

  searchPoint.x = 1024.0f * rand () / (RAND_MAX + 1.0f);
  searchPoint.y = 1024.0f * rand () / (RAND_MAX + 1.0f);
  searchPoint.z = 1024.0f * rand () / (RAND_MAX + 1.0f);

  int K = 10;
  std::vector<int> pointIdxNKNSearch(K);
  std::vector<float> pointNKNSquaredDistance(K);

  std::cout << "K nearest neighbor search at (" << searchPoint.x 
            << " " << searchPoint.y 
            << " " << searchPoint.z
            << ") with K=" << K << std::endl;

  if ( kdtree.nearestKSearch (searchPoint, K, pointIdxNKNSearch, pointNKNSquaredDistance) > 0 )
  {
    for (size_t i = 0; i < pointIdxNKNSearch.size (); ++i)
      std::cout << "    "  <<   cloud->points[ pointIdxNKNSearch[i] ].x 
                << " " << cloud->points[ pointIdxNKNSearch[i] ].y 
                << " " << cloud->points[ pointIdxNKNSearch[i] ].z 
                << " (squared distance: " << pointNKNSquaredDistance[i] << ")" << std::endl;
  }

  std::vector<int> pointIdxRadiusSearch;
  std::vector<float> pointRadiusSquaredDistance;

  float radius = 256.0f * rand () / (RAND_MAX + 1.0f);

  std::cout << "Neighbors within radius search at (" << searchPoint.x 
            << " " << searchPoint.y 
            << " " << searchPoint.z
            << ") with radius=" << radius << std::endl;


  if ( kdtree.radiusSearch (searchPoint, radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0 )
  {
    for (size_t i = 0; i < pointIdxRadiusSearch.size (); ++i)
      std::cout << "    "  <<   cloud->points[ pointIdxRadiusSearch[i] ].x 
                << " " << cloud->points[ pointIdxRadiusSearch[i] ].y 
                << " " << cloud->points[ pointIdxRadiusSearch[i] ].z 
                << " (squared distance: " << pointRadiusSquaredDistance[i] << ")" << std::endl;
  }

  sensor_msgs::PointCloud2 output;
  pcl_conversions::moveFromPCL(cloud_filtered, output);

  pub.publish (output);

} 

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

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

  ros::Subscriber sub = nh.subscribe<sensor_msgs::PointCloud2> ("input", 1, cloud_cb);
  pub = nh.advertise<sensor_msgs::PointCloud2> ("output", 1);

  ros::spin ();
}