3 #include "opencv2/video/tracking.hpp"
7 #include <geometry_msgs/Point.h>
10 #include <opencv2/core/core.hpp>
11 #include <opencv2/highgui/highgui.hpp>
12 #include <opencv2/imgproc/imgproc.hpp>
13 #include <opencv2/video/video.hpp>
14 #include <pcl/io/pcd_io.h>
15 #include <pcl/point_types.h>
17 #include <std_msgs/Float32MultiArray.h>
18 #include <std_msgs/Int32MultiArray.h>
21 #include <pcl/common/centroid.h>
22 #include <pcl/common/geometry.h>
23 #include <pcl/features/normal_3d.h>
24 #include <pcl/filters/extract_indices.h>
25 #include <pcl/filters/voxel_grid.h>
26 #include <pcl/kdtree/kdtree.h>
27 #include <pcl/point_cloud.h>
28 #include <pcl/point_types.h>
29 #include <pcl/sample_consensus/method_types.h>
30 #include <pcl/sample_consensus/model_types.h>
31 #include <pcl/segmentation/extract_clusters.h>
32 #include <pcl/segmentation/sac_segmentation.h>
34 #include <sensor_msgs/PointCloud2.h>
38 #include <visualization_msgs/Marker.h>
39 #include <visualization_msgs/MarkerArray.h>
78 return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y) +
79 (p1.z - p2.z) * (p1.z - p2.z));
106 cout <<
"findIndexOfMin cALLED\n";
107 std::pair<int, int> minIndex;
108 float minEl = std::numeric_limits<float>::max();
109 cout <<
"minEl=" << minEl <<
"\n";
110 for (
int i = 0; i < distMat.size(); i++)
111 for (
int j = 0; j < distMat.at(0).size(); j++) {
112 if (distMat[i][j] < minEl) {
113 minEl = distMat[i][j];
114 minIndex = std::make_pair(i, j);
117 cout <<
"minIndex=" << minIndex.first <<
"," << minIndex.second <<
"\n";
120 void KFT(
const std_msgs::Float32MultiArray ccs) {
124 std::vector<cv::Mat> pred{
KF0.predict(),
KF1.predict(),
KF2.predict(),
125 KF3.predict(),
KF4.predict(),
KF5.predict()};
135 std::vector<geometry_msgs::Point> clusterCenters;
138 for (std::vector<float>::const_iterator it = ccs.data.begin();
139 it != ccs.data.end(); it += 3) {
140 geometry_msgs::Point pt;
145 clusterCenters.push_back(pt);
149 std::vector<geometry_msgs::Point> KFpredictions;
151 for (
auto it = pred.begin(); it != pred.end(); it++) {
152 geometry_msgs::Point pt;
153 pt.x = (*it).at<
float>(0);
154 pt.y = (*it).at<
float>(1);
155 pt.z = (*it).at<
float>(2);
157 KFpredictions.push_back(pt);
167 std::vector<geometry_msgs::Point> copyOfClusterCenters(clusterCenters);
168 std::vector<std::vector<float>> distMat;
170 for (
int filterN = 0; filterN < 6; filterN++) {
171 std::vector<float> distVec;
172 for (
int n = 0; n < 6; n++) {
177 distMat.push_back(distVec);
188 cout <<
"filterN=" << filterN <<
"\n";
191 cout <<
"distMat.size()" << distMat.size() <<
"\n";
192 cout <<
"distMat[0].size()" << distMat.at(0).size() <<
"\n";
194 for (
const auto &row : distMat) {
195 for (
const auto &
s : row)
196 std::cout <<
s <<
' ';
197 std::cout << std::endl;
200 for (
int clusterCount = 0; clusterCount < 6; clusterCount++) {
203 cout <<
"Received minIndex=" << minIndex.first <<
"," << minIndex.second
206 objID[minIndex.first] = minIndex.second;
209 distMat[minIndex.first] =
210 std::vector<float>(6, 10000.0);
211 for (
int row = 0; row < distMat.size();
214 distMat[row][minIndex.second] = 10000.0;
217 cout <<
"clusterCount=" << clusterCount <<
"\n";
231 visualization_msgs::MarkerArray clusterMarkers;
233 for (
int i = 0; i < 6; i++) {
234 visualization_msgs::Marker m;
237 m.type = visualization_msgs::Marker::CUBE;
238 m.header.frame_id =
"map";
242 m.action = visualization_msgs::Marker::ADD;
244 m.color.r = i % 2 ? 1 : 0;
245 m.color.g = i % 3 ? 1 : 0;
246 m.color.b = i % 4 ? 1 : 0;
249 geometry_msgs::Point clusterC(KFpredictions[i]);
250 m.pose.position.x = clusterC.x;
251 m.pose.position.y = clusterC.y;
252 m.pose.position.z = clusterC.z;
254 clusterMarkers.markers.push_back(m);
261 std_msgs::Int32MultiArray obj_id;
262 for (
auto it =
objID.begin(); it !=
objID.end(); it++)
263 obj_id.data.push_back(*it);
267 std::vector<std::vector<float>> cc;
268 for (
int i = 0; i < 6; i++) {
270 pt.push_back(clusterCenters[
objID[i]].x);
271 pt.push_back(clusterCenters[
objID[i]].y);
272 pt.push_back(clusterCenters[
objID[i]].z);
277 float meas0[2] = {cc[0].at(0), cc[0].at(1)};
278 float meas1[2] = {cc[1].at(0), cc[1].at(1)};
279 float meas2[2] = {cc[2].at(0), cc[2].at(1)};
280 float meas3[2] = {cc[3].at(0), cc[3].at(1)};
281 float meas4[2] = {cc[4].at(0), cc[4].at(1)};
282 float meas5[2] = {cc[5].at(0), cc[5].at(1)};
285 cv::Mat meas0Mat = cv::Mat(2, 1, CV_32F, meas0);
286 cv::Mat meas1Mat = cv::Mat(2, 1, CV_32F, meas1);
287 cv::Mat meas2Mat = cv::Mat(2, 1, CV_32F, meas2);
288 cv::Mat meas3Mat = cv::Mat(2, 1, CV_32F, meas3);
289 cv::Mat meas4Mat = cv::Mat(2, 1, CV_32F, meas4);
290 cv::Mat meas5Mat = cv::Mat(2, 1, CV_32F, meas5);
293 if (!(meas0Mat.at<
float>(0, 0) == 0.0f || meas0Mat.at<
float>(1, 0) == 0.0f))
294 Mat estimated0 =
KF0.correct(meas0Mat);
295 if (!(meas1[0] == 0.0
f || meas1[1] == 0.0
f))
296 Mat estimated1 =
KF1.correct(meas1Mat);
297 if (!(meas2[0] == 0.0
f || meas2[1] == 0.0
f))
298 Mat estimated2 =
KF2.correct(meas2Mat);
299 if (!(meas3[0] == 0.0
f || meas3[1] == 0.0
f))
300 Mat estimated3 =
KF3.correct(meas3Mat);
301 if (!(meas4[0] == 0.0
f || meas4[1] == 0.0
f))
302 Mat estimated4 =
KF4.correct(meas4Mat);
303 if (!(meas5[0] == 0.0
f || meas5[1] == 0.0
f))
304 Mat estimated5 =
KF5.correct(meas5Mat);
313 pcl::PointCloud<pcl::PointXYZ>::Ptr cluster) {
314 sensor_msgs::PointCloud2::Ptr clustermsg(
new sensor_msgs::PointCloud2);
316 clustermsg->header.frame_id =
"map";
321 void cloud_cb(
const sensor_msgs::PointCloud2ConstPtr &input)
336 KF0.transitionMatrix = (Mat_<float>(4, 4) << dx, 0, 1, 0, 0, dy, 0, 1, 0, 0,
337 dvx, 0, 0, 0, 0, dvy);
338 KF1.transitionMatrix = (Mat_<float>(4, 4) << dx, 0, 1, 0, 0, dy, 0, 1, 0, 0,
339 dvx, 0, 0, 0, 0, dvy);
340 KF2.transitionMatrix = (Mat_<float>(4, 4) << dx, 0, 1, 0, 0, dy, 0, 1, 0, 0,
341 dvx, 0, 0, 0, 0, dvy);
342 KF3.transitionMatrix = (Mat_<float>(4, 4) << dx, 0, 1, 0, 0, dy, 0, 1, 0, 0,
343 dvx, 0, 0, 0, 0, dvy);
344 KF4.transitionMatrix = (Mat_<float>(4, 4) << dx, 0, 1, 0, 0, dy, 0, 1, 0, 0,
345 dvx, 0, 0, 0, 0, dvy);
346 KF5.transitionMatrix = (Mat_<float>(4, 4) << dx, 0, 1, 0, 0, dy, 0, 1, 0, 0,
347 dvx, 0, 0, 0, 0, dvy);
349 cv::setIdentity(
KF0.measurementMatrix);
350 cv::setIdentity(
KF1.measurementMatrix);
351 cv::setIdentity(
KF2.measurementMatrix);
352 cv::setIdentity(
KF3.measurementMatrix);
353 cv::setIdentity(
KF4.measurementMatrix);
354 cv::setIdentity(
KF5.measurementMatrix);
371 cv::setIdentity(
KF0.measurementNoiseCov, cv::Scalar(sigmaQ));
372 cv::setIdentity(
KF1.measurementNoiseCov, cv::Scalar(sigmaQ));
373 cv::setIdentity(
KF2.measurementNoiseCov, cv::Scalar(sigmaQ));
374 cv::setIdentity(
KF3.measurementNoiseCov, cv::Scalar(sigmaQ));
375 cv::setIdentity(
KF4.measurementNoiseCov, cv::Scalar(sigmaQ));
376 cv::setIdentity(
KF5.measurementNoiseCov, cv::Scalar(sigmaQ));
379 pcl::PointCloud<pcl::PointXYZ>::Ptr input_cloud(
380 new pcl::PointCloud<pcl::PointXYZ>);
381 pcl::PointCloud<pcl::PointXYZ>::Ptr clustered_cloud(
382 new pcl::PointCloud<pcl::PointXYZ>);
384 pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(
385 new pcl::search::KdTree<pcl::PointXYZ>);
389 tree->setInputCloud(input_cloud);
391 std::vector<pcl::PointIndices> cluster_indices;
392 pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
393 ec.setClusterTolerance(0.08);
394 ec.setMinClusterSize(10);
395 ec.setMaxClusterSize(600);
396 ec.setSearchMethod(tree);
397 ec.setInputCloud(input_cloud);
399 ec.extract(cluster_indices);
401 std::vector<pcl::PointIndices>::const_iterator it;
402 std::vector<int>::const_iterator pit;
404 std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr> cluster_vec;
406 std::vector<pcl::PointXYZ> clusterCentroids;
408 for (it = cluster_indices.begin(); it != cluster_indices.end(); ++it) {
410 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster(
411 new pcl::PointCloud<pcl::PointXYZ>);
415 for (pit = it->indices.begin(); pit != it->indices.end(); pit++) {
417 cloud_cluster->points.push_back(input_cloud->points[*pit]);
418 x += input_cloud->points[*pit].x;
419 y += input_cloud->points[*pit].y;
428 pcl::PointXYZ centroid;
429 centroid.x = x / numPts;
430 centroid.y = y / numPts;
433 cluster_vec.push_back(cloud_cluster);
436 clusterCentroids.push_back(centroid);
440 while (cluster_vec.size() < 6) {
441 pcl::PointCloud<pcl::PointXYZ>::Ptr empty_cluster(
442 new pcl::PointCloud<pcl::PointXYZ>);
443 empty_cluster->points.push_back(pcl::PointXYZ(0, 0, 0));
444 cluster_vec.push_back(empty_cluster);
447 while (clusterCentroids.size() < 6) {
448 pcl::PointXYZ centroid;
453 clusterCentroids.push_back(centroid);
457 KF0.statePre.at<
float>(0) = clusterCentroids.at(0).x;
458 KF0.statePre.at<
float>(1) = clusterCentroids.at(0).y;
459 KF0.statePre.at<
float>(2) = 0;
460 KF0.statePre.at<
float>(3) = 0;
463 KF1.statePre.at<
float>(0) = clusterCentroids.at(1).x;
464 KF1.statePre.at<
float>(1) = clusterCentroids.at(1).y;
465 KF1.statePre.at<
float>(2) = 0;
466 KF1.statePre.at<
float>(3) = 0;
469 KF2.statePre.at<
float>(0) = clusterCentroids.at(2).x;
470 KF2.statePre.at<
float>(1) = clusterCentroids.at(2).y;
471 KF2.statePre.at<
float>(2) = 0;
472 KF2.statePre.at<
float>(3) = 0;
475 KF3.statePre.at<
float>(0) = clusterCentroids.at(3).x;
476 KF3.statePre.at<
float>(1) = clusterCentroids.at(3).y;
477 KF3.statePre.at<
float>(2) = 0;
478 KF3.statePre.at<
float>(3) = 0;
481 KF4.statePre.at<
float>(0) = clusterCentroids.at(4).x;
482 KF4.statePre.at<
float>(1) = clusterCentroids.at(4).y;
483 KF4.statePre.at<
float>(2) = 0;
484 KF4.statePre.at<
float>(3) = 0;
487 KF5.statePre.at<
float>(0) = clusterCentroids.at(5).x;
488 KF5.statePre.at<
float>(1) = clusterCentroids.at(5).y;
489 KF5.statePre.at<
float>(2) = 0;
490 KF5.statePre.at<
float>(3) = 0;
494 for (
int i = 0; i < 6; i++) {
495 geometry_msgs::Point pt;
496 pt.x = clusterCentroids.at(i).x;
497 pt.y = clusterCentroids.at(i).y;
515 pcl::PointCloud<pcl::PointXYZ>::Ptr input_cloud(
516 new pcl::PointCloud<pcl::PointXYZ>);
517 pcl::PointCloud<pcl::PointXYZ>::Ptr clustered_cloud(
518 new pcl::PointCloud<pcl::PointXYZ>);
520 pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(
521 new pcl::search::KdTree<pcl::PointXYZ>);
525 tree->setInputCloud(input_cloud);
533 std::vector<pcl::PointIndices> cluster_indices;
534 pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
535 ec.setClusterTolerance(0.3);
536 ec.setMinClusterSize(10);
537 ec.setMaxClusterSize(600);
538 ec.setSearchMethod(tree);
539 ec.setInputCloud(input_cloud);
542 ec.extract(cluster_indices);
552 std::vector<pcl::PointIndices>::const_iterator it;
553 std::vector<int>::const_iterator pit;
555 std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr> cluster_vec;
558 std::vector<pcl::PointXYZ> clusterCentroids;
560 for (it = cluster_indices.begin(); it != cluster_indices.end(); ++it) {
564 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster(
565 new pcl::PointCloud<pcl::PointXYZ>);
566 for (pit = it->indices.begin(); pit != it->indices.end(); pit++) {
568 cloud_cluster->points.push_back(input_cloud->points[*pit]);
570 x += input_cloud->points[*pit].x;
571 y += input_cloud->points[*pit].y;
580 pcl::PointXYZ centroid;
581 centroid.x = x / numPts;
582 centroid.y = y / numPts;
585 cluster_vec.push_back(cloud_cluster);
588 clusterCentroids.push_back(centroid);
593 while (cluster_vec.size() < 6) {
594 pcl::PointCloud<pcl::PointXYZ>::Ptr empty_cluster(
595 new pcl::PointCloud<pcl::PointXYZ>);
596 empty_cluster->points.push_back(pcl::PointXYZ(0, 0, 0));
597 cluster_vec.push_back(empty_cluster);
600 while (clusterCentroids.size() < 6) {
601 pcl::PointXYZ centroid;
606 clusterCentroids.push_back(centroid);
609 std_msgs::Float32MultiArray cc;
610 for (
int i = 0; i < 6; i++) {
611 cc.data.push_back(clusterCentroids.at(i).x);
612 cc.data.push_back(clusterCentroids.at(i).y);
613 cc.data.push_back(clusterCentroids.at(i).z);
620 bool publishedCluster[6];
621 for (
auto it =
objID.begin(); it !=
objID.end();
625 cout <<
"Inside the switch case\n";
628 publishedCluster[i] =
635 publishedCluster[i] =
642 publishedCluster[i] =
649 publishedCluster[i] =
656 publishedCluster[i] =
664 publishedCluster[i] =
676 int main(
int argc,
char **argv) {
684 cout <<
"About to setup callback\n";