-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcv_kick.py
108 lines (70 loc) · 2.07 KB
/
cv_kick.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python
import cv2
import numpy as np
import time
import rospy
from std_msgs.msg import String
try:
index = sys.argv[1]
except:
index = 0
cap = cv2.VideoCapture(index)
y,u,v = 0,43,160
kernel = np.ones((5,5),np.uint8)
width = cap.get(3)
height= cap.get(4)
flag = 1
def detect():
global cap,y,u,v,kernel,width,height,flag
while True:
#get_image
ret,frame = cap.read()
#colour space conversion and masking
img_yuv = cv2.cvtColor(frame,cv2.COLOR_BGR2YUV)
mask = cv2.inRange(img_yuv, (np.array([0,u-45,v-45])), (np.array([255,u+45,v+45])))
#morphological_transformation_open
erode = cv2.erode(mask,kernel,iterations = 1)
dilate = cv2.dilate(erode,kernel,iterations = 1)
#morphological_transformation_close
erode = cv2.erode(mask,kernel,iterations = 1)
dilate = cv2.dilate(erode,kernel,iterations = 1)
#contour detection
image,contour,hierarchy = cv2.findContours(dilate,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(frame, contour, -1, (0,255,0), 2)
#waitKey init
if cv2.waitKey(5) == 27:
flag = 0
#contour operations
if contour:
#cnt = contour[0]
cnt = max(contour, key = cv2.contourArea) #getting contour with max area
(x,y),radius = cv2.minEnclosingCircle(cnt) #calculating center of the ball
x,y,radius = int(x),int(y),int(radius)
cv2.circle(frame,(x,y),radius,(0,255,0),2) #drawing circle across contour
area = radius*radius*3.14
'''if cnt:
return x,y,1 # return {1 = detected(in range), 0 = (detected not in range), -1 = not detected}
else:
return "0"
'''
print "area: "area," ","x: "x," ","y: ",y
else:
#return "-1"
print "Contour Nahi He"
cv2.imshow("Masking",mask)
cv2.imshow("YUV",frame)
cap.release()
cv2.destroyAllWindows()
def talker() :
pub=rospy.Publisher('detect',String,queue_size=10)
rospy.init_node('talker',anonymous=True)
rate=rospy.Rate(10)
while not rospy.is_shutdown() :
msg = detect()
pub.publish(msg)
time.sleep(0.1)
if __name__=="__main__" :
try :
talker()
except rospy.ROSInterruptException :
pass