import cv2 import numpy as np def fill_color_demo(image): copyIma = image.copy() h, w = image.shape[:2] print(h, w) mask = np.zeros([h+2, w+2], np.uint8) #cv2.floodFill(image, mask, seedPoint, newVal, loDiff, upDiff, flags) cv2.floodFill(copyIma, mask, (30, 30), (0, 0, 0), (100, 100, 100), (50, 50, 50), cv2.FLOODFILL_FIXED_RANGE) cv2.imshow("fill_color", copyIma) copyIma = cv2.cvtColor(copyIma, cv2.COLOR_BGR2GRAY) ret, mask = cv2.threshold(copyIma, 1, 255, cv2.THRESH_BINARY) cv2.imshow("threshold", mask) mask_inv = cv2.bitwise_not(mask) cv2.imshow("bitwise_not", mask_inv) src = cv2.imread("test2.jpg") cv2.namedWindow("input image", cv2.WINDOW_AUTOSIZE) cv2.imshow("input image", src) fill_color_demo(src) cv2.waitKey(0) cv2.destroyAllWindows()