From 4feb62e9053d59066ebe199f60fa88a4bbbc605f Mon Sep 17 00:00:00 2001 From: lindsayshuo Date: Thu, 9 May 2024 16:28:55 +0800 Subject: [PATCH] Solving the issue of out of bounds caused by detection boxes less than 0 when filling colors in segmentation masks --- yolov8/src/postprocess.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/yolov8/src/postprocess.cpp b/yolov8/src/postprocess.cpp index a7869145..8967f8ab 100644 --- a/yolov8/src/postprocess.cpp +++ b/yolov8/src/postprocess.cpp @@ -25,7 +25,12 @@ cv::Rect get_rect(cv::Mat& img, float bbox[4]) { t = t / r_h; b = b / r_h; } - return cv::Rect(round(l), round(t), round(r - l), round(b - t)); + l = std::max(0.0f, l); + t = std::max(0.0f, t); + int width = std::max(0, std::min(int(round(r - l)), img.cols - int(round(l)))); + int height = std::max(0, std::min(int(round(b - t)), img.rows - int(round(t)))); + + return cv::Rect(int(round(l)), int(round(t)), width, height); } cv::Rect get_rect_adapt_landmark(cv::Mat& img, float bbox[4], float lmk[kNumberOfPoints * 3]) { @@ -53,7 +58,12 @@ cv::Rect get_rect_adapt_landmark(cv::Mat& img, float bbox[4], float lmk[kNumberO // lmk[i + 2] } } - return cv::Rect(l, t, r - l, b - t); + l = std::max(0.0f, l); + t = std::max(0.0f, t); + int width = std::max(0, std::min(int(round(r - l)), img.cols - int(round(l)))); + int height = std::max(0, std::min(int(round(b - t)), img.rows - int(round(t)))); + + return cv::Rect(int(round(l)), int(round(t)), width, height); } static float iou(float lbox[4], float rbox[4]) {