Cluster extraction in different files for different regions
Hi,
I am new in using Kinect.. I am trying to do a simple code for color recognition. I followed the tutorials in PCL library. It worked for me and I identified the different clusters that I want to have. Now I have problem in extracting the data from these clusters.. I save each cluster in a PCD file. I got a problem where the second cluster is the equal to the firat cluster and the second cluster. The third cluster is equal to the first + second + third clusters. The forth cluster is equal to the First + second + third + forth cluster and so on. I identified this problem by running "rosrun pcl_ros pcltopointcloud" and visuliza the pcd files in RVIZ. Now I want each cluster alone without the others.
I have the following code and I don not know where I am mistaken.
#include <ros/ros.h>
#include <iostream>
#include <fstream>
#include <sensor_msgs/PointCloud.h>
#include <sensor_msgs/PointCloud2.h>
#include <sensor_msgs/point_cloud_conversion.h>
#include <visualization_msgs/Marker.h>
#include <tf/transform_listener.h>
#include <tf/transform_broadcaster.h>
#include <tf/transform_datatypes.h>
#include <vector>
#include <pcl/search/search.h>
#include <pcl/segmentation/region_growing_rgb.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/filters/passthrough.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/features/normal_3d.h>
#include <pcl/filters/project_inliers.h>
#include <pcl/surface/convex_hull.h>
#include <pcl/search/kdtree.h>
#include <pcl/segmentation/extract_polygonal_prism_data.h>
#include <pcl/segmentation/extract_clusters.h>
#include <pcl_ros/transforms.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
int main (int argc, char** argv)
{
ros::init (argc, argv, "color_segmentation");
ros::NodeHandle nh_;
tf::TransformListener listener_;
typedef pcl::PointXYZRGB Point;
std::string processing_frame_="/camera_link";
pcl::search::Search <pcl::PointXYZRGB>::Ptr tree = boost::shared_ptr<pcl::search::Search<pcl::PointXYZRGB> > (new pcl::search::KdTree<pcl::PointXYZRGB>);
std::string topic = nh_.resolveName("/camera/depth_registered/points");
ROS_INFO(" waiting for a point_cloud2 on topic %s", topic.c_str());
sensor_msgs::PointCloud2::ConstPtr recent_cloud = ros::topic::waitForMessage<sensor_msgs::PointCloud2>(topic);
sensor_msgs::PointCloud old_cloud;
sensor_msgs::convertPointCloud2ToPointCloud (*recent_cloud, old_cloud);
sensor_msgs::PointCloud2 converted_cloud;
sensor_msgs::convertPointCloudToPointCloud2 (old_cloud, converted_cloud);
pcl::PointCloud<Point>::Ptr cloud (new pcl::PointCloud<Point>);
// pcl::PointCloud <pcl::Point>::Ptr cloud (new pcl::PointCloud <pcl::Point>);
try
{
pcl::fromROSMsg(converted_cloud, *cloud);
}
catch (...)
{
ROS_ERROR("Failure while converting the ROS Message to PCL point cloud. segmentation now requires the input cloud to have color info, so you must input a cloud with XYZRGB points, not just XYZ.");
throw;
}
pcl::IndicesPtr indices (new std::vector <int>);
pcl::PassThrough<pcl::PointXYZRGB> pass;
pass.setInputCloud (cloud);
pass.setFilterFieldName ("z");
pass.setFilterLimits (0.0, 2.0);
pass.filter (*indices);
pcl::RegionGrowingRGB<pcl::PointXYZRGB> reg;
reg.setInputCloud (cloud);
reg.setIndices (indices);
reg.setSearchMethod (tree);
reg.setDistanceThreshold (10);
reg.setPointColorThreshold (6);
reg.setRegionColorThreshold (5);
reg.setMinClusterSize (1000);
std::vector <pcl::PointIndices> clusters;
reg.extract (clusters);
ROS_INFO ("Number of clusters found matching the given constraints: %d.", (int ...