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
00042 int stateDim=4;
00043 int measDim=2;
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;
00066
00067
00068 bool firstFrame=true;
00069
00070
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
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 void KFT(const std_msgs::Float32MultiArray ccs)
00096 {
00097
00098
00099
00100
00101
00102 std::vector<cv::Mat> pred{KF0.predict(),KF1.predict(),KF2.predict(),KF3.predict(),KF4.predict(),KF5.predict()};
00103
00104
00105
00106
00107
00108
00109
00110
00111 std::vector<geometry_msgs::Point> 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
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
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169 objID.clear();
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
00177 objID.push_back(std::distance(distVec.begin(),min_element(distVec.begin(),distVec.end())));
00178
00179
00180 }
00181
00182
00183
00184 for (int i=0;i<6;i++)
00185 {
00186 prevClusterCenters[objID.at(i)]=clusterCenters.at(i);
00187
00188 }
00189
00190
00191
00192
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
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
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
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
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
00238
00239
00240
00241
00242
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
00260 if (firstFrame)
00261 {
00262
00263
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
00278
00279
00280
00281
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
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
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
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
00309
00310
00311
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
00321 ec.extract (cluster_indices);
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331 std::vector<pcl::PointIndices>::const_iterator it;
00332 std::vector<int>::const_iterator pit;
00333
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
00343
00344
00345 }
00346
00347
00348 cluster_vec.push_back(cloud_cluster);
00349
00350 }
00351
00352
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
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;
00365 KF0.statePre.at<float>(3)=0;
00366
00367
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;
00371 KF1.statePre.at<float>(3)=0;
00372
00373
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;
00377 KF2.statePre.at<float>(3)=0;
00378
00379
00380
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;
00384 KF3.statePre.at<float>(3)=0;
00385
00386
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;
00390 KF4.statePre.at<float>(3)=0;
00391
00392
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;
00396 KF5.statePre.at<float>(3)=0;
00397
00398 firstFrame=false;
00399
00400
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
00409
00410
00411
00412
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
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
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
00438
00439
00440
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
00451 ec.extract (cluster_indices);
00452 cout<<"PCL extract successfull\n";
00453
00454
00455
00456
00457
00458
00459
00460
00461 std::vector<pcl::PointIndices>::const_iterator it;
00462 std::vector<int>::const_iterator pit;
00463
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
00473
00474
00475 }
00476
00477
00478 cluster_vec.push_back(cloud_cluster);
00479
00480 }
00481
00482
00483
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
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;
00512 i++;
00513 break;
00514
00515 }
00516 case 1: {
00517 publish_cloud(pub_cluster1,cluster_vec[i]);
00518 publishedCluster[i]=true;
00519 i++;
00520 break;
00521
00522 }
00523 case 2: {
00524 publish_cloud(pub_cluster2,cluster_vec[i]);
00525 publishedCluster[i]=true;
00526 i++;
00527 break;
00528
00529 }
00530 case 3: {
00531 publish_cloud(pub_cluster3,cluster_vec[i]);
00532 publishedCluster[i]=true;
00533 i++;
00534 break;
00535
00536 }
00537 case 4: {
00538 publish_cloud(pub_cluster4,cluster_vec[i]);
00539 publishedCluster[i]=true;
00540 i++;
00541 break;
00542
00543 }
00544
00545 case 5: {
00546 publish_cloud(pub_cluster5,cluster_vec[i]);
00547 publishedCluster[i]=true;
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
00567 ros::init (argc,argv,"KFTracker");
00568 ros::NodeHandle nh;
00569
00570
00571
00572
00573
00574
00575
00576 cout<<"About to setup callback\n";
00577
00578
00579 ros::Subscriber sub = nh.subscribe ("filtered_cloud", 1, cloud_cb);
00580
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
00588
00589 objID_pub = nh.advertise<std_msgs::Int32MultiArray>("obj_id", 1);
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600 ros::spin();
00601
00602
00603 }