chore: 完全越界时报 error

This commit is contained in:
uye
2025-11-24 02:46:18 +08:00
parent 5c6f654762
commit bf30c84b4f

View File

@@ -83,15 +83,19 @@ Rect VisionHelper::correct_rect(const Rect& rect, const cv::Mat& image)
if (rect.empty()) {
return { 0, 0, image.cols, image.rows };
}
if (rect.x >= image.cols || rect.y >= image.rows) {
Log.error(__FUNCTION__, "roi is out of range", image.cols, image.rows, rect.to_string());
return rect;
}
Rect res = rect;
res.x = std::clamp(res.x, 0, image.cols);
res.y = std::clamp(res.y, 0, image.rows);
res.width = std::clamp(res.width, 1, std::max(1, image.cols - res.x));
res.height = std::clamp(res.height, 1, std::max(1, image.rows - res.y));
res.x = std::clamp(res.x, 0, image.cols - 1);
res.y = std::clamp(res.y, 0, image.rows - 1);
res.width = std::clamp(res.width, 1, image.cols - res.x);
res.height = std::clamp(res.height, 1, image.rows - res.y);
if (res != rect) {
Log.warn("roi is out of range", image.cols, image.rows, rect.to_string(), "clamped");
Log.warn(__FUNCTION__, "roi is out of range", image.cols, image.rows, rect.to_string(), "clamped");
}
return res;