Apollo  6.0
Open source self driving car software
spp_label_image.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 #pragma once
17 
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
22 #include "gtest/gtest_prod.h"
23 
24 #include "cyber/common/macros.h"
27 
28 namespace apollo {
29 namespace perception {
30 namespace lidar {
31 
33  public:
34  SppLabelImage() { clusters_.reserve(kDefaultReserveSize); }
36  if (labels_) {
37  common::IFree2(&labels_);
38  }
39  if (range_mask_) {
40  common::IFree2(&range_mask_);
41  }
42  }
43  // @brief: initialize label image
44  // @param [in]: image width
45  // @param [in]: image height
46  // @param [in]: sensor name
47  void Init(size_t width, size_t height,
48  const std::string& sensor_name = "velodyne64");
49  // @brief: initialize range mask of label image
50  // @param [in]: range
51  // @param [in]: boundary distance for mask
52  void InitRangeMask(float range, float boundary_distance);
53  // @brief: transform clusters to label image
55  // @brief: transform label image to clusters
57  // @brief: filter clusters, given confidence map
58  // @param [in]: confidence_map of the same size
59  // @param [in]: confidence threshold
60  void FilterClusters(const float* confidence_map, float threshold);
61  // @brief: filter clusters, given confidence and category map
62  // @param [in]: confidence_map of the same size
63  // @param [in]: category_map of the same size
64  // @param [in]: confidence threshold
65  // @param [in]: category threshold
66  void FilterClusters(const float* confidence_map, const float* category_map,
67  float confidence_threshold, float category_threshold);
68  // @brief: calculate class for each cluster, given class map
69  // @param [in]: class_map of the same size
70  // @param [in]: class number
71  void CalculateClusterClass(const float* class_map, size_t class_num);
72  // @brief: calculate heading (yaw) for each cluster, given heading map
73  // @param [in]: heading_map of the same size
74  void CalculateClusterHeading(const float* heading_map);
75  // @brief: calculate top_z for each cluster, given top_z map
76  // @param [in]: top_z_map of the same size
77  void CalculateClusterTopZ(const float* top_z_map);
78  // @brief: get label image
79  // @return: label image
80  uint16_t** GetSppLabelImage() { return labels_; }
81  // @brief: get label image, const version
82  // @return: const label image
83  const uint16_t* const* GetSppLabelImage() const { return labels_; }
84  // @brief: get clusters
85  // @return: clusters
86  inline std::vector<SppClusterPtr>& GetClusters() { return clusters_; }
87  // @brief: get clusters, const version
88  // @return: const clusters
89  inline const std::vector<SppClusterPtr>& GetClusters() const {
90  return clusters_;
91  }
92  // @brief: get label row pointer
93  // @param [in]: row id
94  // @return: row pointer
95  uint16_t* operator[](size_t id) { return labels_[id]; }
96  // @brief: get label row pointer, const version
97  // @param [in]: row id
98  // @return: row pointer
99  const uint16_t* operator[](size_t id) const { return labels_[id]; }
100  // @brief: get cluster number
101  // @return: cluster number
102  size_t GetClusterNum() const { return clusters_.size(); }
103  // @brief: get cluster given id
104  // @param [in]: cluster id
105  // @return: cluster pointer
106  SppClusterPtr GetCluster(size_t id) { return clusters_[id]; }
107  // @brief: get cluster given id, const version
108  // @param [in]: cluster id
109  // @return: cluster pointer
110  SppClusterConstPtr GetCluster(size_t id) const { return clusters_[id]; }
111  // @brief: add a pixel to labeled cluster
112  // @param [in]: label id
113  // @param [in]: pixel x
114  // @param [in]: pixel y
115  void AddPixelSample(size_t id, uint16_t x, uint16_t y) {
116  return AddPixelSample(id, y * static_cast<uint32_t>(width_) + x);
117  }
118  // @brief: add a pixel to labeled cluster
119  // @param [in]: label id
120  // @param [in]: pixel id
121  void AddPixelSample(size_t id, uint32_t pixel);
122  // @brief: clear clusters
123  inline void ClearClusters() { clusters_.clear(); }
124  // @brief: resize clusters
125  // @param [in]: target cluster size
126  void ResizeClusters(size_t size);
127  // @brief: reset clusters
128  // @param [in]: target cluster size
129  void ResetClusters(size_t size);
130 
131  private:
132  // note the correspondence between label and cluster id is
133  // label - 1 == cluster id, label zero is reserved for background
134  uint16_t** labels_ = nullptr;
135  size_t width_ = 0;
136  size_t height_ = 0;
137  char** range_mask_ = nullptr;
138  std::vector<SppClusterPtr> clusters_;
139  std::string sensor_name_;
140 
141  private:
142  static const size_t kDefaultReserveSize = 500;
143 
144  FRIEND_TEST(SppClusterTest, spp_cluster_test);
145  DISALLOW_COPY_AND_ASSIGN(SppLabelImage);
146 };
147 
148 typedef std::shared_ptr<SppLabelImage> SppLabelImagePtr;
149 typedef std::shared_ptr<const SppLabelImage> SppLabelImageConstPtr;
150 
151 } // namespace lidar
152 } // namespace perception
153 } // namespace apollo
void FilterClusters(const float *confidence_map, float threshold)
void CalculateClusterHeading(const float *heading_map)
void CalculateClusterClass(const float *class_map, size_t class_num)
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
const uint16_t *const * GetSppLabelImage() const
Definition: spp_label_image.h:83
size_t GetClusterNum() const
Definition: spp_label_image.h:102
const uint16_t * operator[](size_t id) const
Definition: spp_label_image.h:99
void AddPixelSample(size_t id, uint16_t x, uint16_t y)
Definition: spp_label_image.h:115
~SppLabelImage()
Definition: spp_label_image.h:35
std::shared_ptr< const SppCluster > SppClusterConstPtr
Definition: spp_cluster.h:132
void ClearClusters()
Definition: spp_label_image.h:123
SppClusterConstPtr GetCluster(size_t id) const
Definition: spp_label_image.h:110
uint16_t * operator[](size_t id)
Definition: spp_label_image.h:95
const std::vector< SppClusterPtr > & GetClusters() const
Definition: spp_label_image.h:89
uint16_t ** GetSppLabelImage()
Definition: spp_label_image.h:80
void CalculateClusterTopZ(const float *top_z_map)
void IFree2(T ***A)
Definition: i_alloc.h:79
Definition: spp_label_image.h:32
SppLabelImage()
Definition: spp_label_image.h:34
void Init(size_t width, size_t height, const std::string &sensor_name="velodyne64")
std::shared_ptr< SppCluster > SppClusterPtr
Definition: spp_cluster.h:131
std::shared_ptr< const SppLabelImage > SppLabelImageConstPtr
Definition: spp_label_image.h:149
SppClusterPtr GetCluster(size_t id)
Definition: spp_label_image.h:106
void InitRangeMask(float range, float boundary_distance)
Image< uchar > * threshold(Image< T > *src, int t)
Definition: imutil.h:63
std::vector< SppClusterPtr > & GetClusters()
Definition: spp_label_image.h:86
std::shared_ptr< SppLabelImage > SppLabelImagePtr
Definition: spp_label_image.h:148