| 100 | |
| 101 | |
| 102 | std::vector<bool> |
| 103 | maskForegroundPoints (const PointCloudXYZRGBA::ConstPtr & input, |
| 104 | float min_depth, float max_depth, float max_height) |
| 105 | { |
| 106 | std::vector<bool> foreground_mask (input->size (), false); |
| 107 | |
| 108 | // Mask off points outside the specified near and far depth thresholds |
| 109 | pcl::IndicesPtr indices (new pcl::Indices); |
| 110 | for (std::size_t i = 0; i < input->size (); ++i) |
| 111 | { |
| 112 | const float z = (*input)[i].z; |
| 113 | if (min_depth < z && z < max_depth) |
| 114 | { |
| 115 | foreground_mask[i] = true; |
| 116 | indices->push_back (static_cast<int> (i)); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // Find the dominant plane between the specified near/far thresholds |
| 121 | constexpr float distance_threshold = 0.02f; |
| 122 | constexpr int max_iterations = 500; |
| 123 | pcl::SACSegmentation<pcl::PointXYZRGBA> seg; |
| 124 | seg.setOptimizeCoefficients (true); |
| 125 | seg.setModelType (pcl::SACMODEL_PLANE); |
| 126 | seg.setMethodType (pcl::SAC_RANSAC); |
| 127 | seg.setDistanceThreshold (distance_threshold); |
| 128 | seg.setMaxIterations (max_iterations); |
| 129 | seg.setInputCloud (input); |
| 130 | seg.setIndices (indices); |
| 131 | pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients ()); |
| 132 | pcl::PointIndices::Ptr inliers (new pcl::PointIndices ()); |
| 133 | seg.segment (*inliers, *coefficients); |
| 134 | |
| 135 | // Mask off the plane inliers |
| 136 | for (const auto &index : inliers->indices) |
| 137 | foreground_mask[index] = false; |
| 138 | |
| 139 | // Mask off any foreground points that are too high above the detected plane |
| 140 | const std::vector<float> & c = coefficients->values; |
| 141 | for (std::size_t i = 0; i < input->size (); ++i) |
| 142 | { |
| 143 | if (foreground_mask[i]) |
| 144 | { |
| 145 | const pcl::PointXYZRGBA & p = (*input)[i]; |
| 146 | float d = std::abs (c[0]*p.x + c[1]*p.y + c[2]*p.z + c[3]); |
| 147 | foreground_mask[i] = (d < max_height); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return (foreground_mask); |
| 152 | } |
| 153 | |
| 154 | void |
| 155 | trainTemplate (const PointCloudXYZRGBA::ConstPtr & input, const std::vector<bool> &foreground_mask, |
no test coverage detected