83bool FindCC(
const std::vector<unsigned char>& src,
int width,
int height,
84 const base::RectI& roi, std::vector<ConnectedComponent>* cc) {
86 AERROR <<
"input image is empty";
93 int x_max = x_min + roi.
width - 1;
94 int y_max = y_min + roi.
height - 1;
96 AERROR <<
"x_min is less than zero: " << x_min;
100 AERROR <<
"y_min is less than zero: " << y_min;
103 if (x_max >= width) {
104 AERROR <<
"x_max is larger than image width: " << x_max <<
"|" << width;
107 if (y_max >= height) {
108 AERROR <<
"y_max is larger than image height: " << y_max <<
"|" << height;
112 AERROR <<
"too small roi range";
116 size_t total_pix =
static_cast<size_t>(roi.
width * roi.
height);
117 std::vector<int> frame_label(total_pix);
119 std::vector<int> root_map;
120 root_map.reserve(total_pix);
131 for (y = y_min; y <= y_max; y++) {
132 int row_start = y * width;
133 for (x = x_min; x <= x_max; x++, cur_idx++) {
134 left_idx = cur_idx - 1;
135 up_idx = cur_idx - width;
140 left_val = src[row_start + x - 1];
146 up_val = src[row_start - width + x];
149 if (src[row_start + x] > 0) {
150 if (left_val == 0 && up_val == 0) {
152 frame_label[cur_idx] = labels.
Add();
153 root_map.push_back(-1);
154 }
else if (left_val != 0 && up_val == 0) {
156 frame_label[cur_idx] = frame_label[left_idx];
157 }
else if (left_val == 0 && up_val != 0) {
159 frame_label[cur_idx] = frame_label[up_idx];
163 frame_label[cur_idx] = (frame_label[left_idx] > frame_label[up_idx])
164 ? frame_label[up_idx]
165 : frame_label[left_idx];
166 labels.
Unite(frame_label[left_idx], frame_label[up_idx]);
169 frame_label[cur_idx] = -1;
173 AINFO <<
"subset number = " << labels.
Size();
179 for (y = y_min; y <= y_max; y++) {
180 for (x = x_min; x <= x_max; x++, cur_idx++) {
181 curt_label = frame_label[cur_idx];
182 if (curt_label >= 0 && curt_label <
static_cast<int>(labels.
Num())) {
183 curt_label = labels.
Find(curt_label);
184 if (curt_label >=
static_cast<int>(root_map.size())) {
185 AERROR <<
"curt_label should be smaller than root_map.size() "
186 << curt_label <<
" vs. " << root_map.size();
189 if (root_map.at(curt_label) != -1) {
190 (*cc)[root_map.at(curt_label)].AddPixel(x, y);
193 root_map.at(curt_label) = cc_count++;
198 AINFO <<
"cc number = " << cc_count;
204 float camera_ground_height,
205 const Eigen::Matrix3f& intrinsic_params_inverse,
206 Eigen::Vector3d* camera_point) {
207 Eigen::MatrixXf pt_m(3, 1);
208 pt_m << img_point.
x, img_point.
y, 1;
209 const Eigen::MatrixXf& org_camera_point = intrinsic_params_inverse * pt_m;
211 float cos_pitch =
static_cast<float>(cos(pitch_angle));
212 float sin_pitch =
static_cast<float>(sin(pitch_angle));
213 Eigen::Matrix3f pitch_matrix;
214 pitch_matrix << 1, 0, 0, 0, cos_pitch, sin_pitch, 0, -sin_pitch, cos_pitch;
215 const Eigen::MatrixXf& rotate_point = pitch_matrix * org_camera_point;
216 if (fabs(rotate_point(1, 0)) < lane_eps_value) {
219 float scale = camera_ground_height / rotate_point(1, 0);
220 (*camera_point)(0) = scale * org_camera_point(0, 0);
221 (*camera_point)(1) = scale * org_camera_point(1, 0);
222 (*camera_point)(2) = scale * org_camera_point(2, 0);
227 const Eigen::Matrix3f& intrinsic_params,
229 Eigen::Vector3f camera_point3f;
230 camera_point3f(0, 0) =
static_cast<float>(camera_point(0, 0));
231 camera_point3f(1, 0) =
static_cast<float>(camera_point(1, 0));
232 camera_point3f(2, 0) =
static_cast<float>(camera_point(2, 0));
233 Eigen::MatrixXf img_point3f = intrinsic_params * camera_point3f;
234 if (fabs(img_point3f(2, 0)) < lane_eps_value) {
237 img_point->
x = img_point3f(0, 0) / img_point3f(2, 0);
238 img_point->
y = img_point3f(1, 0) / img_point3f(2, 0);
248 AWARN <<
"dim is smaller than k";
252 AWARN <<
"k is smaller than 0";
255 std::vector<float> small_value(k);
258 for (
int i = 0; i < k; i++) {
259 small_value[i] = distance[index[i]];
262 for (
int i = k; i < dim; i++) {
263 float max_value = small_value[k - 1];
264 if (distance[i] >= max_value) {
267 int locate_index = -1;
268 if (distance[i] < small_value[0]) {
271 for (
int j = 0; j < k - 1; j++) {
272 if (distance[i] >= small_value[j] &&
273 distance[i] <= small_value[j + 1]) {
274 locate_index = j + 1;
279 if (locate_index == -1) {
282 for (
int j = k - 2; j >= locate_index; j--) {
283 small_value[j + 1] = small_value[j];
284 index[j + 1] = index[j];
286 small_value[locate_index] = distance[i];
287 index[locate_index] = i;
294 AWARN <<
"dim is smaller than k";
298 AWARN <<
"k is smaller than 0";
301 std::vector<float> large_value(k);
302 std::vector<int> large_index(k);
304 QuickSort(&(large_index[0]), distance, k);
305 for (
int i = 0; i < k; i++) {
306 index[i] = large_index[k - 1 - i];
307 large_value[i] = distance[index[i]];
310 for (
int i = k; i < dim; i++) {
311 float min_value = large_value[k - 1];
312 if (distance[i] <= min_value) {
315 int locate_index = -1;
316 if (distance[i] > large_value[0]) {
319 for (
int j = 0; j < k - 1; j++) {
320 if (distance[i] <= large_value[j] &&
321 distance[i] >= large_value[j + 1]) {
322 locate_index = j + 1;
327 if (locate_index == -1) {
330 for (
int j = k - 2; j >= locate_index; j--) {
331 large_value[j + 1] = large_value[j];
332 index[j + 1] = index[j];
334 large_value[locate_index] = distance[i];
335 index[locate_index] = i;