opencv_floodfill.py 827 B

123456789101112131415161718192021222324
  1. import cv2
  2. import numpy as np
  3. def fill_color_demo(image):
  4. copyIma = image.copy()
  5. h, w = image.shape[:2]
  6. print(h, w)
  7. mask = np.zeros([h+2, w+2], np.uint8)
  8. #cv2.floodFill(image, mask, seedPoint, newVal, loDiff, upDiff, flags)
  9. cv2.floodFill(copyIma, mask, (30, 30), (0, 0, 0), (100, 100, 100), (50, 50, 50), cv2.FLOODFILL_FIXED_RANGE)
  10. cv2.imshow("fill_color", copyIma)
  11. copyIma = cv2.cvtColor(copyIma, cv2.COLOR_BGR2GRAY)
  12. ret, mask = cv2.threshold(copyIma, 1, 255, cv2.THRESH_BINARY)
  13. cv2.imshow("threshold", mask)
  14. mask_inv = cv2.bitwise_not(mask)
  15. cv2.imshow("bitwise_not", mask_inv)
  16. src = cv2.imread("test2.jpg")
  17. cv2.namedWindow("input image", cv2.WINDOW_AUTOSIZE)
  18. cv2.imshow("input image", src)
  19. fill_color_demo(src)
  20. cv2.waitKey(0)
  21. cv2.destroyAllWindows()