Apollo 10.0
自动驾驶开放平台
hough_transfer.h
浏览该文件的文档.
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
25namespace apollo {
26namespace perception {
27namespace algorithm {
28
29struct 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:
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 algorithm
124} // namespace perception
125} // namespace apollo
bool ImageVote(const std::vector< int > &image, bool with_distribute)
bool Init(int img_w, int img_h, float d_r, float d_theta)
const std::vector< int > & get_vote_map() const
bool GetLines(int min_pt_num, int r_neibor, int theta_neibor, bool with_distribute, std::vector< HoughLine > *lines) const
void PointVote(int x, int y, bool with_distribute)
const std::vector< std::vector< int > > & get_distribute_map() const
class register implement
Definition arena_queue.h:37