Created
September 9, 2019 08:26
-
-
Save prostoiChelovek/28c9d199ee4922c90d5d0b6ed91fd102 to your computer and use it in GitHub Desktop.
Simple function to prevent rect from going abroad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <opencv2/core.hpp> | |
static bool fixBound(cv::Rect &rect, const cv::Size &roi) { | |
bool ok = true; | |
if (rect.width <= 0) { | |
ok = false; | |
rect.width = roi.width; | |
} | |
if (rect.height <= 0) { | |
ok = false; | |
rect.height = roi.height; | |
} | |
if (rect.x < 0) rect.x = 0; | |
if (rect.y < 0) rect.y = 0; | |
if (rect.x >= roi.width) | |
rect.x = roi.width - rect.width; | |
if (rect.y >= roi.height) | |
rect.y = roi.height - rect.height; | |
if (rect.x + rect.width > roi.width) | |
rect.width = roi.width - rect.x; | |
if (rect.y + rect.height > roi.height) | |
rect.height = roi.height - rect.y; | |
if (rect.width > roi.width) | |
rect.width = roi.width; | |
if (rect.height > roi.height) | |
rect.height = roi.height; | |
if (rect.x < 0) rect.x = 0; | |
if (rect.y < 0) rect.y = 0; | |
return ok; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment