Skip to content

Instantly share code, notes, and snippets.

@serycjon
Created June 14, 2017 13:27
Show Gist options
  • Save serycjon/04af2571a82eb405eb4e568004f27fd8 to your computer and use it in GitHub Desktop.
Save serycjon/04af2571a82eb405eb4e568004f27fd8 to your computer and use it in GitHub Desktop.
IOU rotated rect openCV
// compute intersection area
std::vector<cv::Point2f> intersections_unsorted;
std::vector<cv::Point2f> intersections;
cv::rotatedRectangleIntersection(bounding_rect, detection_bbox, intersections_unsorted);
if (intersections_unsorted.size() < 3) {
return GT_score{};
}
// need to sort the vertices CW or CCW
cv::convexHull(intersections_unsorted, intersections);
// Shoelace formula
float intersection_area = 0;
for (unsigned int i = 0; i < intersections.size(); ++i) {
const auto& pt = intersections[i];
const unsigned int i_next = (i + 1) == intersections.size() ? 0 : (i + 1);
const auto& pt_next = intersections[i_next];
intersection_area += (pt.x * pt_next.y - pt_next.x * pt.y);
}
intersection_area = std::abs(intersection_area) / 2;
// compute union area
const float area_GT = bounding_rect.size.area();
const float area_detection = detection_bbox.size.area();
const float union_area = area_GT + area_detection - intersection_area;
// intersection over union
const float overlap_score = intersection_area / union_area;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment