Apollo 10.0
自动驾驶开放平台
hm_matcher.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2024 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 *****************************************************************************/
17
18#include <string>
19#include <utility>
20#include <numeric>
21
22#include "modules/perception/camera_detection_occupancy/proto/matcher_config.pb.h"
23
24#include "cyber/common/file.h"
25#include "cyber/common/log.h"
28
29namespace apollo {
30namespace perception {
31namespace camera {
32
33bool HMMatcher::Init(const MatcherInitOptions &options) {
34 std::string config_file =
35 GetConfigFile(options.config_path, options.config_file);
36 MatcherConfig config_params;
37 ACHECK(cyber::common::GetProtoFromFile(config_file, &config_params))
38 << "Failed to parse MatcherConfig config file.";
39 double max_match_distance = config_params.max_match_distance();
40 double bound_match_distance = config_params.bound_match_distance();
41 BaseMatcher::SetMaxMatchDistance(max_match_distance);
42 BaseMatcher::SetBoundMatchDistance(bound_match_distance);
43 return true;
44}
45
46// @brief match camera objects to tracks
47// @params[IN] camera_tracks: global tracks
48// @params[IN] camera_frame: current camera frame
49// @params[IN] options: matcher options for future use
50// @params[OUT] assignments: matched pair of tracks and measurements
51// @params[OUT] unassigned_tracks: unmatched tracks
52// @params[OUT] unassigned_objects: unmatched objects
53// @return nothing
54bool HMMatcher::Match(const std::vector<CameraTrackPtr> &camera_tracks,
55 const base::Frame &camera_frame,
56 const TrackObjectMatcherOptions &options,
57 std::vector<TrackObjectPair> *assignments,
58 std::vector<size_t> *unassigned_tracks,
59 std::vector<size_t> *unassigned_objects) {
60 TrackObjectPropertyMatch(camera_tracks, camera_frame, assignments,
61 unassigned_tracks, unassigned_objects);
62 return true;
63}
64
66 double track_timestamp,
67 const base::ObjectPtr &camera_object,
68 double camera_timestamp) {
69 double dist = 0.5 * DistanceBetweenObs(track_object, track_timestamp,
70 camera_object, camera_timestamp) +
71 0.5 * DistanceBetweenObs(camera_object, camera_timestamp,
72 track_object, track_timestamp);
73
75}
76
77void HMMatcher::TrackObjectPropertyMatch(
78 const std::vector<CameraTrackPtr> &camera_tracks,
79 const base::Frame &camera_frame, std::vector<TrackObjectPair> *assignments,
80 std::vector<size_t> *unassigned_tracks,
81 std::vector<size_t> *unassigned_objects) {
82 size_t num_track = camera_tracks.size();
83 const auto &objects = camera_frame.objects;
84 size_t num_obj = objects.size();
85
86 unassigned_tracks->resize(num_track);
87 unassigned_objects->resize(num_obj);
88 std::iota(unassigned_tracks->begin(), unassigned_tracks->end(), 0);
89 std::iota(unassigned_objects->begin(), unassigned_objects->end(), 0);
90
91 if (unassigned_tracks->empty() || unassigned_objects->empty()) {
92 return;
93 }
94 std::vector<std::vector<double>> association_mat(num_track);
95 for (size_t i = 0; i < association_mat.size(); ++i) {
96 association_mat[i].resize(num_obj, 0);
97 }
98 ComputeAssociationMat(camera_tracks, camera_frame, *unassigned_tracks,
99 *unassigned_objects, &association_mat);
100
101 // from perception-common
102 algorithm::SecureMat<double> *global_costs =
103 hungarian_matcher_.mutable_global_costs();
104 global_costs->Resize(unassigned_tracks->size(), unassigned_objects->size());
105 for (size_t i = 0; i < unassigned_tracks->size(); ++i) {
106 for (size_t j = 0; j < unassigned_objects->size(); ++j) {
107 (*global_costs)(i, j) = association_mat[i][j];
108 }
109 }
110 std::vector<TrackObjectPair> property_assignments;
111 std::vector<size_t> property_unassigned_tracks;
112 std::vector<size_t> property_unassigned_objects;
113 hungarian_matcher_.Match(
116 &property_assignments, &property_unassigned_tracks,
117 &property_unassigned_objects);
118
119 for (size_t i = 0; i < property_assignments.size(); ++i) {
120 size_t gt_idx = unassigned_tracks->at(property_assignments[i].first);
121 size_t go_idx = unassigned_objects->at(property_assignments[i].second);
122 assignments->push_back(std::pair<size_t, size_t>(gt_idx, go_idx));
123 }
124 std::vector<size_t> temp_unassigned_tracks;
125 std::vector<size_t> temp_unassigned_objects;
126 for (size_t i = 0; i < property_unassigned_tracks.size(); ++i) {
127 size_t gt_idx = unassigned_tracks->at(property_unassigned_tracks[i]);
128 temp_unassigned_tracks.push_back(gt_idx);
129 }
130 for (size_t i = 0; i < property_unassigned_objects.size(); ++i) {
131 size_t go_idx = unassigned_objects->at(property_unassigned_objects[i]);
132 temp_unassigned_objects.push_back(go_idx);
133 }
134 *unassigned_tracks = temp_unassigned_tracks;
135 *unassigned_objects = temp_unassigned_objects;
136}
137
138void HMMatcher::ComputeAssociationMat(
139 const std::vector<CameraTrackPtr> &camera_tracks,
140 const base::Frame &camera_frame,
141 const std::vector<size_t> &unassigned_tracks,
142 const std::vector<size_t> &unassigned_objects,
143 std::vector<std::vector<double>> *association_mat) {
144 double frame_timestamp = camera_frame.timestamp;
145 for (size_t i = 0; i < unassigned_tracks.size(); ++i) {
146 for (size_t j = 0; j < unassigned_objects.size(); ++j) {
147 const base::ObjectPtr &track_object =
148 camera_tracks[unassigned_tracks[i]]->GetObs();
149 const base::ObjectPtr &frame_object =
150 camera_frame.objects[unassigned_objects[j]];
151 double track_timestamp =
152 camera_tracks[unassigned_tracks[i]]->GetTimestamp();
153 double distance_forward = DistanceBetweenObs(
154 track_object, track_timestamp, frame_object, frame_timestamp);
155 double distance_backward = DistanceBetweenObs(
156 frame_object, frame_timestamp, track_object, track_timestamp);
157 association_mat->at(i).at(j) =
158 0.5 * distance_forward + 0.5 * distance_backward;
159 }
160 }
161}
162
163double HMMatcher::DistanceBetweenObs(const base::ObjectPtr &obs1,
164 double timestamp1,
165 const base::ObjectPtr &obs2,
166 double timestamp2) {
167 double time_diff = timestamp2 - timestamp1;
168 return (obs2->center - obs1->center -
169 obs1->velocity.cast<double>() * time_diff)
170 .head(2)
171 .norm();
172}
173
175
176} // namespace camera
177} // namespace perception
178} // namespace apollo
#define PERCEPTION_REGISTER_MATCHER(name)
void Match(T cost_thresh, OptimizeFlag opt_flag, std::vector< std::pair< size_t, size_t > > *assignments, std::vector< size_t > *unassigned_rows, std::vector< size_t > *unassigned_cols)
void Resize(const size_t resize_height, const size_t resize_width)
static void SetBoundMatchDistance(double dist)
Set the Bound Match Distance
static double GetBoundMatchDistance()
Get the Bound Match Distance
static void SetMaxMatchDistance(double dist)
Set the Max Match Distance
static double GetMaxMatchDistance()
Get the Max Match Distance
bool Init(const MatcherInitOptions &options) override
Init HMMatcher config
Definition hm_matcher.cc:33
bool RefinedTrack(const base::ObjectPtr &track_object, double track_timestamp, const base::ObjectPtr &camera_object, double camera_timestamp) override
After the IDMatch, then track targets based on their features.
Definition hm_matcher.cc:65
bool Match(const std::vector< CameraTrackPtr > &camera_tracks, const base::Frame &camera_frame, const TrackObjectMatcherOptions &options, std::vector< TrackObjectPair > *assignments, std::vector< size_t > *unassigned_tracks, std::vector< size_t > *unassigned_objects) override
match camera objects to tracks
Definition hm_matcher.cc:54
#define ACHECK(cond)
Definition log.h:80
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
Definition file.cc:132
std::shared_ptr< Object > ObjectPtr
Definition object.h:127
std::string GetConfigFile(const std::string &config_path, const std::string &config_file)
Definition util.cc:80
class register implement
Definition arena_queue.h:37
std::vector< std::shared_ptr< Object > > objects
Definition frame.h:48