cv::Rect 的异常处理

Exception Handling for cv::Rect

本文关键字:异常处理 Rect cv      更新时间:2023-10-16

我有边界框,我想用这个边界框裁剪图像。

但是我想增加边界框的大小,所以我这样做

if ((roi_.x - 5) > 0) // i test here in case the component at the left near border we do not minus otherwise it will be error
{
    roi_.x += (-5);
}
if ((roi_.y - 5) > 0) // i test here in case the component at the left near border we do not minus otherwise it will be error
{
    roi_.y += (-5);
}
if (&(roi_ + cv::Size(10, 0)) != NULL)
{
    roi_.width += 10;
}
if (&(roi_ + cv::Size(0, 10)) != NULL)
{
    roi_.height += 10;
}

对于最右边靠近边框的组件,如果我增加宽度,这将是错误的。如果组件位于靠近边框的底部,则高度也是如此

有什么方法可以处理此异常吗?

您收到错误是因为&需要 l 值,而 l 值不适用于roi_ + cv::Size(10, 0)roi_ + cv::Size(0, 10)

你需要改变

if (&(roi_ + cv::Size(10, 0)) != NULL)
...
if (&(roi_ + cv::Size(0, 10)) != NULL)

if ((roi_.x + roi_.width + 10) < img.cols)
...
if ((roi_.y + roi_.height + 10) < img.rows)