void DepthmapEstimator::PatchMatchUpdatePixel()

in opensfm/src/dense/src/depthmap.cc [303:372]


void DepthmapEstimator::PatchMatchUpdatePixel(DepthmapEstimatorResult *result,
                                              int i, int j, int adjacent[2][2],
                                              bool sample) {
  // Ignore pixels with depth == 0.
  if (result->depth.at<float>(i, j) == 0.0f) {
    return;
  }

  // Check neighbors and their planes for adjacent pixels.
  for (int k = 0; k < 2; ++k) {
    int i_adjacent = i + adjacent[k][0];
    int j_adjacent = j + adjacent[k][1];

    // Do not propagate ignored adjacent pixels.
    if (result->depth.at<float>(i_adjacent, j_adjacent) == 0.0f) {
      continue;
    }

    cv::Vec3f plane = result->plane.at<cv::Vec3f>(i_adjacent, j_adjacent);

    if (sample) {
      int nghbr = result->nghbr.at<int>(i_adjacent, j_adjacent);
      CheckPlaneImageCandidate(result, i, j, plane, nghbr);
    } else {
      CheckPlaneCandidate(result, i, j, plane);
    }
  }

  // Check random planes for current neighbor.
  float depth_range = 0.02;
  float normal_range = 0.5;
  int current_nghbr = result->nghbr.at<int>(i, j);
  for (int k = 0; k < 6; ++k) {
    float current_depth = result->depth.at<float>(i, j);
    float depth = current_depth * exp(depth_range * unit_normal_(rng_));

    cv::Vec3f current_plane = result->plane.at<cv::Vec3f>(i, j);
    if (current_plane(2) == 0.0) {
      continue;
    }
    cv::Vec3f normal(-current_plane(0) / current_plane(2) +
                         normal_range * unit_normal_(rng_),
                     -current_plane(1) / current_plane(2) +
                         normal_range * unit_normal_(rng_),
                     -1.0f);

    cv::Vec3f plane = PlaneFromDepthAndNormal(j, i, Ks_[0], depth, normal);
    if (sample) {
      CheckPlaneImageCandidate(result, i, j, plane, current_nghbr);
    } else {
      CheckPlaneCandidate(result, i, j, plane);
    }

    depth_range *= 0.3;
    normal_range *= 0.8;
  }

  if (!sample || images_.size() <= 2) {
    return;
  }

  // Check random other neighbor for current plane.
  int other_nghbr = uni_(rng_);
  while (other_nghbr == current_nghbr) {
    other_nghbr = uni_(rng_);
  }

  cv::Vec3f plane = result->plane.at<cv::Vec3f>(i, j);
  CheckPlaneImageCandidate(result, i, j, plane, other_nghbr);
}