preface

Here we do not bite hard principle (principle can refer to Opencv[Posts and Telecommunications Press]).

I. Principle of flood water filling method

Flooding filling method is an important method for image segmentation in image processing. Its principle is to find the same area according to the difference between pixel gray values to achieve segmentation.

Two. Flood water filling method steps

2.1 choose seed point (x, y) 2.2 on seed points, 4 – neighborhood judging or 8 – neighborhood pixels with seed point pixel values of the difference, the difference is less than the threshold value of pixels to add into the area of 2.3 to add a new pixel as a new seed point, repeatedly perform the second step, until there is no new pixel point is added to the region \

3. Parameter Analysis:

floodFill(image,
           mask,
           seedPoint,
           newVal,
           loDiff=None,
           upDiff=None, 
           flags=None)
Copy the code

Mask: mask matrix. It is a single-channel image whose size is 2 larger and 2 larger than the input image. It is used to mark the filling area. SeedPoint newVal: the minimum matrix that is assigned to the seedPoint region loDiff: the lower difference of the conditions added to the seedPoint region upDiff: Add the upper difference value of the seed point area condition. When the difference between the seed point pixel value and the pixel value of a pixel point in the neighborhood is less than this value, the pixel point will be added to the region flags: The operation flag of flood filling method.

4. Design steps

4.1 Image reading 4.2 Generate random number for randomly generating pixels 4.3 Determine the connected domain mode 4.4 Value of mask image 4.5 Set the difference between selected pixels 4.6 Randomly generate a point pixel in the image 4.7 Pixel value filled in color image 4.8 Flood filling function \

5. The demo

There are some differences in procedures and design steps, but the single can still be completed.

import random import cv2 as cv import numpy as np # def fill_image(image): Mask = np.zeros([h + 2, w + 2], P = random. Randint (0, h) py = random. Randint (0, w) print("px", px, "py", px) cv.floodFill(copyImage, mask, (px, py), (255, 0, 0), (100, 100, 50), (50, 50, 50), flags=4 | (255 << 8) | cv.FLOODFILL_FIXED_RANGE) mask_copy = mask[1:h + 1, Mask_copy = CV. Merge ([mask_copy, mask_copy, mask_copy, SRC = CV. Bitwise_and (image, merge) SRC = CV. CV. NamedWindow ("fill", CV.WINDOW_NORMAL) CV. Imshow ("fill", CV. CV. NamedWindow ("and", CV.WINDOW_NORMAL) # CV. Imshow ("and", Image) return SRC SRC = cv.imread("0.jpg") # cv.namedWindow("raw", cv.WINDOW_NORMAL) fill_image(src)) cv.waitKey(0) cv.destroyAllWindows()Copy the code