Ask Your Question

Revision history [back]

A question about the 'floodFill()' function.

Hello! I have a task to find the structure of a file. I am using floodFill () function. I need the entire file to be filled. How can you do it? I started to do it, but the work time is very long. Tell me please.

Here is my code:

void processFloodFill() {
int lo = 20;
int up = 20;
Mat mask;
Point seed = Point(0, 0);

int connectivity = 4;
int newMaskVal = 255;
int ffillMode = 1;

int flags = connectivity + (newMaskVal << 8) +
    (ffillMode == 1 ? FLOODFILL_FIXED_RANGE : 0);

Rect ccomp;
int b = (unsigned)theRNG() & 255;
int g = (unsigned)theRNG() & 255;
int r = (unsigned)theRNG() & 255;
Scalar newVal = Scalar(b, g, r) ;
cv::Mat img;
image.copyTo(img);
dst = img;

mask.create(image.rows + 2, image.cols + 2, CV_8UC1);
mask = Scalar::all(0);
threshold(mask, mask, 1, 128, THRESH_BINARY);
for (int c = 0; c < diff.cols; c++) {
    for (int r = 0; r < diff.rows; r++) {
        Vec3b colour = diff.at<Vec3b>(Point(c, r));
        if (colour.val[0] == 255 && colour.val[1] == 255 && colour.val[2] == 255) {

            //std::cout << seed << std::endl;
            //threshold(mask, mask, 1, 128, THRESH_BINARY);
            seed = Point(c, r);

            int area = floodFill(dst, mask, seed, newVal, &ccomp, Scalar(lo, lo, lo),
                Scalar(up, up, up), flags);

            vector<vector<Point> > contours;
            vector<Vec4i> hierarchy;

            findContours(mask, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);

            // iterate through all the top-level contours,
            // draw each connected component with its own random color
            int idx = 0;
            for (; idx >= 0; idx = hierarchy[idx][0]) {                    
                drawContours(layout_image, contours, idx, newVal, CV_FILLED, 8, hierarchy);
            }
            //converting the difference into grascale                    
            absdiff(img, layout_image, diff);
        }
    }
}

}

This is the picture I am using: image description