int main()

in source/depth_estimation/DerpCLI.cpp [177:326]


int main(int argc, char* argv[]) {
  system_util::initDep(argc, argv, kUsageMessage);

  boost::timer::cpu_timer matchTimer;
  verifyInputs();

  Camera::Rig rigSrc = Camera::loadRig(FLAGS_rig);
  const int numSrcs = rigSrc.size();
  CHECK_GT(numSrcs, 0) << "no source cameras!";

  Camera::Rig rigDst = filterDestinations(rigSrc, FLAGS_cameras);
  const int numDsts = rigDst.size();
  CHECK_GT(numDsts, 0) << "no destination cameras!";
  const std::vector<int> dst2srcIdxs = mapSrcToDstIndexes(rigSrc, rigDst);

  // Get pyramid level sizes from both the disparity and color directories
  std::map<int, cv::Size> pyramidLevelSizes;
  getPyramidLevelSizes(pyramidLevelSizes, FLAGS_color);
  getPyramidLevelSizes(
      pyramidLevelSizes, getImageDir(FLAGS_output_root, ImageType::disparity_levels));
  const int numLevels =
      FLAGS_num_levels == -1 ? pyramidLevelSizes.rbegin()->first + 1 : FLAGS_num_levels;

  // Get largest level smaller or equal to the requested resolution
  const int levelStart = FLAGS_level_start >= 0 ? FLAGS_level_start : numLevels - 1;
  const int levelEnd = getLevelEnd(pyramidLevelSizes);

  CHECK_LE(FLAGS_level_start, numLevels);
  const int numFrames = std::stoi(FLAGS_last) - std::stoi(FLAGS_first) + 1;
  verifyInputImagePaths(rigSrc, rigDst, numLevels);
  filesystem::create_directories(FLAGS_output_root);

  // These must be computed before normalizing to determine the correct resolutions
  const Camera& camRef = rigDst[0];
  const int widthFullSize = camRef.resolution.x();
  const int heightFullSize = camRef.resolution.y();

  // Normalize cameras (needed to generate FOV masks and to process frames)
  Camera::normalizeRig(rigSrc);
  Camera::normalizeRig(rigDst);

  for (int level = levelStart; level >= levelEnd; --level) {
    // Create level output directories
    createLevelOutputDirs(FLAGS_output_root, level, rigDst, FLAGS_save_debug_images);

    // Create dst FOV masks for current level size
    const cv::Size& sizeLevel = pyramidLevelSizes.at(level);
    const std::vector<cv::Mat_<bool>> dstFovMasks =
        generateFovMasks(rigDst, sizeLevel, FLAGS_threads);

    for (int iFrame = 0; iFrame < numFrames; ++iFrame) {
      // Load current level data
      const std::string frameName =
          image_util::intToStringZeroPad(iFrame + std::stoi(FLAGS_first), 6);

      // Color
      std::vector<cv::Mat_<PixelType>> colorImagesLevel =
          loadLevelImages<PixelType>(FLAGS_color, level, rigSrc, frameName, FLAGS_threads);

      // Foreground masks
      std::vector<cv::Mat_<bool>> srcForegroundMasksLevel = FLAGS_use_foreground_masks
          ? loadLevelImages<bool>(FLAGS_foreground_masks, level, rigSrc, frameName, FLAGS_threads)
          : cv_util::generateAllPassMasks(sizeLevel, numSrcs);

      // Background disparities
      std::vector<cv::Mat_<float>> dstBackgroundDisparitiesLevel(rigDst.size());
      if (FLAGS_use_foreground_masks) {
        dstBackgroundDisparitiesLevel = loadLevelImages<float>(
            FLAGS_background_disp, level, rigDst, FLAGS_background_frame, FLAGS_threads);
      }

      PyramidLevel<PixelType> framePyramidLevel(
          iFrame,
          frameName,
          numFrames,
          level,
          numLevels,
          pyramidLevelSizes,
          rigSrc,
          rigDst,
          dst2srcIdxs,
          colorImagesLevel,
          srcForegroundMasksLevel,
          dstFovMasks,
          dstBackgroundDisparitiesLevel,
          widthFullSize,
          heightFullSize,
          FLAGS_color,
          FLAGS_var_noise_floor,
          FLAGS_var_high_thresh,
          FLAGS_use_foreground_masks,
          FLAGS_output_root,
          FLAGS_threads);

      // Generate/link reprojections
      precomputeProjections(framePyramidLevel, FLAGS_threads);

      if (level < numLevels - 1) {
        // Allocate masks but only populate them if needed
        std::vector<cv::Mat_<bool>> dstForegroundMasksLevel(numDsts);
        std::vector<cv::Mat_<bool>> dstForegroundMasksCoarse(numDsts);
        if (FLAGS_use_foreground_masks) {
          dstForegroundMasksLevel = loadLevelImages<bool>(
              FLAGS_foreground_masks, level, rigDst, frameName, FLAGS_threads);
          dstForegroundMasksCoarse = loadLevelImages<bool>(
              FLAGS_foreground_masks, level + 1, rigDst, frameName, FLAGS_threads);
        }

        const std::vector<cv::Mat_<float>> dstDispsCoarse =
            loadImages<float>(getLevelDisparityDir(level + 1), rigDst, frameName, FLAGS_threads);

        const std::vector<cv::Mat_<float>> dstDispsNextLevel = upsampleDisparities(
            rigDst,
            dstDispsCoarse,
            dstBackgroundDisparitiesLevel,
            dstForegroundMasksCoarse,
            dstForegroundMasksLevel,
            sizeLevel,
            FLAGS_use_foreground_masks,
            FLAGS_threads);

        for (int dstIdx = 0; dstIdx < numDsts; ++dstIdx) {
          framePyramidLevel.dsts[dstIdx].disparity = dstDispsNextLevel[dstIdx];
        }
      }

      processLevel(
          framePyramidLevel,
          FLAGS_output_formats,
          FLAGS_use_foreground_masks,
          FLAGS_output_root,
          FLAGS_random_proposals,
          FLAGS_partial_coverage,
          FLAGS_min_depth_m,
          FLAGS_max_depth_m,
          FLAGS_do_median_filter,
          FLAGS_save_debug_images,
          FLAGS_ping_pong_iterations,
          FLAGS_mismatches_start_level,
          FLAGS_do_bilateral_filter,
          FLAGS_threads);
    }

    LOG(INFO) << folly::sformat("-- Elapsed time: {}", matchTimer.format());
  }

  LOG(INFO) << folly::sformat("-- TOTAL: {}", matchTimer.format());

  return EXIT_SUCCESS;
}