main_naive.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 #include <string.h>
00003 #include <fstream>
00004 #include <algorithm>
00005 #include <iterator>
00006 #include "kf_tracker/featureDetection.h"
00007 #include "kf_tracker/CKalmanFilter.h"
00008 #include <opencv2/core/core.hpp>
00009 #include <opencv2/highgui/highgui.hpp>
00010 #include <opencv2/imgproc/imgproc.hpp>
00011 #include <opencv2/video/video.hpp>
00012 #include "opencv2/video/tracking.hpp"
00013 #include <ros/ros.h>
00014 #include <pcl/io/pcd_io.h>
00015 #include <pcl/point_types.h>
00016 #include "pcl_ros/point_cloud.h"
00017 #include <geometry_msgs/Point.h>
00018 #include <std_msgs/Float32MultiArray.h>
00019 #include <std_msgs/Int32MultiArray.h>
00020 
00021 #include <sensor_msgs/PointCloud2.h>
00022 #include <pcl_conversions/pcl_conversions.h>
00023 #include <pcl/point_cloud.h>
00024 #include <pcl/point_types.h>
00025 #include <pcl/common/geometry.h>
00026 #include <pcl/filters/extract_indices.h>
00027 #include <pcl/filters/voxel_grid.h>
00028 #include <pcl/features/normal_3d.h>
00029 #include <pcl/kdtree/kdtree.h>
00030 #include <pcl/sample_consensus/method_types.h>
00031 #include <pcl/sample_consensus/model_types.h>
00032 #include <pcl/segmentation/sac_segmentation.h>
00033 #include <pcl/segmentation/extract_clusters.h>
00034 
00035 using namespace std;
00036 using namespace cv;
00037 
00038 
00039 ros::Publisher objID_pub;
00040 
00041     // KF init
00042     int stateDim=4;// [x,y,v_x,v_y]//,w,h]
00043     int measDim=2;// [z_x,z_y,z_w,z_h]
00044     int ctrlDim=0;
00045     cv::KalmanFilter KF0(stateDim,measDim,ctrlDim,CV_32F);
00046     cv::KalmanFilter KF1(stateDim,measDim,ctrlDim,CV_32F);
00047     cv::KalmanFilter KF2(stateDim,measDim,ctrlDim,CV_32F);
00048     cv::KalmanFilter KF3(stateDim,measDim,ctrlDim,CV_32F);
00049     cv::KalmanFilter KF4(stateDim,measDim,ctrlDim,CV_32F);
00050     cv::KalmanFilter KF5(stateDim,measDim,ctrlDim,CV_32F);
00051 
00052     ros::Publisher pub_cluster0;
00053     ros::Publisher pub_cluster1;
00054     ros::Publisher pub_cluster2;
00055     ros::Publisher pub_cluster3;
00056     ros::Publisher pub_cluster4;
00057     ros::Publisher pub_cluster5;
00058 
00059     std::vector<geometry_msgs::Point> prevClusterCenters;
00060 
00061 
00062     cv::Mat state(stateDim,1,CV_32F);
00063     cv::Mat_<float> measurement(2,1); 
00064 
00065     std::vector<int> objID;// Output of the data association using KF
00066    // measurement.setTo(Scalar(0));
00067 
00068 bool firstFrame=true;
00069 
00070 // calculate euclidean distance of two points
00071   double euclidean_distance(geometry_msgs::Point& p1, geometry_msgs::Point& p2)
00072   {
00073     return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y) + (p1.z - p2.z) * (p1.z - p2.z));
00074   }
00075 /*
00076 //Count unique object IDs. just to make sure same ID has not been assigned to two KF_Trackers.  
00077 int countIDs(vector<int> v)
00078 {
00079     transform(v.begin(), v.end(), v.begin(), abs); // O(n) where n = distance(v.end(), v.begin())
00080     sort(v.begin(), v.end()); // Average case O(n log n), worst case O(n^2) (usually implemented as quicksort.
00081     // To guarantee worst case O(n log n) replace with make_heap, then sort_heap.
00082 
00083     // Unique will take a sorted range, and move things around to get duplicated
00084     // items to the back and returns an iterator to the end of the unique section of the range
00085     auto unique_end = unique(v.begin(), v.end()); // Again n comparisons
00086     return distance(unique_end, v.begin()); // Constant time for random access iterators (like vector's)
00087 }
00088 */
00089 
00090 /*
00091 
00092 objID: vector containing the IDs of the clusters that should be associated with each KF_Tracker
00093 objID[0] corresponds to KFT0, objID[1] corresponds to KFT1 etc.
00094 */
00095 void KFT(const std_msgs::Float32MultiArray ccs)
00096 {
00097 
00098 
00099 
00100     // First predict, to update the internal statePre variable
00101 
00102     std::vector<cv::Mat> pred{KF0.predict(),KF1.predict(),KF2.predict(),KF3.predict(),KF4.predict(),KF5.predict()};
00103  //cout<<"Pred successfull\n";
00104 
00105     //cv::Point predictPt(prediction.at<float>(0),prediction.at<float>(1));
00106    // cout<<"Prediction 1 ="<<prediction.at<float>(0)<<","<<prediction.at<float>(1)<<"\n";
00107 
00108     // Get measurements
00109     // Extract the position of the clusters forom the multiArray. To check if the data
00110     // coming in, check the .z (every third) coordinate and that will be 0.0
00111     std::vector<geometry_msgs::Point> clusterCenters;//clusterCenters
00112    
00113     int i=0;
00114     for (std::vector<float>::const_iterator it=ccs.data.begin();it!=ccs.data.end();it+=3)
00115     {
00116         geometry_msgs::Point pt;
00117         pt.x=*it;
00118         pt.y=*(it+1);
00119         pt.z=*(it+2);
00120 
00121         clusterCenters.push_back(pt);
00122        
00123     }
00124 
00125   //  cout<<"CLusterCenters Obtained"<<"\n";
00126     std::vector<geometry_msgs::Point> KFpredictions;
00127     i=0;
00128     for (auto it=pred.begin();it!=pred.end();it++)
00129     {
00130         geometry_msgs::Point pt;
00131         pt.x=(*it).at<float>(0);
00132         pt.y=(*it).at<float>(1);
00133         pt.z=(*it).at<float>(2);
00134 
00135         KFpredictions.push_back(pt);
00136         
00137     }
00138    // cout<<"Got predictions"<<"\n";
00139 
00140     /* Original Version using Kalman filter prediction
00141     
00142     // Find the cluster that is more probable to be belonging to a given KF.
00143     objID.clear();//Clear the objID vector
00144     for(int filterN=0;filterN<6;filterN++)
00145     {
00146         std::vector<float> distVec;
00147         for(int n=0;n<6;n++)
00148             distVec.push_back(euclidean_distance(KFpredictions[filterN],clusterCenters[n]));
00149        
00150       // cout<<"distVec[="<<distVec[0]<<","<<distVec[1]<<","<<distVec[2]<<","<<distVec[3]<<","<<distVec[4]<<","<<distVec[5]<<"\n";
00151         objID.push_back(std::distance(distVec.begin(),min_element(distVec.begin(),distVec.end())));
00152        // cout<<"MinD for filter"<<filterN<<"="<<*min_element(distVec.begin(),distVec.end())<<"\n";
00153     
00154     }
00155 
00156    // cout<<"Got object IDs"<<"\n";
00157     //countIDs(objID);// for verif/corner cases
00158       Original version using kalman filter prediction
00159     */
00160     //display objIDs
00161   /* DEBUG
00162     cout<<"objID= ";
00163     for(auto it=objID.begin();it!=objID.end();it++)
00164         cout<<*it<<" ,";
00165     cout<<"\n";
00166     */
00167 
00168 /* Naive version without using kalman filter */
00169 objID.clear();//Clear the objID vector
00170     for(int filterN=0;filterN<6;filterN++)
00171     {
00172         std::vector<float> distVec;
00173         for(int n=0;n<6;n++)
00174             distVec.push_back(euclidean_distance(prevClusterCenters[n],clusterCenters[n]));
00175        
00176       // cout<<"distVec[="<<distVec[0]<<","<<distVec[1]<<","<<distVec[2]<<","<<distVec[3]<<","<<distVec[4]<<","<<distVec[5]<<"\n";
00177         objID.push_back(std::distance(distVec.begin(),min_element(distVec.begin(),distVec.end())));
00178        // cout<<"MinD for filter"<<filterN<<"="<<*min_element(distVec.begin(),distVec.end())<<"\n";
00179     
00180     }
00181      /* Naive version without kalman filter */
00182     //prevClusterCenters.clear();
00183     // Set the current associated clusters to the prevClusterCenters
00184      for (int i=0;i<6;i++)
00185      {
00186         prevClusterCenters[objID.at(i)]=clusterCenters.at(i);
00187        
00188      }
00189 
00190      /* Naive version without kalman filter */
00191 
00192 /* Naive version without using kalman filter */    
00193 
00194 
00195     std_msgs::Int32MultiArray obj_id;
00196     for(auto it=objID.begin();it!=objID.end();it++)
00197         obj_id.data.push_back(*it);
00198     objID_pub.publish(obj_id);
00199     // convert clusterCenters from geometry_msgs::Point to floats
00200     std::vector<std::vector<float> > cc;
00201     for (int i=0;i<clusterCenters.size();i++)
00202     {
00203         vector<float> pt;
00204         pt.push_back(clusterCenters[i].x);
00205         pt.push_back(clusterCenters[i].y);
00206         pt.push_back(clusterCenters[i].z);
00207         
00208         cc.push_back(pt);
00209     }
00210     //cout<<"cc[5][0]="<<cc[5].at(0)<<"cc[5][1]="<<cc[5].at(1)<<"cc[5][2]="<<cc[5].at(2)<<"\n";
00211     float meas0[3]={cc[0].at(0),cc[0].at(1)};
00212     float meas1[3]={cc[1].at(0),cc[1].at(1)};
00213     float meas2[3]={cc[2].at(0),cc[2].at(1)};
00214     float meas3[3]={cc[3].at(0),cc[3].at(1)};
00215     float meas4[3]={cc[4].at(0),cc[4].at(1)};
00216     float meas5[3]={cc[5].at(0),cc[5].at(1)};
00217 
00218 
00219 
00220     // The update phase 
00221     cv::Mat meas0Mat=cv::Mat(2,1,CV_32F,meas0);
00222     cv::Mat meas1Mat=cv::Mat(2,1,CV_32F,meas1);
00223     cv::Mat meas2Mat=cv::Mat(2,1,CV_32F,meas2);
00224     cv::Mat meas3Mat=cv::Mat(2,1,CV_32F,meas3);
00225     cv::Mat meas4Mat=cv::Mat(2,1,CV_32F,meas4);
00226     cv::Mat meas5Mat=cv::Mat(2,1,CV_32F,meas5);
00227 
00228 //cout<<"meas0Mat"<<meas0Mat<<"\n";
00229 
00230     Mat estimated0 = KF0.correct(meas0Mat);
00231     Mat estimated1 = KF0.correct(meas1Mat);
00232     Mat estimated2 = KF0.correct(meas2Mat);
00233     Mat estimated3 = KF0.correct(meas3Mat);
00234     Mat estimated4 = KF0.correct(meas4Mat);
00235     Mat estimated5 = KF0.correct(meas5Mat);
00236  
00237     // Publish the point clouds belonging to each clusters
00238 
00239 
00240    // cout<<"estimate="<<estimated.at<float>(0)<<","<<estimated.at<float>(1)<<"\n";
00241    // Point statePt(estimated.at<float>(0),estimated.at<float>(1));
00242 //cout<<"DONE KF_TRACKER\n";
00243    
00244 }
00245 void publish_cloud(ros::Publisher& pub, pcl::PointCloud<pcl::PointXYZ>::Ptr cluster){
00246   sensor_msgs::PointCloud2::Ptr clustermsg (new sensor_msgs::PointCloud2);
00247   pcl::toROSMsg (*cluster , *clustermsg);
00248   clustermsg->header.frame_id = "/map";
00249   clustermsg->header.stamp = ros::Time::now();
00250   pub.publish (*clustermsg);
00251 
00252 }
00253 
00254 
00255 void cloud_cb (const sensor_msgs::PointCloud2ConstPtr& input)
00256 
00257 {
00258     cout<<"IF firstFrame="<<firstFrame<<"\n";
00259     // If this is the first frame, initialize kalman filters for the clustered objects
00260 if (firstFrame)
00261 {   
00262   // Initialize 6 Kalman Filters; Assuming 6 max objects in the dataset. 
00263   // Could be made generic by creating a Kalman Filter only when a new object is detected  
00264     KF0.transitionMatrix = *(Mat_<float>(4, 4) << 1,0,1,0,   0,1,0,1,  0,0,1,0,  0,0,0,1);
00265     KF1.transitionMatrix = *(Mat_<float>(4, 4) << 1,0,1,0,   0,1,0,1,  0,0,1,0,  0,0,0,1);
00266     KF2.transitionMatrix = *(Mat_<float>(4, 4) << 1,0,1,0,   0,1,0,1,  0,0,1,0,  0,0,0,1);
00267     KF3.transitionMatrix = *(Mat_<float>(4, 4) << 1,0,1,0,   0,1,0,1,  0,0,1,0,  0,0,0,1);
00268     KF4.transitionMatrix = *(Mat_<float>(4, 4) << 1,0,1,0,   0,1,0,1,  0,0,1,0,  0,0,0,1);
00269     KF5.transitionMatrix = *(Mat_<float>(4, 4) << 1,0,1,0,   0,1,0,1,  0,0,1,0,  0,0,0,1);
00270 
00271     cv::setIdentity(KF0.measurementMatrix);
00272     cv::setIdentity(KF1.measurementMatrix);
00273     cv::setIdentity(KF2.measurementMatrix);
00274     cv::setIdentity(KF3.measurementMatrix);
00275     cv::setIdentity(KF4.measurementMatrix);
00276     cv::setIdentity(KF5.measurementMatrix);
00277     // Process Noise Covariance Matrix Q
00278     // [ Ex 0  0    0 0    0 ]
00279     // [ 0  Ey 0    0 0    0 ]
00280     // [ 0  0  Ev_x 0 0    0 ]
00281     // [ 0  0  0    1 Ev_y 0 ]
00284     setIdentity(KF0.processNoiseCov, Scalar::all(1e-4));
00285     setIdentity(KF1.processNoiseCov, Scalar::all(1e-4));
00286     setIdentity(KF2.processNoiseCov, Scalar::all(1e-4));
00287     setIdentity(KF3.processNoiseCov, Scalar::all(1e-4));
00288     setIdentity(KF4.processNoiseCov, Scalar::all(1e-4));
00289     setIdentity(KF5.processNoiseCov, Scalar::all(1e-4));
00290     // Meas noise cov matrix R
00291      cv::setIdentity(KF0.measurementNoiseCov, cv::Scalar(1e-1));
00292      cv::setIdentity(KF1.measurementNoiseCov, cv::Scalar(1e-1));
00293      cv::setIdentity(KF2.measurementNoiseCov, cv::Scalar(1e-1));
00294      cv::setIdentity(KF3.measurementNoiseCov, cv::Scalar(1e-1));
00295      cv::setIdentity(KF4.measurementNoiseCov, cv::Scalar(1e-1));
00296      cv::setIdentity(KF5.measurementNoiseCov, cv::Scalar(1e-1));
00297 
00298 // Process the point cloud
00299      pcl::PointCloud<pcl::PointXYZ>::Ptr input_cloud (new pcl::PointCloud<pcl::PointXYZ>);
00300   pcl::PointCloud<pcl::PointXYZ>::Ptr clustered_cloud (new pcl::PointCloud<pcl::PointXYZ>);
00301       /* Creating the KdTree from input point cloud*/
00302   pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ>);
00303 
00304   pcl::fromROSMsg (*input, *input_cloud);
00305 
00306   tree->setInputCloud (input_cloud);
00307 
00308   /* Here we are creating a vector of PointIndices, which contains the actual index
00309    * information in a vector<int>. The indices of each detected cluster are saved here.
00310    * Cluster_indices is a vector containing one instance of PointIndices for each detected 
00311    * cluster. Cluster_indices[0] contain all indices of the first cluster in input point cloud.
00312    */
00313   std::vector<pcl::PointIndices> cluster_indices;
00314   pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
00315   ec.setClusterTolerance (0.08); 
00316   ec.setMinClusterSize (10);
00317   ec.setMaxClusterSize (600);
00318   ec.setSearchMethod (tree);
00319   ec.setInputCloud (input_cloud);
00320   /* Extract the clusters out of pc and save indices in cluster_indices.*/
00321   ec.extract (cluster_indices);
00322 
00323   /* To separate each cluster out of the vector<PointIndices> we have to 
00324    * iterate through cluster_indices, create a new PointCloud for each 
00325    * entry and write all points of the current cluster in the PointCloud. 
00326    */
00327   //pcl::PointXYZ origin (0,0,0);
00328   //float mindist_this_cluster = 1000;
00329   //float dist_this_point = 1000;
00330 
00331   std::vector<pcl::PointIndices>::const_iterator it;
00332   std::vector<int>::const_iterator pit;
00333   // Vector of cluster pointclouds
00334   std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr > cluster_vec;
00335 
00336   for(it = cluster_indices.begin(); it != cluster_indices.end(); ++it) {
00337           pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster (new pcl::PointCloud<pcl::PointXYZ>);
00338           for(pit = it->indices.begin(); pit != it->indices.end(); pit++) 
00339           {
00340           
00341                   cloud_cluster->points.push_back(input_cloud->points[*pit]);
00342                   //dist_this_point = pcl::geometry::distance(input_cloud->points[*pit],
00343                   //                                          origin);
00344                   //mindist_this_cluster = std::min(dist_this_point, mindist_this_cluster);
00345           }
00346 
00347          
00348       cluster_vec.push_back(cloud_cluster);
00349 
00350     }
00351 
00352     //Ensure at least 6 clusters exist to publish (later clusters may be empty)
00353     while (cluster_vec.size() < 6){
00354       pcl::PointCloud<pcl::PointXYZ>::Ptr empty_cluster (new pcl::PointCloud<pcl::PointXYZ>);
00355       empty_cluster->points.push_back(pcl::PointXYZ(0,0,0));
00356       cluster_vec.push_back(empty_cluster);
00357     }
00358     
00359 
00360      // Set initial state
00361      KF0.statePre.at<float>(0)=cluster_vec[0]->points[cluster_vec[0]->points.size()/2].x;
00362      
00363      KF0.statePre.at<float>(1)=cluster_vec[0]->points[cluster_vec[0]->points.size()/2].y;
00364      KF0.statePre.at<float>(2)=0;// initial v_x
00365      KF0.statePre.at<float>(3)=0;//initial v_y
00366 
00367      // Set initial state
00368      KF1.statePre.at<float>(0)=cluster_vec[1]->points[cluster_vec[1]->points.size()/2].x;
00369      KF1.statePre.at<float>(1)=cluster_vec[1]->points[cluster_vec[1]->points.size()/2].y;
00370      KF1.statePre.at<float>(2)=0;// initial v_x
00371      KF1.statePre.at<float>(3)=0;//initial v_y
00372 
00373      // Set initial state
00374      KF2.statePre.at<float>(0)=cluster_vec[2]->points[cluster_vec[2]->points.size()/2].x;
00375      KF2.statePre.at<float>(1)=cluster_vec[2]->points[cluster_vec[2]->points.size()/2].y;
00376      KF2.statePre.at<float>(2)=0;// initial v_x
00377      KF2.statePre.at<float>(3)=0;//initial v_y
00378 
00379 
00380      // Set initial state
00381      KF3.statePre.at<float>(0)=cluster_vec[3]->points[cluster_vec[3]->points.size()/2].x;
00382      KF3.statePre.at<float>(1)=cluster_vec[3]->points[cluster_vec[3]->points.size()/2].y;
00383      KF3.statePre.at<float>(2)=0;// initial v_x
00384      KF3.statePre.at<float>(3)=0;//initial v_y
00385 
00386      // Set initial state
00387      KF4.statePre.at<float>(0)=cluster_vec[4]->points[cluster_vec[4]->points.size()/2].x;
00388      KF4.statePre.at<float>(1)=cluster_vec[4]->points[cluster_vec[4]->points.size()/2].y;
00389      KF4.statePre.at<float>(2)=0;// initial v_x
00390      KF4.statePre.at<float>(3)=0;//initial v_y
00391 
00392      // Set initial state
00393      KF5.statePre.at<float>(0)=cluster_vec[5]->points[cluster_vec[5]->points.size()/2].x;
00394      KF5.statePre.at<float>(1)=cluster_vec[5]->points[cluster_vec[5]->points.size()/2].y;
00395      KF5.statePre.at<float>(2)=0;// initial v_x
00396      KF5.statePre.at<float>(3)=0;//initial v_y
00397 
00398      firstFrame=false;
00399 
00400      // Print the initial state of the kalman filter for debugging
00401      cout<<"KF0.satePre="<<KF0.statePre.at<float>(0)<<","<<KF0.statePre.at<float>(1)<<"\n";
00402      cout<<"KF1.satePre="<<KF1.statePre.at<float>(0)<<","<<KF1.statePre.at<float>(1)<<"\n";
00403      cout<<"KF2.satePre="<<KF2.statePre.at<float>(0)<<","<<KF2.statePre.at<float>(1)<<"\n";
00404      cout<<"KF3.satePre="<<KF3.statePre.at<float>(0)<<","<<KF3.statePre.at<float>(1)<<"\n";
00405      cout<<"KF4.satePre="<<KF4.statePre.at<float>(0)<<","<<KF4.statePre.at<float>(1)<<"\n";
00406      cout<<"KF5.satePre="<<KF5.statePre.at<float>(0)<<","<<KF5.statePre.at<float>(1)<<"\n";
00407 
00408      //cin.ignore();// To be able to see the printed initial state of the KalmanFilter
00409 
00410 
00411 
00412      /* Naive version without kalman filter */
00413      for (int i=0;i<6;i++)
00414      {
00415         geometry_msgs::Point pt;
00416         pt.x=cluster_vec[i]->points[cluster_vec[i]->points.size()/2].x;
00417         pt.y=cluster_vec[i]->points[cluster_vec[i]->points.size()/2].y;
00418         prevClusterCenters.push_back(pt);
00419      }
00420 
00421      /* Naive version without kalman filter */
00422 }
00423 
00424  
00425 else
00426 { 
00427   cout<<"ELSE firstFrame="<<firstFrame<<"\n";
00428   pcl::PointCloud<pcl::PointXYZ>::Ptr input_cloud (new pcl::PointCloud<pcl::PointXYZ>);
00429   pcl::PointCloud<pcl::PointXYZ>::Ptr clustered_cloud (new pcl::PointCloud<pcl::PointXYZ>);
00430       /* Creating the KdTree from input point cloud*/
00431   pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ>);
00432 
00433   pcl::fromROSMsg (*input, *input_cloud);
00434 
00435   tree->setInputCloud (input_cloud);
00436 
00437   /* Here we are creating a vector of PointIndices, which contains the actual index
00438    * information in a vector<int>. The indices of each detected cluster are saved here.
00439    * Cluster_indices is a vector containing one instance of PointIndices for each detected 
00440    * cluster. Cluster_indices[0] contain all indices of the first cluster in input point cloud.
00441    */
00442   std::vector<pcl::PointIndices> cluster_indices;
00443   pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
00444   ec.setClusterTolerance (0.08); 
00445   ec.setMinClusterSize (10);
00446   ec.setMaxClusterSize (600);
00447   ec.setSearchMethod (tree);
00448   ec.setInputCloud (input_cloud);
00449 cout<<"PCL init successfull\n";
00450   /* Extract the clusters out of pc and save indices in cluster_indices.*/
00451   ec.extract (cluster_indices);
00452 cout<<"PCL extract successfull\n";
00453   /* To separate each cluster out of the vector<PointIndices> we have to 
00454    * iterate through cluster_indices, create a new PointCloud for each 
00455    * entry and write all points of the current cluster in the PointCloud. 
00456    */
00457   //pcl::PointXYZ origin (0,0,0);
00458   //float mindist_this_cluster = 1000;
00459   //float dist_this_point = 1000;
00460 
00461   std::vector<pcl::PointIndices>::const_iterator it;
00462   std::vector<int>::const_iterator pit;
00463   // Vector of cluster pointclouds
00464   std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr > cluster_vec;
00465 
00466   for(it = cluster_indices.begin(); it != cluster_indices.end(); ++it) {
00467           pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster (new pcl::PointCloud<pcl::PointXYZ>);
00468           for(pit = it->indices.begin(); pit != it->indices.end(); pit++) 
00469           {
00470           
00471                   cloud_cluster->points.push_back(input_cloud->points[*pit]);
00472                   //dist_this_point = pcl::geometry::distance(input_cloud->points[*pit],
00473                   //                                          origin);
00474                   //mindist_this_cluster = std::min(dist_this_point, mindist_this_cluster);
00475           }
00476 
00477          
00478       cluster_vec.push_back(cloud_cluster);
00479 
00480     }
00481     //cout<<"cluster_vec got some clusters\n";
00482 
00483     //Ensure at least 6 clusters exist to publish (later clusters may be empty)
00484     while (cluster_vec.size() < 6){
00485       pcl::PointCloud<pcl::PointXYZ>::Ptr empty_cluster (new pcl::PointCloud<pcl::PointXYZ>);
00486       empty_cluster->points.push_back(pcl::PointXYZ(0,0,0));
00487       cluster_vec.push_back(empty_cluster);
00488     }
00489     std_msgs::Float32MultiArray cc;
00490     for(int i=0;i<6;i++)
00491     {
00492         cc.data.push_back(cluster_vec[i]->points[cluster_vec[i]->points.size()/2].x);
00493         cc.data.push_back(cluster_vec[i]->points[cluster_vec[i]->points.size()/2].y);
00494         cc.data.push_back(cluster_vec[i]->points[cluster_vec[i]->points.size()/2].z);
00495 
00496     }
00497     cout<<"6 clusters initialized\n";
00498 
00499     //cc_pos.publish(cc);// Publish cluster mid-points.
00500     KFT(cc);
00501     int i=0;
00502     bool publishedCluster[6];
00503     for(auto it=objID.begin();it!=objID.end();it++)
00504         { cout<<"Inside the for loop\n";
00505             
00506             switch(*it)
00507             {
00508                 cout<<"Inside the switch case\n";
00509                 case 0: { 
00510                             publish_cloud(pub_cluster0,cluster_vec[i]);
00511                             publishedCluster[i]=true;//Use this flag to publish only once for a given obj ID
00512                             i++;
00513                             break;
00514 
00515                         }
00516                 case 1: {
00517                             publish_cloud(pub_cluster1,cluster_vec[i]);
00518                             publishedCluster[i]=true;//Use this flag to publish only once for a given obj ID
00519                             i++;
00520                             break;
00521 
00522                         }
00523                 case 2: {
00524                             publish_cloud(pub_cluster2,cluster_vec[i]);
00525                             publishedCluster[i]=true;//Use this flag to publish only once for a given obj ID
00526                             i++;
00527                             break;
00528 
00529                         }
00530                 case 3: {
00531                             publish_cloud(pub_cluster3,cluster_vec[i]);
00532                             publishedCluster[i]=true;//Use this flag to publish only once for a given obj ID
00533                             i++;
00534                             break;
00535 
00536                         }
00537                 case 4: {
00538                             publish_cloud(pub_cluster4,cluster_vec[i]);
00539                             publishedCluster[i]=true;//Use this flag to publish only once for a given obj ID
00540                             i++;
00541                             break;
00542 
00543                         }
00544 
00545                 case 5: {
00546                             publish_cloud(pub_cluster5,cluster_vec[i]);
00547                             publishedCluster[i]=true;//Use this flag to publish only once for a given obj ID
00548                             i++;
00549                             break;
00550 
00551                         }
00552                 default: break;
00553             }
00554     
00555         }
00556 
00557 } 
00558 
00559 }   
00560 
00561 
00562   
00563 
00564 int main(int argc, char** argv)
00565 {
00566     // ROS init
00567     ros::init (argc,argv,"KFTracker");
00568     ros::NodeHandle nh;
00569 
00570    
00571     // Publishers to publish the state of the objects (pos and vel)
00572     //objState1=nh.advertise<geometry_msgs::Twist> ("obj_1",1);
00573 
00574 
00575 
00576 cout<<"About to setup callback\n";
00577 
00578 // Create a ROS subscriber for the input point cloud
00579   ros::Subscriber sub = nh.subscribe ("filtered_cloud", 1, cloud_cb);
00580   // Create a ROS publisher for the output point cloud
00581   pub_cluster0 = nh.advertise<sensor_msgs::PointCloud2> ("cluster_0", 1);
00582   pub_cluster1 = nh.advertise<sensor_msgs::PointCloud2> ("cluster_1", 1);
00583   pub_cluster2 = nh.advertise<sensor_msgs::PointCloud2> ("cluster_2", 1);
00584   pub_cluster3 = nh.advertise<sensor_msgs::PointCloud2> ("cluster_3", 1);
00585   pub_cluster4 = nh.advertise<sensor_msgs::PointCloud2> ("cluster_4", 1);
00586   pub_cluster5 = nh.advertise<sensor_msgs::PointCloud2> ("cluster_5", 1);
00587       // Subscribe to the clustered pointclouds
00588     //ros::Subscriber c1=nh.subscribe("ccs",100,KFT); 
00589     objID_pub = nh.advertise<std_msgs::Int32MultiArray>("obj_id", 1);
00590 /* Point cloud clustering
00591 */
00592     
00593   //cc_pos=nh.advertise<std_msgs::Float32MultiArray>("ccs",100);//clusterCenter1
00594 
00595 
00596 /* Point cloud clustering
00597 */    
00598 
00599 
00600     ros::spin();
00601 
00602 
00603 }


multi_object_tracking_lidar
Author(s): Praveen Palanisamy
autogenerated on Thu Apr 18 2019 02:40:56