-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy patheasycv2.py
154 lines (116 loc) · 5.21 KB
/
easycv2.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# import the necessary packages
import numpy as np
import argparse
import cv2
import time
def Calibrate(threshold):
cap = cv2.VideoCapture(0) # Set Capture Device, in case of a USB Webcam try 1, or give -1 to get a list of available devices
tryloop = 0
found = 0
cx = [0,0,0,0]
cy = [0,0,0,0]
while tryloop < 20:
ret, frame = cap.read()
# load the image, clone it for output, and then convert it to grayscale
output = frame.copy()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# apply GuassianBlur to reduce noise. medianBlur is also added for smoothening, reducing noise.
gray = cv2.GaussianBlur(gray,(5,5),0);
gray = cv2.medianBlur(gray,5)
# Adaptive Guassian Threshold is to detect sharp edges in the Image. For more information Google it.
gray = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,3.5)
kernel = np.ones((2.6,2.7),np.uint8)
gray = cv2.erode(gray,kernel,iterations = 1)
# gray = erosion
gray = cv2.dilate(gray,kernel,iterations = 1)
# gray = dilation
# get the size of the final image
# img_size = gray.shape
# print img_size
# detect circles in the image
#circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 260, param1=30, param2=65, minRadius=0, maxRadius=0)
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 260, param1=60, param2=threshold, minRadius=0, maxRadius=0)
# ensure at least some circles were found
if circles is not None:
# convert the (x, y) coordinates and radius of the circles to integers
circles = np.round(circles[0, :]).astype("int")
print (len(circles))
if len(circles) >1:
# loop over the (x, y) coordinates and radius of the circles
c = 0
found = 1
for (x, y, r) in circles:
# draw the circle in the output image, then draw a rectangle in the image
# corresponding to the center of the circle
print ("C: {} \t\t X:{} \t Y:{} \t R:{}".format(c,x,y,r))
cx[c] = x
cy[c] = y
c = c + 1
tryloop = tryloop + 1
if found ==1:
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
return found
def getCircle(threshold):
cap = cv2.VideoCapture(0) # Set Capture Device, in case of a USB Webcam try 1, or give -1 to get a list of available devices
tryloop = 0
found = 0
xt = 0
yt = 0
rt = 0
x = 0
x = 0
r = 0
while tryloop < 20:
ret, frame = cap.read()
# load the image, clone it for output, and then convert it to grayscale
output = frame.copy()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# apply GuassianBlur to reduce noise. medianBlur is also added for smoothening, reducing noise.
gray = cv2.GaussianBlur(gray,(5,5),0);
gray = cv2.medianBlur(gray,5)
# Adaptive Guassian Threshold is to detect sharp edges in the Image. For more information Google it.
gray = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,3.5)
kernel = np.ones((2.6,2.7),np.uint8)
gray = cv2.erode(gray,kernel,iterations = 1)
# gray = erosion
gray = cv2.dilate(gray,kernel,iterations = 1)
# gray = dilation
# get the size of the final image
# img_size = gray.shape
# print img_size
# detect circles in the image
#circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 260, param1=30, param2=65, minRadius=0, maxRadius=0)
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 260, param1=60, param2=threshold, minRadius=0, maxRadius=0)
# ensure at least some circles were found
if circles is not None:
# convert the (x, y) coordinates and radius of the circles to integers
circles = np.round(circles[0, :]).astype("int")
print (len(circles))
if len(circles) == 1:
# loop over the (x, y) coordinates and radius of the circles
for (x, y, r) in circles:
# draw the circle in the output image, then draw a rectangle in the image
# corresponding to the center of the circle
print ("C: {} \t\t X:{} \t Y:{} \t R:{}".format(c,x,y,r))
found = found+ 1
xt = xt + x
yt = yt + y
rt = rt + r
tryloop = tryloop + 1
if found > 2:
x = xt / found
y = yt / found
r = rt / found
print ("X:{} \t Y:{} \t R:{}".format(x,y,r))
else:
x = 0
y = 0
r = 0
print ("No circle detected")
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
return x,y,r