| 346 | } |
| 347 | |
| 348 | std::vector<std::shared_ptr<Feature>> Initializer::SelectFeaturesForInit( |
| 349 | const std::vector<std::shared_ptr<Frame>>& frames |
| 350 | ) const { |
| 351 | std::vector<std::shared_ptr<Feature>> selected_features; |
| 352 | |
| 353 | if (frames.empty()) { |
| 354 | return selected_features; |
| 355 | } |
| 356 | |
| 357 | // Get features from the last frame (they have all the observation history) |
| 358 | auto last_frame = frames.back(); |
| 359 | const auto& all_features = last_frame->GetFeatures(); |
| 360 | |
| 361 | // Filter by observation count |
| 362 | std::vector<std::shared_ptr<Feature>> candidates; |
| 363 | for (const auto& feat : all_features) { |
| 364 | int obs_count = feat->GetObservationCount(); |
| 365 | if (obs_count >= m_min_observations) { |
| 366 | candidates.push_back(feat); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | if (candidates.size() < static_cast<size_t>(m_min_features)) { |
| 371 | return selected_features; |
| 372 | } |
| 373 | |
| 374 | // Grid-based sampling for uniform distribution |
| 375 | const int grid_cols = m_init_grid_cols; |
| 376 | const int grid_rows = m_init_grid_rows; |
| 377 | const int total_grids = grid_cols * grid_rows; |
| 378 | |
| 379 | // Get camera dimensions from config |
| 380 | const int img_width = m_camera_width; |
| 381 | const int img_height = m_camera_height; |
| 382 | |
| 383 | const float cell_width = static_cast<float>(img_width) / grid_cols; |
| 384 | const float cell_height = static_cast<float>(img_height) / grid_rows; |
| 385 | |
| 386 | // Assign candidates to grid cells |
| 387 | std::vector<std::vector<std::shared_ptr<Feature>>> grid(total_grids); |
| 388 | |
| 389 | for (const auto& feat : candidates) { |
| 390 | cv::Point2f pt = feat->GetPixelCoord(); |
| 391 | int col = static_cast<int>(pt.x / cell_width); |
| 392 | int row = static_cast<int>(pt.y / cell_height); |
| 393 | |
| 394 | // Clamp to valid range |
| 395 | col = std::max(0, std::min(col, grid_cols - 1)); |
| 396 | row = std::max(0, std::min(row, grid_rows - 1)); |
| 397 | |
| 398 | int grid_idx = row * grid_cols + col; |
| 399 | grid[grid_idx].push_back(feat); |
| 400 | } |
| 401 | |
| 402 | // Count non-empty cells |
| 403 | int non_empty_cells = 0; |
| 404 | for (const auto& cell : grid) { |
| 405 | if (!cell.empty()) { |
no test coverage detected