c++ - cv::Rect 的异常处理

标签 c++ opencv exception rect

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

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

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 值,它不适用于 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)

关于c++ - cv::Rect 的异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21834072/

相关文章:

c# - 检查目标方法可以在 .Net 中抛出的所有可能的异常?

c++ - 运算符重载类型转换 : Polar to cartesian c++

c++ - 访问迭代器指向的列表元素

c++ - add 的位运算

c++ - 如何使用opencv中的单应矩阵将一个点从一张图像重新投影到另一张图像?

opencv - cvundistortpoints 中的新相机矩阵是什么?

python - Python 中的 OpenCV。问 : Recognize my face and put my name. 像数据库

c++ - 如何在可执行文件上设置 MOTW

python - 为什么 Exception 将 __str__ 代理到 args 上?

C#(不是 ASP/MVC/WinForms)——捕获类中的所有异常