opencv-python learning (nine): picture filling - flood filling (floodFill)

1. Specify color fill

code show as below:

# 引入包
import cv2 as cv
import numpy as np

def fill_image(image):
    copyImage = image.copy() # 复制原图像
    h, w = image.shape[:2] # 读取图像的宽高
    mask = np.zeros([h+2, w+2], np.uint8) # 新建图像矩阵  +2是官方函数要求
    #(0,80) 起始点,(0,100,255) 蓝色 ,(100,100,50)棕色 ,(50,50,50) 浅黑
    cv.floodFill(copyImage, mask, (0,0), (0, 100, 255), (100, 100, 50), (50, 50, 50), cv.FLOODFILL_FIXED_RANGE)
    cv.namedWindow("fill", cv.WINDOW_NORMAL)
    cv.imshow("fill", copyImage)

src = cv.imread("./static/image/Linux.jpg")
cv.namedWindow("oldImage", cv.WINDOW_NORMAL)
cv.imshow("oldImage", src)
fill_image(src)
cv.waitKey(0)
cv.destroyAllWindows()

Code Explanation:
Idea
Flood Filling: It is to change the area connected with the seed point to a specific color, and the effect of filling can be controlled by setting the connection mode or the range of pixels. It is usually used to mark or separate a part of the image for processing or analysis, or to speed up the process through masking. You can only process the part specified by the mask or mask the area on the mask without processing.

The main function is to select points that are connected to the seed point and have similar colors, and process the value of the pixel point. If a mask is encountered, process it according to the mask.

work process:

  1. Selected seed point (x,y)
  2. Check the color of the seed point, if the color of the point is different from the color of the surrounding connection points, set the color of the surrounding points to the color of the point, if they are the same, do not process. But the surrounding points do not necessarily become the same color as the seed point, if the surrounding connection points are within a given range (lodiff - updiff) or within the pixel range of the seed point, the color will change.
  3. Detect other connection points and perform 2-step processing until there are no connection points, that is, the boundary of the detection area is reached and stopped.

Original: https://blog.csdn.net/u013539952/article/details/80702849
floodFill(image, mask, seedPoint, newVal, loDiff=None, upDiff=None, flags=None)
  • image: input/output] 1 or 3 channel, 8bit or floating point image. The image will not be modified only when the FLOODFILL_MASK_ONLY flag of the parameter flags is set, otherwise it will be modified.
  • mask: [Input/Output] Operation mask, which must be single-channel, 8bit, and 2 pixels wider and higher than the original image. The reason for adding 2 can be understood in this way: when the flood fill scan starts from 0 rows and 0 columns, the extra 2 in the mask can ensure that the pixels on the boundary of the scan will be processed. Must be initialized before use. When the flag is FLOORFILL_MAK_ONLY, only the area with a value of 0 in the mask will be filled.
  • seedPoint: The seed point for flood filling, the starting pixel point.
  • newVal: the new pixel value of the filled pixel
  • loDiff: The lower boundary difference added to the seed point area condition. Indicates the maximum negative difference in brightness or color between the currently observed pixel value and its neighboring pixel values ​​or the seed pixel value to be added.
  • upDiff: The upper bound difference added to the seed point area condition. Indicates the maximum positive difference in brightness or color between the currently observed pixel value and its neighboring pixel values ​​or the seed pixel value to be added.
  • flag: the processing mode of the flooding algorithm
    • When it is CV_FLOODFILL_FIXED_RANGE, the pixel to be processed is compared with the seed point, and if it is within the range, the pixel is filled. (change image)
    • CV_FLOODFILL_MASK_ONLY This bit sets the filled object. If this bit is set, the mask cannot be empty. At this time, the function does not fill the original image img, but fills the mask image.

Popular explanation: floodFill( 1. Operation image, 2. Mask, 3. Starting pixel value, 4. Filling color, 5. The lower boundary difference added to the seed point area condition, 6. Added to the seed point area condition The upper bound difference, 7. Filling method)

(100, 100, 50), (50, 50, 50) is the color difference range, my understanding is that if the difference is too large, all colors will be covered, that is, the entire picture will become thefill color of parameter 4

Example (800, 800, 800), (50, 50, 50) :
insert image description here

When the seed point is (0, 0) , the area with the same color as the seed point area will be filled with color
insert image description here

When the seed point is (68, 75) , the area of ​​the same pigment as the seed point area will be filled with color, that is, the letter L will be filled with the same color as the fill color
insert image description here


2. Fill in the specified position

code show as below:

def fill2_image():
    image = np.zeros([200, 200, 3], np.uint8)
    # image[100:300, 100:300, :] = 255
    cv.namedWindow("image", cv.WINDOW_NORMAL)
    cv.imshow("image", image)
    # mask 的高宽比原图多2,即h+2,w+2
    mask = np.ones([202, 202, 1], np.uint8)
    # mask需要填充的位置的像素设置为0,mask就是填充的指定位置。
    mask[100:150, 100:150] = 0
    # 调用floodFill函数填充 (0,0,255)指定红色填充
    cv.floodFill(image, mask, (100, 100), (0, 0, 255), cv.FLOODFILL_MASK_ONLY)
    cv.namedWindow("fill", cv.WINDOW_NORMAL)
    cv.imshow("fill", image)

fill2_image()
cv.waitKey(0)
cv.destroyAllWindows()

Populate the schematic
insert image description here

Guess you like

Origin blog.csdn.net/weixin_33538887/article/details/118386871