Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

you're almost there ! all it needs is to actually apply the mask from drawContours !

(bitwise_and it with your binary image)

Mat result;
vector<vector<Point> >contours;
vector<Vec4i>hierarchy;
int savedContour = -1;
double maxArea = 0.0;
// Find the largest contour
//
//// findContours will "eat" the input image, so clone the binary Mat, we need it later:
//
findContours(binarayImage.clone(), contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point());
for (int i = 0; i< contours.size(); i++)
{
    double area = contourArea(contours[i]);
    if (area > maxArea)
    {
        maxArea = area;
        savedContour = i;
    }
}
// Create mask
drawContours(result, contours, savedContour, Scalar(255), CV_FILLED, 8);

// apply the mask:
binarayImage &= result;