skimage2.segmentation.flood_fill#
- skimage2.segmentation.flood_fill(image, seed_point, new_value, *, footprint=None, connectivity=None, tolerance=None, in_place=False)[source]#
Perform flood filling on an image.
Starting at a specific
seed_point, connected points equal or withintoleranceof the seed value are found, then set tonew_value.- Parameters:
- imagendarray
An n-dimensional array.
- seed_pointtuple or int
The point in
imageused as the starting point for the flood fill. If the image is 1D, this point may be given as an integer.- new_value
imagetype New value to set the entire fill. This must be chosen in agreement with the dtype of
image.- footprintndarray, optional
The footprint (structuring element) used to determine the neighborhood of each evaluated pixel. It must contain only 1’s and 0’s, have the same number of dimensions as
image. If not given, all adjacent pixels are considered as part of the neighborhood (fully connected).- connectivityint, optional
A number used to determine the neighborhood of each evaluated pixel. Adjacent pixels whose squared distance from the center is less than or equal to
connectivityare considered neighbors. Ignored iffootprintis not None.- tolerancefloat or int, optional
If None (default), adjacent values must be strictly equal to the value of
imageatseed_pointto be filled. This is fastest. If a tolerance is provided, adjacent points with values within plus or minus tolerance from the seed point are filled (inclusive).- in_placebool, optional
If True, flood filling is applied to
imagein place. If False, the flood filled result is returned without modifying the inputimage(default).
- Returns:
- filledndarray
An array with the same shape as
imageis returned, with values in areas connected to and equal (or within tolerance of) the seed point replaced withnew_value.
Notes
The conceptual analogy of this operation is the ‘paint bucket’ tool in many raster graphics programs.
Examples
>>> from _skimage2.morphology import flood_fill >>> image = np.zeros((4, 7), dtype=int) >>> image[1:3, 1:3] = 1 >>> image[3, 0] = 1 >>> image[1:3, 4:6] = 2 >>> image[3, 6] = 3 >>> image array([[0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 2, 2, 0], [0, 1, 1, 0, 2, 2, 0], [1, 0, 0, 0, 0, 0, 3]])
Fill connected ones with 5, with full connectivity (diagonals included):
>>> flood_fill(image, (1, 1), 5) array([[0, 0, 0, 0, 0, 0, 0], [0, 5, 5, 0, 2, 2, 0], [0, 5, 5, 0, 2, 2, 0], [5, 0, 0, 0, 0, 0, 3]])
Fill connected ones with 5, excluding diagonal points (connectivity 1):
>>> flood_fill(image, (1, 1), 5, connectivity=1) array([[0, 0, 0, 0, 0, 0, 0], [0, 5, 5, 0, 2, 2, 0], [0, 5, 5, 0, 2, 2, 0], [1, 0, 0, 0, 0, 0, 3]])
Fill with a tolerance:
>>> flood_fill(image, (0, 0), 5, tolerance=1) array([[5, 5, 5, 5, 5, 5, 5], [5, 5, 5, 5, 2, 2, 5], [5, 5, 5, 5, 2, 2, 5], [5, 5, 5, 5, 5, 5, 3]])