Apollo  6.0
Open source self driving car software
hough_transfer.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.h>
19 #include <cmath>
20 
21 #include <set>
22 #include <string>
23 #include <vector>
24 
25 namespace apollo {
26 namespace perception {
27 namespace common {
28 
29 struct HoughLine {
30  float r = 0.0f;
31  float theta = 0.0f;
32  float length = 0.0f;
33  int vote_num = 0;
34  std::vector<int> pts;
35 };
36 
38  public:
39  HoughTransfer();
40  ~HoughTransfer() = default;
41 
42  // step1
43  // @brief: initiate
44  // @params[IN] img_w, img_h: width and height of binary image
45  // d_r, d_theta: discretization step of r and theta
46  // in polar coordinates
47  bool Init(int img_w, int img_h, float d_r, float d_theta);
48 
49  // step2
50  // @brief: HoughTransform in 2D binary image
51  // @params[IN] image: 2D binary image.
52  // with_distribute: flag to control whether to calculate element
53  // length,vote_num,pts in HoughLine
54  bool ImageVote(const std::vector<int>& image, bool with_distribute);
55 
56  // @brief: transform one point to parameter space in polar coodinates and vote
57  // @params[IN] x, y: pos in image.
58  // with_distribute: flag to control whether to calculate element
59  // length,vote_num,pts in HoughLine
60  void PointVote(int x, int y, bool with_distribute);
61  // @params[IN] pos = y*img_w + x
62  void PointVote(int pos, bool with_distribute);
63 
64  // step3
65  // @brief get lines
66  // @params[IN] min_pt_num: minimum points on the same line.
67  // r_neibor, theta_neibor: query region
68  // with_distribute: flag to control whether to calculate element
69  // length,vote_num,pts in HoughLine
70  // lines: save lines detected.
71  bool GetLines(int min_pt_num, int r_neibor, int theta_neibor,
72  bool with_distribute, std::vector<HoughLine>* lines) const;
73 
74  unsigned int MemoryConsume() const;
75 
76  // prepared state not change.
77  // when we use hough with with_distribute mode in large image long time,
78  // memory consume maybe too large, so use this func to free no used cache.
79  void FreeCache();
80 
81  void ResetMaps(bool with_distribute);
82 
83  inline std::string name() const { return "HoughTransfer"; }
84 
85  inline bool is_prepared() const { return prepared_; }
86 
87  inline const std::vector<int>& get_vote_map() const { return vote_map_; }
88  inline const std::vector<std::vector<int>>& get_distribute_map() const {
89  return distribute_map_;
90  }
91 
92  inline float get_d_r() const { return d_r_; }
93  inline float get_d_theta() const { return d_theta_; }
94  inline int get_img_w() const { return img_w_; }
95  inline int get_img_h() const { return img_h_; }
96  inline int get_r_size() const { return r_size_; }
97  inline int get_theta_size() const { return theta_size_; }
98 
99  void ClearWithShrink();
100 
101  private:
102  bool CheckPrepared() const;
103 
104  void GetMaxVotes(int min_pt_num, int r_neibor, int theta_neibor, int r_step,
105  int theta_step, std::set<int>* max_vote_lines) const;
106 
107  bool VotePosToHoughLine(int vote_pos, bool with_distribute,
108  HoughLine* out_line) const;
109 
110  bool prepared_;
111  float d_r_;
112  float d_theta_;
113  int img_w_;
114  int img_h_;
115  int r_size_;
116  int theta_size_;
117  int vote_reserve_size_;
118  std::vector<int> vote_map_;
119  std::vector<std::vector<int>> query_map_;
120  std::vector<std::vector<int>> distribute_map_;
121 };
122 
123 } // namespace common
124 } // namespace perception
125 } // namespace apollo
int get_r_size() const
Definition: hough_transfer.h:96
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Definition: hough_transfer.h:29
Definition: hough_transfer.h:37
int get_img_w() const
Definition: hough_transfer.h:94
const std::vector< int > & get_vote_map() const
Definition: hough_transfer.h:87
int get_theta_size() const
Definition: hough_transfer.h:97
const std::vector< std::vector< int > > & get_distribute_map() const
Definition: hough_transfer.h:88
float theta
Definition: hough_transfer.h:31
int vote_num
Definition: hough_transfer.h:33
float length
Definition: hough_transfer.h:32
std::vector< int > pts
Definition: hough_transfer.h:34
float get_d_r() const
Definition: hough_transfer.h:92
bool Init(const char *binary_name)
float get_d_theta() const
Definition: hough_transfer.h:93
bool is_prepared() const
Definition: hough_transfer.h:85
std::string name() const
Definition: hough_transfer.h:83
float r
Definition: hough_transfer.h:30
int get_img_h() const
Definition: hough_transfer.h:95