This is the 19th day of my participation in the August Wenwen Challenge.More challenges in August

Learning goals

One hour a day, 365 days.

The Python OpenCV flood fill algorithm is also known as the flood fill algorithm.

The principle is to start from a pixel point, fill all nearby points that meet the pixel requirements into a specified color, until the point that does not meet the requirements is encountered.

From the concept of memory, there are commonly four neighborhood pixel filling method, eight neighborhood pixel filling method, pixel filling method based on scan line.

To skip these concepts, it is useless to learn any of the basic concepts until you have established the whole picture.

The function prototype

The syntax prototype for flood filling is as follows:

floodFill(image, mask, seedPoint, newVal[, loDiff[, upDiff[, flags]]]) -> retval, image, mask, rect
Copy the code

The function takes seven arguments, as follows:

  • Image: operation image
  • Mask: mask, 2 pixels higher than the height and 2 pixels wider than the image. Padding cannot pass through non-zero pixels in the input mask
  • SeedPoint: The starting pixel point
  • NewVal: New fill value (color)
  • LoDiff: The lower value of the fill color (the color at the starting pixel minus this value)
  • UpDiff: The high value of the fill color (the color at the starting pixel plus this value)
  • Flags: Operation bit identifier, usually for color imagesFLOODFILL_FIXED_RANGESpecify color fill

I also found a clear statement about flags:

When it is CV_FLOODFILL_FIXED_RANGE, the pixel points to be processed are compared with the seed points. If they are (S — loDiff, S + upDiff) (S is the pixel value of the seed points), the pixel points will be filled. When it is CV_FLOODFILL_MASK_ONLY, the mask cannot be empty. In this case, the function does not fill the original image IMG, but the mask image.

Case code

Test pictures are as follows:

The test code is as follows:

import cv2 as cv
import numpy as np

# Color image fill
def fill_color_demo(src) :
    img_copy = src.copy()
    h, w, ch = src.shape
    Declare a rectangular shape, notice that both the height and width are increased by 2 pixels
    Zeros returns an array filled with zeros of the given shape and type
    mask = np.zeros([h+2, w+2], np.uint8)
    Parameter 1, the image to be filled with flooding
    # Parameter 2: mask. The mask can be used to specify which region the algorithm is used in. If it is used for the whole image, the mask size is the number of rows + 2 and the number of columns + 2 of the original image
    # mask, is a two-dimensional 0 matrix, because only the position corresponding to 0 on the mask can flood
    Parameter 3: seed point filled with flooding. Based on the pixel of the point, judge whether the pixel point with similar color is flooded
    # Parameter 4, new color for flooding area (BGR format)
    # Parameter 5, seed point pixel can be down to the pixel value
    # Parameter 6, seed point pixel can be up to pixel value
    # Parameter 7, processing mode of flooding algorithm
    cv.floodFill(img_copy, mask, (20.20), (0.255.0),
                 (50.50.50), (100.100.100), cv.FLOODFILL_FIXED_RANGE)
    cv.imshow("color_demo", img_copy)


if __name__ == "__main__":
    src = cv.imread('./25.jpg')

    fill_color_demo(src)
    cv.waitKey()
    cv.destroyAllWindows()
Copy the code

The result is as follows:

Part of the explanation given for why pixels are +2 when setting the mask is that when a flood fill scan starts at row 0, column 0, the extra 2 in the mask ensures that all pixels at the scanned boundary are processed. Understand this for a moment.

For parameter 5 and parameter 6, find the following information:

Starting from the initial seed point, fill the connected pixels with the specified color. The connectivity depends on the color and brightness of adjacent pixels, which are part of the redrawn region in the following cases, as shown below.

The popular instructions are as follows:

  • (20, 20) : is the position of seed point;
  • (0, 255, 0) : indicates the flood filling color, green;
  • (50, 50, 50) : Based on the three-channel value of seed point pixel [B, G, R], the lowest value of the filled norm in the original three-channel value is [B-50, G-50, R-50];
  • (100, 100, 100) : Based on the three-channel value of seed point pixel [B, G, R], the maximum value of the filled norm in the original three-channel value is [B +100, G +100, R +100];
  • cv.FLOODFILL_FIXED_RANGE: The pixel to be processed is compared with the seed point, and the pixel is filled within the range.

In the original image, only the three-channel values of pixels [B-50, G-50, R-50] <=[b, G, r] <=[B +100, g+100, r+100] within this range will be filled with green (0, 255, 0).

Binary image fill

Look at the code first, and pay attention to the comments.

import cv2 as cv
import numpy as np

def fill_binary() :
    Create a 400 by 400 rectangle
    image = np.zeros([400.400.3], np.uint8)
    # Fill the interior with a white square

    image[100:300.100:300, :] = 255
    cv.imshow("fill_binary", image)

    # Set mask
    mask = np.ones([402.402], np.uint8)
    mask[101:301.101:301] = 0
    # mask 0 will not be filled. # mask 0 will be filled
    cv.floodFill(image, mask, (200.200), (255.255.0),
                 cv.FLOODFILL_MASK_ONLY)
    cv.imshow("filled_binary", image)

fill_binary()
cv.waitKey(0)
cv.destroyAllWindows()
Copy the code

FLOODFILL_MASK_ONLY is the value of FLOODFILL_MASK_ONLY, which indicates that the mask region is filled from the seed point.

OpenCV end

An hour has passed. Have you mastered the Python OpenCV related knowledge points?

As a beginner, there are many places to learn not in-depth, I hope you stick to it with me.

In your spare time, you can subscribe to eraser’s crawler course to learn crawler knowledge.


If you have ideas or techniques you’d like to share, feel free to leave them in the comments section.


Blogger ID: Dream eraser, hope you like, comment, favorites.