bool TryCrop()

in src/io/image_det_aug_default.cc [290:352]


  bool TryCrop(const Rect crop_box,
    const float min_crop_overlap, const float max_crop_overlap,
    const float min_crop_sample_coverage, const float max_crop_sample_coverage,
    const float min_crop_object_coverage, const float max_crop_object_coverage,
    const int crop_emit_mode, const float emit_overlap_thresh) {
    if (objects_.size() < 1) {
      return true;  // no object, raise error or just skip?
    }
    // check if crop_box valid
    bool valid = false;
    if (min_crop_overlap > 0.f && max_crop_overlap < 1.f &&
        min_crop_sample_coverage > 0.f && max_crop_sample_coverage < 1.f &&
        min_crop_object_coverage > 0.f && max_crop_object_coverage < 1.f) {
      for (auto& obj : objects_) {
        Rect gt_box = obj.ToRect();
        if (min_crop_overlap > 0.f || max_crop_overlap < 1.f) {
          float ovp = RectIOU(crop_box, gt_box);
          if (ovp < min_crop_overlap || ovp > max_crop_overlap) {
            continue;
          }
        }
        if (min_crop_sample_coverage > 0.f || max_crop_sample_coverage < 1.f) {
          float c = (crop_box & gt_box).area() / crop_box.area();
          if (c < min_crop_sample_coverage || c > max_crop_sample_coverage) {
            continue;
          }
        }
        if (min_crop_object_coverage > 0.f || max_crop_object_coverage < 1.f) {
          float c = (crop_box & gt_box).area() / gt_box.area();
          if (c < min_crop_object_coverage || c > max_crop_object_coverage) {
            continue;
          }
        }
        valid = true;
        break;
      }
    } else {
      valid = true;
    }

    if (!valid) return false;
    // transform ground-truth labels
    std::vector<ImageDetObject> new_objects;
    for (auto iter = objects_.begin(); iter != objects_.end(); ++iter) {
      if (image_det_aug_default_enum::kCenter == crop_emit_mode) {
        float center_x = (iter->left + iter->right) * 0.5f;
        float center_y = (iter->top + iter->bottom) * 0.5f;
        if (!crop_box.contains(cv::Point2f(center_x, center_y))) {
          continue;
        }
        new_objects.push_back(iter->Project(crop_box));
      } else if (image_det_aug_default_enum::kOverlap == crop_emit_mode) {
        Rect gt_box = iter->ToRect();
        float overlap = (crop_box & gt_box).area() / gt_box.area();
        if (overlap > emit_overlap_thresh) {
          new_objects.push_back(iter->Project(crop_box));
        }
      }
    }
    if (new_objects.size() < 1) return false;
    objects_ = new_objects;  // replace the old objects
    return true;
  }