# Fit a rectangle within mask

**URL:** <https://forum.opencv.org/t/fit-a-rectangle-within-mask/5085>\
**Category:** C++\
**Tags:** imgproc\
**Created:** [September 6, 2021, 11:09pm UTC](https://forum.opencv.org/t/fit-a-rectangle-within-mask/5085 "2021-09-06T23:09:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephane\_Charette](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/stephane_charette/32/1369_2.png) [@Stephane\_Charette](https://forum.opencv.org/u/Stephane_Charette)\
**Post date:** [September 6, 2021, 11:09pm UTC](https://forum.opencv.org/t/fit-a-rectangle-within-mask/5085/1 "2021-09-06T23:09:50Z")

</div>

I have a binary mask which always forms a rectangular-ish object like this:

![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/a/a54ac2bd19bd51d3b7a9fbe586930849e85f2c34.png)

What I need to do is find the rectangle that fits within this area, like the pink one here:

![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/3/3878e23a1cbc14ea7caf208c15ab60c4894a08a7.png)

I tried using `cv::findNonZero()` and `cv::boundingRect()` like this:

```
cv::Mat points;
cv::findNonZero(output_mask, points);
cv::Rect r = cv::boundingRect(points);
cv::rectangle(output, r, cv::Scalar(0, 0, 255));

```

but of course that gives me a rectangle that includes the _outside_ of what I want vs the inside rectangle:

![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/b/ba1a485ee46cfd55456137d16ee72835eb3cd4e4.png)

I feel like I’m missing something obvious. 🙂

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [September 7, 2021, 5:03pm UTC](https://forum.opencv.org/t/fit-a-rectangle-within-mask/5085/2 "2021-09-07T17:03:07Z")

</div>

I saw something like that on stack overflow just recently.

this problem might be an optimization problem, i.e. no single best solution, or the problem space is non-convex.

---

<div class="post-metadata">

**Author:** ![Stephane\_Charette](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/stephane_charette/32/1369_2.png) [@Stephane\_Charette](https://forum.opencv.org/u/Stephane_Charette)\
**Post date:** [September 7, 2021, 5:35pm UTC](https://forum.opencv.org/t/fit-a-rectangle-within-mask/5085/3 "2021-09-07T17:35:18Z")

</div>

Here is what I did to solve this. Note that the code I was working on only runs once, so it didn’t need to be very efficient.

Start by inverting the mask, so the outside is white and the inside portion is black:

```
cv::Mat inverted_mask;
cv::bitwise_not(output_mask, inverted_mask);

```

Get the “outside” rectangle:

```
cv::Mat points;
cv::findNonZero(inverted_mask, points);
const cv::Rect outside_rect = cv::boundingRect(points);

```

Then I decrease this rectangle until all pixels are black (zero):

```
int step = 50;
auto inside_rect = outside_rect;
while (true)
{
    const auto count = cv::countNonZero(inverted_mask(inside_rect));
    if (count == 0)
    {
        // we found a rectangle we can use!
        break;
    }

    inside_rect.x += step;
    inside_rect.y += step;
    inside_rect.width -= (step * 2);
    inside_rect.height -= (step * 2);
}

```

Now at this point we have a usable “inside” rectangle. So one at a time, I increase and decrease the left, right, top, and bottom borders until I have the largest rectangle possible, checking each change with a call to `cv::countNonZero()` to decide if I’ve gone too far.
