mask_to_maskref.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import numpy as np
  2. import cv2
  3. import os
  4. ###
  5. #
  6. # maskdet_to_maskfin
  7. #
  8. #
  9. ###
  10. # create_maskref ===============================================================
  11. # return:
  12. # maskref image
  13. def create_maskref(cv_mask, cv_correct):
  14. #Create a total green image
  15. green = np.zeros((512,512,3), np.uint8)
  16. green[:,:,:] = (0,255,0) # (B, G, R)
  17. #Define the green color filter
  18. f1 = np.asarray([0, 250, 0]) # green color filter
  19. f2 = np.asarray([10, 255, 10])
  20. #From mask, extrapolate only the green mask
  21. green_mask = cv2.inRange(cv_mask, f1, f2) #green is 0
  22. # (OPTIONAL) Apply dilate and open to mask
  23. kernel = np.ones((5,5),np.uint8) #Try change it?
  24. green_mask = cv2.dilate(green_mask, kernel, iterations = 1)
  25. #green_mask = cv2.morphologyEx(green_mask, cv2.MORPH_OPEN, kernel)
  26. # Create an inverted mask
  27. green_mask_inv = cv2.bitwise_not(green_mask)
  28. # Cut correct and green image, using the green_mask & green_mask_inv
  29. res1 = cv2.bitwise_and(cv_correct, cv_correct, mask = green_mask_inv)
  30. res2 = cv2.bitwise_and(green, green, mask = green_mask)
  31. # Compone:
  32. return cv2.add(res1, res2)