Every day 1 Hours ,365 It's a miracle .
Learning today Python OpenCV Flood filling knowledge , Flood filling is also called flood filling algorithm .
The principle is Start with a pixel , A point that meets the pixel requirements nearby , Fill all in the specified color , Until it comes to the point where it doesn't meet the requirements .
If you remember conceptually , There are four common neighborhood pixel filling method , Eight neighborhood pixel filling method , Pixel filling method based on scan line .
For these concepts , Skip first , Before the establishment of the whole cognition , There's no value in learning any basic concepts .
The syntax prototype of flood fill is as follows ：
floodFill(image, mask, seedPoint, newVal[, loDiff[, upDiff[, flags]]]) -> retval, image, mask, rect
This function has seven parameters , They are as follows ：
FLOODFILL_FIXED_RANGESpecify color fill
About flags And find a clear explanation ：
When it comes to
CV_FLOODFILL_FIXED_RANGE when , The pixel to be processed is compared with the seed point , If meet
(s – loDiff, s + upDiff) Between (s Is the pixel value of the seed point ), Then fill in ;
When it comes to
CV_FLOODFILL_MASK_ONLY when , be mask Can't be empty , here , Function does not fill the original image img, It's filling in the mask image .
The test picture is as follows ：
The test code is as follows ：
import cv2 as cv import numpy as np # Color image filling def fill_color_demo(src): img_copy = src.copy() h, w, ch = src.shape # Declare a rectangular shape , Notice that the height and width increase 2 Pixel # np.zeros Returns a function of a given shape and type 0 Filled array mask = np.zeros([h+2, w+2], np.uint8) # Parameters 1, Images to be filled with flooding # Parameters 2, A mask , Using a mask can specify in which area the algorithm is used , If you want to use it for the whole image , The mask size is the number of lines in the original image + 2, Number of columns + 2 # A mask , It's a two-dimensional 0 matrix , Because only the mask corresponds to 0 The location of the flood # Parameters 3, Flood filling seed points , Based on the pixels of the point, judge the pixels of similar colors , Whether it is flooded or not # Parameters 4, New colors for flood areas （BGR Format ） # Parameters 5, The seed pixel can be down the pixel value # Parameters 6, Seed point pixel can be up pixel value # Parameters 7, The 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()
The results are as follows ：
About setting the mask , Why do pixels
+2, The explanation given in part is ： When from 0 That's ok 0 Column begins flooding fill scan ,mask Extra 2 It can ensure that the pixels on the boundary of scanning will be processed . Let's understand for a moment .
About parameters 5 With the parameters 6, Find the following information ：
Start from the starting seed point , Fill the connected pixels with the specified color . Connectivity depends on the color and brightness of adjacent pixels , Pixels belong to the repainted area in the following cases , The formula is as follows .
The popular explanation is as follows ：
cv.FLOODFILL_FIXED_RANGE： The pixel to be processed is compared with the seed point , Within the scope of , Then fill this pixel .
In the original image, there are only 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 designated green (0, 255, 0) fill .
Look at the code first , Pay attention to the notes .
import cv2 as cv import numpy as np def fill_binary(): # Set up a 400*400 The rectangular image = np.zeros([400, 400, 3], np.uint8) # Fill the inside with a white square image[100:300, 100:300, :] = 255 cv.imshow("fill_binary", image) # Set the mask mask = np.ones([402, 402], np.uint8) mask[101:301, 101:301] = 0 # mask Not for 0 The area of is not filled ,mask by 0 It's the area that's filled in 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()
This part of the code comes from the Internet , Focus on understanding
FLOODFILL_MASK_ONLY that will do , This value indicates starting from the seed point , Fill the mask area .
1 Two hours later , Yes Python OpenCV Related knowledge , Have you got it ？
As a beginner , There are still many places where learning is not in-depth , I hope you will stick with me .
In my spare time , You can subscribe to the 100 cases of crawler course of eraser to learn the knowledge of crawler .
Want to learn Python Reptiles , You can subscribe to the eraser column ~ Click to find the surprise
Today is the first day of continuous writing 59 / 100 God .
If you have ideas you want to communicate 、 technology , Feel free to leave a comment in the comments section .
If you want to build a close relationship with a blogger , You can follow bloggers , Or focus on Blogger official account “
Non undergraduate programmer”, Learn how a non undergraduate programmer grows up .
Dream eraser, I hope you like 、 Comment on 、 Collection