简体   繁体   中英

Exception Handling for cv::Rect

I have bounding box and I want to crop image with this bounding box.

However I want to increase the size of the bounding box, so I do

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;
}

For the component at the most right near the border if I increase the width it will be error. The same thing for the height in case the component is at the bottom near the border

Are there any ways to handle this exception?

You get the error because & requires the l-value , which are not for both roi_ + cv::Size(10, 0) and roi_ + cv::Size(0, 10) .

You need to change

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM