-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzzugumi
42 lines (31 loc) · 1.08 KB
/
zzugumi
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
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(1):
ret, frame = cap.read()
frame = cv2.flip(frame, 1)
frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_color= (84,81,133)
upper_color=(119,201,255)
frame_mask = cv2.inRange(frame_hsv,lower_color,upper_color)
kernel = np.ones((5, 5), np.uint8)
frame_mask = cv2.erode(frame_mask, kernel, iterations=1)
frame_mask = cv2.dilate(frame_mask, kernel, iterations=1)
frame_mask = cv2.GaussianBlur(frame_mask, (3, 3), 0)
contours, hierarchy = cv2.findContours(frame_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
areas = [cv2.contourArea(c) for c in contours]
max_index = np.argmax(areas)
cnt = contours[max_index]
cv2.drawContours(frame, cnt, -1, (255, 0, 0), 5)
M = cv2.moments(cnt)
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
cv2.circle(frame, (cx, cy), 5, (0,0,255), -1)
print(cx)
cv2.imshow('frame',frame)
cv2.imshow('mask', frame_mask)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
cap.release()