forked from zjudmd1015/Mini-3D-Scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcl_processing.cpp
166 lines (130 loc) · 4.93 KB
/
pcl_processing.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <string>
#include <sstream>
#include <ros/ros.h>
#include <sensor_msgs/PointCloud2.h>
#include <std_msgs/Int64.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_cloud.h>
#include <pcl/filters/passthrough.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/filters/extract_indices.h>
#include <Eigen/Eigen>
typedef pcl::PointXYZRGBA PointT;
static int pcd_index = 0;
static char gotDataFlag = 0;// could use a 'class' to reduce this global variable
pcl::PointCloud<PointT>::Ptr
cloud_filter(pcl::PointCloud<PointT>::Ptr &cloud);
void
callback(sensor_msgs::PointCloud2 cloud_raw)
{
// cloud_raw is PC data from Kinect V2;
// static int pcd_index = 0;
pcl::PointCloud<PointT>::Ptr cloud_ptr (new pcl::PointCloud<PointT>);
std::string filename = "/home/dylan2/catkin_ws/src/scanner/data/" + std::to_string(pcd_index) + ".pcd";
ROS_INFO("Processing #%i PointCloud...", pcd_index);
// change PC format from PointCloud2 to pcl::PointCloud<PointT>
pcl::fromROSMsg(cloud_raw, *cloud_ptr);
// crop, segment, filter
cloud_ptr = cloud_filter(cloud_ptr);
// save PCD file to local folder
pcl::io::savePCDFileBinary (filename, *cloud_ptr);
gotDataFlag = 1;
++pcd_index;
}
int
main (int argc, char **argv)
{
ros::init (argc, argv, "pcl_processing");
ros::NodeHandle nh; // can sub and pub use the same NodeHandle?
ros::Subscriber sub = nh.subscribe("/kinect2/qhd/points", 1 , callback);
ros::Publisher pub = nh.advertise<std_msgs::Int64> ("pcd_save_done", 1);
ros::Rate loop_rate(1);
std_msgs::Int64 number_PCDdone;
// std::stringstream ss;
while (ros::ok())
{
/* Do something? */
// ss.str("");
// ss << "have saved pcd #" << pcd_index ;
// msg.data = ss.str();
number_PCDdone.data = pcd_index;
// ros::spin()
//*** only when this is run, it will get to callback
ros::spinOnce();
// only publish data when having got data
if (gotDataFlag == 1){
pub.publish(number_PCDdone);
gotDataFlag = 0;
}
loop_rate.sleep();
}
return 0;
}
pcl::PointCloud<PointT>::Ptr
cloud_filter(pcl::PointCloud<PointT>::Ptr &cloud)
{
pcl::PointCloud<PointT>::Ptr cloud_filtered (new pcl::PointCloud<PointT>);
//****************************************************//
// Create the filtering object - passthrough
pcl::PassThrough<PointT> passz;
passz.setInputCloud (cloud);
passz.setFilterFieldName ("z");
passz.setFilterLimits (0.75, 1.0);
// passz.setFilterLimits (0.5, 1.5);
// passz.setFilterLimits (-2.0, 4.0);
//pass.setFilterLimitsNegative (true);
passz.filter (*cloud_filtered);
pcl::PassThrough<PointT> passy;
passy.setInputCloud (cloud_filtered);
passy.setFilterFieldName ("y");
passy.setFilterLimits (-0.1, 0.22);
// passy.setFilterLimits (-0.5, 0.5);
// passy.setFilterLimits (-2.0, 2.0);
//pass.setFilterLimitsNegative (true);
passy.filter (*cloud_filtered);
pcl::PassThrough<PointT> passx;
passx.setInputCloud (cloud_filtered);
passx.setFilterFieldName ("x");
passx.setFilterLimits (-0.18, 0.18);
// passx.setFilterLimits (-0.5, 0.5);
// passx.setFilterLimits (-3.0, 3.0);
//pass.setFilterLimitsNegative (true);
passx.filter (*cloud_filtered);
//****************************************************//
//****************************************************//
// // segment ground
// pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
// pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
// // Create the segmentation object
// pcl::SACSegmentation<PointT> seg;
// // Optional
// seg.setOptimizeCoefficients (true);
// // Mandatory
// seg.setModelType (pcl::SACMODEL_PLANE); // plane
// seg.setMethodType (pcl::SAC_RANSAC);
// seg.setDistanceThreshold (0.010);
// seg.setInputCloud (cloud_filtered);
// seg.segment (*inliers, *coefficients);
// pcl::ExtractIndices<PointT> extract;
// extract.setInputCloud(cloud_filtered);
// extract.setIndices(inliers);
// extract.setNegative(true);
// extract.filter(*cloud_filtered);
//****************************************************//
//****************************************************//
// Create the filtering object - StatisticalOutlierRemoval filter
pcl::StatisticalOutlierRemoval<PointT> sor;
sor.setInputCloud (cloud_filtered);
sor.setMeanK (50);
sor.setStddevMulThresh (1.0);
sor.filter (*cloud_filtered);
//****************************************************//
// pcl::PointCloud<PointT>::Ptr cloud_write (new pcl::PointCloud<PointT>);
// cloud_write.width = cloud_filtered.points.size();
// cloud_write.height = 1;
// cloud_write.is_dense = false;
return cloud_filtered;
}