Advertisement
Guest User

eurobot2019_v1

a guest
Feb 23rd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.18 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4. # TODO: add exceptions for when no contours are found
  5. # TODO: rewrite beginning for raspberry pi
  6. # TODO: add auto exposure and gain functions
  7.  
  8. CIRCLE_COLORS = {
  9.     # TODO: better color ranges
  10.     'red1': {'min': (0, 30, 30), 'max': (15, 255, 255), 'draw': (0, 0, 255)},
  11.     'red2': {'min': (245, 30, 30), 'max': (255, 255, 255), 'draw': (0, 0, 255)},
  12.     'green': {'min': (35, 30, 30), 'max': (70, 255, 255), 'draw':(0, 255, 0)},
  13.     'blue':  {'min': (80, 30, 30), 'max': (140, 255, 255), 'draw': (255, 0, 0)},
  14. }
  15.  
  16.  
  17. def start_video(cap):
  18.     return cap.read()
  19.  
  20.  
  21. def start_image(cap):
  22.     return False, cv2.imread(r"C:\Users\Z1\Desktop\eurobot2019.png")
  23.  
  24.  
  25. def check_contour_area(contour, min_area=0, max_area=200000):
  26.     return max_area > cv2.contourArea(contour) > min_area
  27.  
  28.  
  29. def is_perspective_circle(contour, max_ratio=5, min_ratio=0.5):
  30.     _, _, w, h = cv2.boundingRect(contour)
  31.     return max_ratio * w >= h >= min_ratio * w
  32.  
  33.  
  34. def filter_contours(color_mask):
  35.     filtered_contours = []
  36.     contours, hierarchy = cv2.findContours(color_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
  37.     for contour in contours:
  38.         if check_contour_area(contour) and is_perspective_circle(contour):
  39.             filtered_contours.append(contour)
  40.     filtered_contours = sorted(filtered_contours, key=cv2.contourArea)
  41.     return filtered_contours[0]
  42.  
  43.  
  44. def find_center(color_mask):
  45.     contour = filter_contours(color_mask)
  46.     x, y, w, h = cv2.boundingRect(contour)
  47.     return int(x+w/2), int(y+h/2)
  48.  
  49.  
  50. cap = cv2.VideoCapture(0)
  51. ret = True
  52. while ret:
  53.     # read & split images
  54.     ret, image = start_image(cap) # change to start_video(cap) for video streaming
  55.  
  56.     image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  57.     image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
  58.  
  59.     blue_mask = cv2.inRange(image_hsv, CIRCLE_COLORS["blue"]["min"], CIRCLE_COLORS["blue"]["max"])
  60.     green_mask = cv2.inRange(image_hsv, CIRCLE_COLORS["green"]["min"], CIRCLE_COLORS["green"]["max"])
  61.     red1_mask = cv2.inRange(image_hsv, CIRCLE_COLORS["red1"]["min"], CIRCLE_COLORS["red1"]["max"])
  62.     red2_mask = cv2.inRange(image_hsv, CIRCLE_COLORS["red2"]["min"], CIRCLE_COLORS["red2"]["max"])
  63.     red_mask = cv2.bitwise_or(red1_mask, red2_mask)
  64.  
  65.     # remove noise
  66.     kernel = np.ones((7, 7), np.uint8)
  67.     blue_mask = cv2.morphologyEx(blue_mask, cv2.MORPH_OPEN, kernel, iterations=1)
  68.     green_mask = cv2.morphologyEx(green_mask, cv2.MORPH_OPEN, kernel, iterations=1)
  69.     red_mask = cv2.morphologyEx(red_mask, cv2.MORPH_OPEN, kernel, iterations=1)
  70.  
  71.     # blur
  72.     kernel = np.ones((3, 3), np.uint8)
  73.     blue_mask = cv2.morphologyEx(blue_mask, cv2.MORPH_DILATE, kernel, iterations=10)
  74.     green_mask = cv2.morphologyEx(green_mask, cv2.MORPH_DILATE, kernel, iterations=10)
  75.     red_mask = cv2.morphologyEx(red_mask, cv2.MORPH_DILATE, kernel, iterations=10)
  76.  
  77.     blue_mask = cv2.morphologyEx(blue_mask, cv2.MORPH_ERODE, kernel, iterations=10)
  78.     green_mask = cv2.morphologyEx(green_mask, cv2.MORPH_ERODE, kernel, iterations=10)
  79.     red_mask = cv2.morphologyEx(red_mask, cv2.MORPH_ERODE, kernel, iterations=10)
  80.  
  81.     # find centers of pucks
  82.     blue_center = find_center(blue_mask)
  83.     green_center = find_center(green_mask)
  84.     red_center = find_center(red_mask)
  85.  
  86.     # crop original
  87.     blue_image = cv2.bitwise_and(image, image, mask=blue_mask)
  88.     green_image = cv2.bitwise_and(image, image, mask=green_mask)
  89.     red_image = cv2.bitwise_and(image, image, mask=red_mask)
  90.  
  91.     # paint centers to original
  92.     cv2.circle(image, blue_center, 5, CIRCLE_COLORS["blue"]["draw"], -1)
  93.     cv2.circle(image, green_center, 5, CIRCLE_COLORS["green"]["draw"], -1)
  94.     cv2.circle(image, red_center, 5, CIRCLE_COLORS["red1"]["draw"], -1)
  95.  
  96.     cv2.circle(blue_image, blue_center, 5, CIRCLE_COLORS["blue"]["draw"], 2)
  97.     cv2.circle(green_image, green_center, 5, CIRCLE_COLORS["green"]["draw"], 2)
  98.     cv2.circle(red_image, red_center, 5, CIRCLE_COLORS["red1"]["draw"], 2)
  99.  
  100.     # show images
  101.     cv2.imshow("orig",image)
  102.     cv2.imshow("blue", blue_image)
  103.     cv2.imshow("green", green_image)
  104.     cv2.imshow("red", red_image)
  105.  
  106.     cv2.waitKey(0)
  107.  
  108.  
  109. cv2.waitKey(1)
  110. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement