-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathball_recognition.py
66 lines (53 loc) · 2.19 KB
/
ball_recognition.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import cv2
import numpy as np
import imutils
def recalibrate(event, x, y, flags, param):
global lower_orange
global upper_orange
if event == cv2.EVENT_LBUTTONDOWN:
hsv_color = hsv[y, x]
lower_orange = (hsv_color - np.array([4, 100, 30])).clip(0, 255)
upper_orange = (hsv_color + np.array([4, 255, 255])).clip(0, 255)
print(lower_orange)
print(upper_orange)
cap = cv2.VideoCapture(0)
cv2.namedWindow("frame", cv2.WINDOW_NORMAL)
cv2.setMouseCallback("frame", recalibrate)
lower_orange = np.array([13, 63, 255])
upper_orange = np.array([21, 255, 255])
while True:
if cap.isOpened():
_, rgb = cap.read()
rgb_blur = cv2.GaussianBlur(rgb, (5, 5), 0)
hsv = cv2.cvtColor(rgb_blur, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower_orange, upper_orange)
mask = cv2.erode(mask, None, iterations=4)
mask = cv2.dilate(mask, None, iterations=4)
output = cv2.bitwise_and(rgb_blur, rgb_blur, mask=mask)
contours = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(contours)
center = None
# only proceed if at least one contour was found
if len(contours) > 0:
# find the largest contour in the mask, then use
# it to compute the minimum enclosing circle and
# centroid
c = max(contours, key=cv2.contourArea)
((x, y), radius) = cv2.minEnclosingCircle(c)
M = cv2.moments(c)
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
# only proceed if the radius meets a minimum size
if radius > 10:
# draw the circle and centroid on the frame,
# then update the list of tracked points
cv2.circle(rgb_blur, (int(x), int(y)), int(radius), (0, 255, 255), 2)
cv2.circle(rgb_blur, center, 5, (0, 0, 255), -1)
cv2.imshow('frame', rgb_blur)
cv2.imshow('mask', mask)
cv2.imshow('output', output)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
print("Error opening VideoCapture.")
cap.release()
cv2.destroyAllWindows()