OpenCV copy bounded text area to new image
<p>I am new to OpenCV and I am using this code to bound the text area in image. After that I am filtering contours and putting the bounded rectangle to a <code>vector&lt;Rect&gt;</code> to copy these to new image. </p>

<pre><code>Mat large = img1;
Mat rgb;
// downsample and use it for processing
pyrUp(large, rgb);
Mat small;
cvtColor(rgb, small, CV_BGR2GRAY);
// morphological gradient
Mat grad;
Mat morphKernel = getStructuringElement(MORPH_ELLIPSE, Size(2, 2));
morphologyEx(small, grad, MORPH_GRADIENT, morphKernel);
// binarize
Mat bw;
threshold(grad, bw, 0.0, 255.0, THRESH_BINARY | THRESH_OTSU);
// connect horizontally oriented regions
Mat connected;
//morphKernel = getStructuringElement(MORPH_RECT, Size(7, 1));
//morphologyEx(bw, connected, MORPH_CLOSE, morphKernel);
// find contours
connected = bw;
Mat mask = Mat::zeros(bw.size(), CV_8UC1);

Mat mask2;

Mat mask3;
vector&lt;vector&lt;Point&gt;&gt; contours;
vector&lt;Vec4i&gt; hierarchy;
findContours(connected, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

/*drawContours(mask2, contours, -1, Scalar(255), CV_FILLED);
Mat Crop(img1.rows, img1.cols, CV_8UC3);
Crop.setTo(Scalar(0, 255, 0));
img1.copyTo(Crop, mask2);
normalize(mask2.clone(), mask2, 0.0, 255.0, CV_MINMAX, CV_8UC1);
*/

vector&lt;Rect&gt; rect1;
int i = 0;
//filter contours

for (int idx = 0; idx &gt;= 0; idx = hierarchy[idx][0])
{

    Rect rect = boundingRect(contours[idx]);
    Mat maskROI(mask, rect);


    maskROI = Scalar(0, 0, 0);
    // fill the contour
    drawContours(mask, contours, idx, Scalar(255, 255, 255), CV_FILLED);
    // ratio of non-zero pixels in the filled region
    double r = (double)countNonZero(maskROI) / (rect.width*rect.height);

    if (r &gt; .45 /* assume at least 45% of the area is filled if it contains text */
        &amp;&amp;
        (rect.height &gt; 10 &amp;&amp; rect.width &gt; 10 &amp;&amp; rect.height&lt;150 &amp;&amp; rect.width&lt;150) /* constraints on region size */
        /* these two conditions alone are not very robust. better to use something
        like the number of significant peaks in a horizontal projection as a third condition */
        )
    {
        //making rectangles on bounded area
        rectangle(rgb, rect, Scalar(0, 255, 0), 2);
        //pushing bounding rectangles in vector for new mask
        rect1.push_back(rect);      
    }
}
</code></pre>

<p>Input output I am getting after bounded text ares is:</p>

<p><img src="https://i.stack.imgur.com/ejbHP.png" alt=""></p>

<p>After that I am using this code to copy the bounded area only to new mask</p>

<pre><code>//copying  bounded rectangles area from small to new mask2
for (int i = 0; i &lt; rect1.size(); i++){
    mask2 = rgb(rect1[i]);
}
</code></pre>

<p>but by using this I only get this last bounded text area:</p>

<p><img src="https://i.stack.imgur.com/4vc3q.png" alt=""></p>

<p>How can I get or update the <code>mask2</code> rows or cols to get all the mapping of bounded text areas from <code>rgb</code>  to <code>mask2</code>.</p>