8 #include <opencv2/core/core.hpp> 9 #include <opencv2/highgui/highgui.hpp> 10 #include <opencv2/imgproc/imgproc.hpp> 11 #include <opencv2/video/video.hpp> 12 #include "opencv2/video/tracking.hpp" 14 #include <pcl/io/pcd_io.h> 15 #include <pcl/point_types.h> 17 #include <geometry_msgs/Point.h> 18 #include <std_msgs/Float32MultiArray.h> 19 #include <std_msgs/Int32MultiArray.h> 21 #include <sensor_msgs/PointCloud2.h> 23 #include <pcl/point_cloud.h> 24 #include <pcl/point_types.h> 25 #include <pcl/common/geometry.h> 26 #include <pcl/filters/extract_indices.h> 27 #include <pcl/filters/voxel_grid.h> 28 #include <pcl/features/normal_3d.h> 29 #include <pcl/kdtree/kdtree.h> 30 #include <pcl/sample_consensus/method_types.h> 31 #include <pcl/sample_consensus/model_types.h> 32 #include <pcl/segmentation/sac_segmentation.h> 33 #include <pcl/segmentation/extract_clusters.h> 73 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));
95 void KFT(
const std_msgs::Float32MultiArray ccs)
102 std::vector<cv::Mat> pred{
KF0.predict(),
KF1.predict(),
KF2.predict(),
KF3.predict(),
KF4.predict(),
KF5.predict()};
111 std::vector<geometry_msgs::Point> clusterCenters;
114 for (std::vector<float>::const_iterator it=ccs.data.begin();it!=ccs.data.end();it+=3)
116 geometry_msgs::Point pt;
121 clusterCenters.push_back(pt);
126 std::vector<geometry_msgs::Point> KFpredictions;
128 for (
auto it=pred.begin();it!=pred.end();it++)
130 geometry_msgs::Point pt;
131 pt.x=(*it).at<
float>(0);
132 pt.y=(*it).at<
float>(1);
133 pt.z=(*it).at<
float>(2);
135 KFpredictions.push_back(pt);
170 for(
int filterN=0;filterN<6;filterN++)
172 std::vector<float> distVec;
177 objID.push_back(std::distance(distVec.begin(),min_element(distVec.begin(),distVec.end())));
184 for (
int i=0;i<6;i++)
195 std_msgs::Int32MultiArray obj_id;
196 for(
auto it=
objID.begin();it!=
objID.end();it++)
197 obj_id.data.push_back(*it);
200 std::vector<std::vector<float> > cc;
201 for (
int i=0;i<clusterCenters.size();i++)
204 pt.push_back(clusterCenters[i].
x);
205 pt.push_back(clusterCenters[i].
y);
206 pt.push_back(clusterCenters[i].
z);
211 float meas0[3]={cc[0].at(0),cc[0].at(1)};
212 float meas1[3]={cc[1].at(0),cc[1].at(1)};
213 float meas2[3]={cc[2].at(0),cc[2].at(1)};
214 float meas3[3]={cc[3].at(0),cc[3].at(1)};
215 float meas4[3]={cc[4].at(0),cc[4].at(1)};
216 float meas5[3]={cc[5].at(0),cc[5].at(1)};
221 cv::Mat meas0Mat=cv::Mat(2,1,CV_32F,meas0);
222 cv::Mat meas1Mat=cv::Mat(2,1,CV_32F,meas1);
223 cv::Mat meas2Mat=cv::Mat(2,1,CV_32F,meas2);
224 cv::Mat meas3Mat=cv::Mat(2,1,CV_32F,meas3);
225 cv::Mat meas4Mat=cv::Mat(2,1,CV_32F,meas4);
226 cv::Mat meas5Mat=cv::Mat(2,1,CV_32F,meas5);
230 Mat estimated0 =
KF0.correct(meas0Mat);
231 Mat estimated1 =
KF0.correct(meas1Mat);
232 Mat estimated2 =
KF0.correct(meas2Mat);
233 Mat estimated3 =
KF0.correct(meas3Mat);
234 Mat estimated4 =
KF0.correct(meas4Mat);
235 Mat estimated5 =
KF0.correct(meas5Mat);
246 sensor_msgs::PointCloud2::Ptr clustermsg (
new sensor_msgs::PointCloud2);
248 clustermsg->header.frame_id =
"/map";
255 void cloud_cb (
const sensor_msgs::PointCloud2ConstPtr& input)
264 KF0.transitionMatrix = *(Mat_<float>(4, 4) << 1,0,1,0, 0,1,0,1, 0,0,1,0, 0,0,0,1);
265 KF1.transitionMatrix = *(Mat_<float>(4, 4) << 1,0,1,0, 0,1,0,1, 0,0,1,0, 0,0,0,1);
266 KF2.transitionMatrix = *(Mat_<float>(4, 4) << 1,0,1,0, 0,1,0,1, 0,0,1,0, 0,0,0,1);
267 KF3.transitionMatrix = *(Mat_<float>(4, 4) << 1,0,1,0, 0,1,0,1, 0,0,1,0, 0,0,0,1);
268 KF4.transitionMatrix = *(Mat_<float>(4, 4) << 1,0,1,0, 0,1,0,1, 0,0,1,0, 0,0,0,1);
269 KF5.transitionMatrix = *(Mat_<float>(4, 4) << 1,0,1,0, 0,1,0,1, 0,0,1,0, 0,0,0,1);
271 cv::setIdentity(
KF0.measurementMatrix);
272 cv::setIdentity(
KF1.measurementMatrix);
273 cv::setIdentity(
KF2.measurementMatrix);
274 cv::setIdentity(
KF3.measurementMatrix);
275 cv::setIdentity(
KF4.measurementMatrix);
276 cv::setIdentity(
KF5.measurementMatrix);
291 cv::setIdentity(
KF0.measurementNoiseCov, cv::Scalar(1e-1));
292 cv::setIdentity(
KF1.measurementNoiseCov, cv::Scalar(1e-1));
293 cv::setIdentity(
KF2.measurementNoiseCov, cv::Scalar(1e-1));
294 cv::setIdentity(
KF3.measurementNoiseCov, cv::Scalar(1e-1));
295 cv::setIdentity(
KF4.measurementNoiseCov, cv::Scalar(1e-1));
296 cv::setIdentity(
KF5.measurementNoiseCov, cv::Scalar(1e-1));
299 pcl::PointCloud<pcl::PointXYZ>::Ptr input_cloud (
new pcl::PointCloud<pcl::PointXYZ>);
300 pcl::PointCloud<pcl::PointXYZ>::Ptr clustered_cloud (
new pcl::PointCloud<pcl::PointXYZ>);
302 pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (
new pcl::search::KdTree<pcl::PointXYZ>);
306 tree->setInputCloud (input_cloud);
313 std::vector<pcl::PointIndices> cluster_indices;
314 pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
315 ec.setClusterTolerance (0.08);
316 ec.setMinClusterSize (10);
317 ec.setMaxClusterSize (600);
318 ec.setSearchMethod (tree);
319 ec.setInputCloud (input_cloud);
321 ec.extract (cluster_indices);
331 std::vector<pcl::PointIndices>::const_iterator it;
332 std::vector<int>::const_iterator pit;
334 std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr > cluster_vec;
336 for(it = cluster_indices.begin(); it != cluster_indices.end(); ++it) {
337 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster (
new pcl::PointCloud<pcl::PointXYZ>);
338 for(pit = it->indices.begin(); pit != it->indices.end(); pit++)
341 cloud_cluster->points.push_back(input_cloud->points[*pit]);
348 cluster_vec.push_back(cloud_cluster);
353 while (cluster_vec.size() < 6){
354 pcl::PointCloud<pcl::PointXYZ>::Ptr empty_cluster (
new pcl::PointCloud<pcl::PointXYZ>);
355 empty_cluster->points.push_back(pcl::PointXYZ(0,0,0));
356 cluster_vec.push_back(empty_cluster);
361 KF0.statePre.at<
float>(0)=cluster_vec[0]->points[cluster_vec[0]->points.size()/2].x;
363 KF0.statePre.at<
float>(1)=cluster_vec[0]->points[cluster_vec[0]->points.size()/2].y;
364 KF0.statePre.at<
float>(2)=0;
365 KF0.statePre.at<
float>(3)=0;
368 KF1.statePre.at<
float>(0)=cluster_vec[1]->points[cluster_vec[1]->points.size()/2].x;
369 KF1.statePre.at<
float>(1)=cluster_vec[1]->points[cluster_vec[1]->points.size()/2].y;
370 KF1.statePre.at<
float>(2)=0;
371 KF1.statePre.at<
float>(3)=0;
374 KF2.statePre.at<
float>(0)=cluster_vec[2]->points[cluster_vec[2]->points.size()/2].x;
375 KF2.statePre.at<
float>(1)=cluster_vec[2]->points[cluster_vec[2]->points.size()/2].y;
376 KF2.statePre.at<
float>(2)=0;
377 KF2.statePre.at<
float>(3)=0;
381 KF3.statePre.at<
float>(0)=cluster_vec[3]->points[cluster_vec[3]->points.size()/2].x;
382 KF3.statePre.at<
float>(1)=cluster_vec[3]->points[cluster_vec[3]->points.size()/2].y;
383 KF3.statePre.at<
float>(2)=0;
384 KF3.statePre.at<
float>(3)=0;
387 KF4.statePre.at<
float>(0)=cluster_vec[4]->points[cluster_vec[4]->points.size()/2].x;
388 KF4.statePre.at<
float>(1)=cluster_vec[4]->points[cluster_vec[4]->points.size()/2].y;
389 KF4.statePre.at<
float>(2)=0;
390 KF4.statePre.at<
float>(3)=0;
393 KF5.statePre.at<
float>(0)=cluster_vec[5]->points[cluster_vec[5]->points.size()/2].x;
394 KF5.statePre.at<
float>(1)=cluster_vec[5]->points[cluster_vec[5]->points.size()/2].y;
395 KF5.statePre.at<
float>(2)=0;
396 KF5.statePre.at<
float>(3)=0;
401 cout<<
"KF0.satePre="<<
KF0.statePre.at<
float>(0)<<
","<<
KF0.statePre.at<
float>(1)<<
"\n";
402 cout<<
"KF1.satePre="<<
KF1.statePre.at<
float>(0)<<
","<<
KF1.statePre.at<
float>(1)<<
"\n";
403 cout<<
"KF2.satePre="<<
KF2.statePre.at<
float>(0)<<
","<<
KF2.statePre.at<
float>(1)<<
"\n";
404 cout<<
"KF3.satePre="<<
KF3.statePre.at<
float>(0)<<
","<<
KF3.statePre.at<
float>(1)<<
"\n";
405 cout<<
"KF4.satePre="<<
KF4.statePre.at<
float>(0)<<
","<<
KF4.statePre.at<
float>(1)<<
"\n";
406 cout<<
"KF5.satePre="<<
KF5.statePre.at<
float>(0)<<
","<<
KF5.statePre.at<
float>(1)<<
"\n";
413 for (
int i=0;i<6;i++)
415 geometry_msgs::Point pt;
416 pt.x=cluster_vec[i]->points[cluster_vec[i]->points.size()/2].x;
417 pt.y=cluster_vec[i]->points[cluster_vec[i]->points.size()/2].y;
427 cout<<
"ELSE firstFrame="<<firstFrame<<
"\n";
428 pcl::PointCloud<pcl::PointXYZ>::Ptr input_cloud (
new pcl::PointCloud<pcl::PointXYZ>);
429 pcl::PointCloud<pcl::PointXYZ>::Ptr clustered_cloud (
new pcl::PointCloud<pcl::PointXYZ>);
431 pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (
new pcl::search::KdTree<pcl::PointXYZ>);
435 tree->setInputCloud (input_cloud);
442 std::vector<pcl::PointIndices> cluster_indices;
443 pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
444 ec.setClusterTolerance (0.08);
445 ec.setMinClusterSize (10);
446 ec.setMaxClusterSize (600);
447 ec.setSearchMethod (tree);
448 ec.setInputCloud (input_cloud);
449 cout<<
"PCL init successfull\n";
451 ec.extract (cluster_indices);
452 cout<<
"PCL extract successfull\n";
461 std::vector<pcl::PointIndices>::const_iterator it;
462 std::vector<int>::const_iterator pit;
464 std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr > cluster_vec;
466 for(it = cluster_indices.begin(); it != cluster_indices.end(); ++it) {
467 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster (
new pcl::PointCloud<pcl::PointXYZ>);
468 for(pit = it->indices.begin(); pit != it->indices.end(); pit++)
471 cloud_cluster->points.push_back(input_cloud->points[*pit]);
478 cluster_vec.push_back(cloud_cluster);
484 while (cluster_vec.size() < 6){
485 pcl::PointCloud<pcl::PointXYZ>::Ptr empty_cluster (
new pcl::PointCloud<pcl::PointXYZ>);
486 empty_cluster->points.push_back(pcl::PointXYZ(0,0,0));
487 cluster_vec.push_back(empty_cluster);
489 std_msgs::Float32MultiArray cc;
492 cc.data.push_back(cluster_vec[i]->points[cluster_vec[i]->points.size()/2].x);
493 cc.data.push_back(cluster_vec[i]->points[cluster_vec[i]->points.size()/2].y);
494 cc.data.push_back(cluster_vec[i]->points[cluster_vec[i]->points.size()/2].z);
497 cout<<
"6 clusters initialized\n";
502 bool publishedCluster[6];
503 for(
auto it=
objID.begin();it!=
objID.end();it++)
504 { cout<<
"Inside the for loop\n";
508 cout<<
"Inside the switch case\n";
511 publishedCluster[i]=
true;
518 publishedCluster[i]=
true;
525 publishedCluster[i]=
true;
532 publishedCluster[i]=
true;
539 publishedCluster[i]=
true;
547 publishedCluster[i]=
true;
564 int main(
int argc,
char** argv)
576 cout<<
"About to setup callback\n";
581 pub_cluster0 = nh.
advertise<sensor_msgs::PointCloud2> (
"cluster_0", 1);
582 pub_cluster1 = nh.
advertise<sensor_msgs::PointCloud2> (
"cluster_1", 1);
583 pub_cluster2 = nh.
advertise<sensor_msgs::PointCloud2> (
"cluster_2", 1);
584 pub_cluster3 = nh.
advertise<sensor_msgs::PointCloud2> (
"cluster_3", 1);
585 pub_cluster4 = nh.
advertise<sensor_msgs::PointCloud2> (
"cluster_4", 1);
586 pub_cluster5 = nh.
advertise<sensor_msgs::PointCloud2> (
"cluster_5", 1);
589 objID_pub = nh.
advertise<std_msgs::Int32MultiArray>(
"obj_id", 1);
void setIdentity(geometry_msgs::Transform &tx)
cv::KalmanFilter KF3(stateDim, measDim, ctrlDim, CV_32F)
cv::KalmanFilter KF0(stateDim, measDim, ctrlDim, CV_32F)
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
int main(int argc, char **argv)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ros::Publisher pub_cluster2
void KFT(const std_msgs::Float32MultiArray ccs)
void fromROSMsg(const sensor_msgs::PointCloud2 &cloud, pcl::PointCloud< T > &pcl_cloud)
TFSIMD_FORCE_INLINE const tfScalar & y() const
ROSCPP_DECL void spin(Spinner &spinner)
cv::KalmanFilter KF4(stateDim, measDim, ctrlDim, CV_32F)
cv::KalmanFilter KF1(stateDim, measDim, ctrlDim, CV_32F)
void cloud_cb(const sensor_msgs::PointCloud2ConstPtr &input)
cv::Mat_< float > measurement(2, 1)
TFSIMD_FORCE_INLINE const tfScalar & x() const
cv::KalmanFilter KF5(stateDim, measDim, ctrlDim, CV_32F)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
cv::Mat state(stateDim, 1, CV_32F)
TFSIMD_FORCE_INLINE const tfScalar & z() const
std::vector< geometry_msgs::Point > prevClusterCenters
ros::Publisher pub_cluster4
void publish_cloud(ros::Publisher &pub, pcl::PointCloud< pcl::PointXYZ >::Ptr cluster)
void toROSMsg(const sensor_msgs::PointCloud2 &cloud, sensor_msgs::Image &image)
ros::Publisher pub_cluster1
cv::KalmanFilter KF2(stateDim, measDim, ctrlDim, CV_32F)
ros::Publisher pub_cluster5
double euclidean_distance(geometry_msgs::Point &p1, geometry_msgs::Point &p2)
ros::Publisher pub_cluster3
ros::Publisher pub_cluster0