Skip to content Skip to sidebar Skip to footer

How Does Cv2.floodfill Work?

here is a example code showing usage of cv2.floodfill function import cv2 import numpy as np import os def imshow(img): cv2.imshow('img', img) cv2.waitKey(0) cv2.destr

Solution 1:

The short answer for your current implementation is that you don't need to use that parameter. You can simply replace mask1 with None in the call because you're not using it. Check out my answers here, here, and here to see examples of floodFill() without the mask parameter.

The mask parameter simply fills in wherefloodFill() has been applied, or in case you just want to create a mask and not modify your image. You can see an example on one of my simple projects here using the mask parameter. In this case I was mimicking the Adobe Photoshop Magic Wand selection tool, and so I only needed the mask and didn't want to modify the image.

What floodFill() does is connects a pixel to it's neighbors if the neighbors are within some threshold difference of the pixel. Four-way connectivity checks the neighbors above and below, and to the left and right. Eight-way connectivity checks the diagonal pixels in addition. This means that at border pixels, you either need a bunch of if statements to not check the pixels outside the border, OR, you can simply pad the image with one pixel on each side so that you don't need special cases, which both reads better in code and is faster.

Normally, this would just be done inside the function and not exposed to the user. However, floodFill() is designed to be called multiple times on an image if need-be, and so creating a new padded Mat each time it's called would make the function slow. So instead, I believe, the padding is passed on to the user so that if they call the function multiple times, the padded mask is created only once.

Post a Comment for "How Does Cv2.floodfill Work?"