here is a example code showing usage of cv2.floodfill function
import cv2
import numpy as np
import os
def imshow(img):
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread('test4.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV,3,1)
_, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE)
mask = np.zeros(img.shape[:-1],np.uint8)
cv2.drawContours(mask,contours,-1,(255,255,255),-1)
height, width = img.shape[:-1]
mask1 = np.zeros((height+2, width+2), np.uint8) # line 26
cv2.floodFill(mask,mask1,(0,0),255) # line 27
mask_inv=cv2.bitwise_not(mask)
imshow(mask_inv)
i am using this function in one of me projects but i am not understanding the mask1 part of the code (line 26 and line 27 ) that is,
why do we create the mask1 with shape h+2, w+2 for a given image with height-'h' and width-'w'?(line 26)
why do we have to pass this mask1 to the cv2.floodfill function?(line 27)
here is the input and output of the example code.
please help


CV_EXPORTS_Win front rather than justCV_EXPORTS)Noneformask1if you're not using the parameter.(h+2, w+2)is just so that the underlying function doesn't need to create a new padded mask each time the function is called (for the border pixels). For repeated calls, whichfloodFill()is made to do, it is better to just create the padded mask once before calling.