How to remove the outer rectangle from this image? If there should be other rectangles inside the outer rectangle they should stay untoched. Only the outer rectagnel should be removed

Below I have some code which can detect the entire rectangle but how to mask only the outer rectangle and remove it?

Have attached the input and what I expect as output

OpenCV `4.5.1`

[![Input](https://i.stack.imgur.com/LSmPY.png)](https://i.stack.imgur.com/LSmPY.png)

[![Output](https://i.stack.imgur.com/jqXRR.png)](https://i.stack.imgur.com/jqXRR.png)

```
// g++ -Wall -O3 -std=c++17 test.cpp -o test `pkg-config opencv4 --cflags --libs`

#include <iostream>
#include <string>
#include <opencv4/opencv2/opencv.hpp>

const int
    HIERARCHY_NEXT              = 0,
    HIERARCHY_PREV              = 1,
    HIERARCHY_CHILD             = 2,
    HIERARCHY_PARENT            = 3,
    SHAPE_UNKNOWN               = -1,
    SHAPE_RECT                  = 0;

struct Contours {
    std::vector<std::vector<cv::Point>> contours;
    std::vector<cv::Vec4i> hierarchy;
};

Contours find_contours(const cv::Mat& m, int retrieve_type, bool fetch_hierarchy=true){
    std::vector<std::vector<cv::Point>> contours;
    std::vector<cv::Vec4i> hierarchy;

    if(fetch_hierarchy){
        cv::findContours(m, contours, hierarchy, retrieve_type, cv::CHAIN_APPROX_SIMPLE);
    }
    else{
        cv::findContours(m, contours, retrieve_type, cv::CHAIN_APPROX_SIMPLE);
    }

    Contours con;
    con.contours    = contours;
    con.hierarchy   = hierarchy;

    return con;
}

int detect_contour_shape(const std::vector<cv::Point>& contour, double precision=0.02, bool convex=true){
    std::vector<cv::Point> approx;
    cv::approxPolyDP(contour, approx, cv::arcLength(contour, true) * precision, true);

    if(convex && !cv::isContourConvex(approx)){
        return SHAPE_UNKNOWN;
    }

    switch(approx.size()){
        case 4:
            return SHAPE_RECT;

        default:
            return SHAPE_UNKNOWN;
    }
}

bool is_contour_top_with_children(Contours contours, int i){
    return contours.hierarchy[i][HIERARCHY_PARENT] == -1 && contours.hierarchy[i][HIERARCHY_CHILD] != -1;
}

int main(int argc, char* argv[]){
    cv::Mat
        src     = cv::imread("input.png", cv::IMREAD_GRAYSCALE),
        mask    = cv::Mat::zeros(src.rows, src.cols, CV_8UC1);

    Contours contours = find_contours(src, cv::RETR_TREE);

    //  Filter contours
    if(!contours.hierarchy.empty()){
        for(int i = 0; i >= 0; i = contours.hierarchy[i][HIERARCHY_NEXT]){
            //  Contour has no parent (top-level contour) and has children
            if(is_contour_top_with_children(contours, i)){
                if(detect_contour_shape(contours.contours[i]) == SHAPE_RECT){
                    cv::Rect rect = cv::boundingRect(contours.contours[i]);

                    std::cout << rect << '\n';
                    cv::drawContours(mask, contours.contours, i, cv::Scalar(255), cv::FILLED);
                }
            }
        }
    }

    cv::imwrite("mask.png", mask);

    return 0;
}

```