-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebcam_h_c_detector.py
160 lines (134 loc) · 5.01 KB
/
webcam_h_c_detector.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
154
155
156
157
158
159
160
#This script written to detect half circle objects with webcam.
#I chose red and half circled objects.You can chose your filter too, please write Read.me on git.
#@ dasmehdix
import cv2
import numpy as np
def pixDis(a1,b1,a2,b2):
#distance between points(pixels)
y = b2-b1
x = a2-a1
return np.sqrt(x*x+y*y)
def nothing(x):
# any operation
pass
cap = cv2.VideoCapture(0)
cv2.namedWindow("Trackbars")
cv2.createTrackbar("L-H", "Trackbars", 62, 180, nothing)
cv2.createTrackbar("L-S", "Trackbars", 66, 255, nothing)
cv2.createTrackbar("L-V", "Trackbars", 134, 255, nothing)
cv2.createTrackbar("U-H", "Trackbars", 180, 255, nothing)
cv2.createTrackbar("U-S", "Trackbars", 255, 255, nothing)
cv2.createTrackbar("U-V", "Trackbars", 243, 255, nothing)
font = cv2.FONT_HERSHEY_COMPLEX
global center
while True:
ret, frame = cap.read()
frame = cv2.GaussianBlur(frame,(5,5),0)
gray= cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
l_h = cv2.getTrackbarPos("L-H", "Trackbars")
l_s = cv2.getTrackbarPos("L-S", "Trackbars")
l_v = cv2.getTrackbarPos("L-V", "Trackbars")
u_h = cv2.getTrackbarPos("U-H", "Trackbars")
u_s = cv2.getTrackbarPos("U-S", "Trackbars")
u_v = cv2.getTrackbarPos("U-V", "Trackbars")
# lower mask (0-10)
''' lower_red = np.array([0,50,50])
upper_red = np.array([10,255,255])
mask0 = cv2.inRange(hsv, lower_red, upper_red)
# upper mask (170-180)
lower_red = np.array([170,50,50])
upper_red = np.array([180,255,255])
mask1 = cv2.inRange(hsv, lower_red, upper_red)
mask = mask0+mask1 '''
lower_red = np.array([l_h, l_s, l_v])
upper_red = np.array([u_h, u_s, u_v])
mask = cv2.inRange(hsv, lower_red, upper_red)
kernel = np.ones((5, 5), np.uint8)
mask = cv2.dilate(mask, kernel)
mask[np.where(mask==0)] = 0
#mask = cv2.Canny(mask,50,100)
# Contours detection
if int(cv2.__version__[0]) > 3:
# Opencv 4.x.x
contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
else:
# Opencv 3.x.x
_, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
oldArea = 0
oldCnt = 0
for cnt in contours:
area = cv2.contourArea(cnt)
approx = cv2.approxPolyDP(cnt, 0.02*cv2.arcLength(cnt, True), True)
diameter = np.sqrt(4*area/np.pi)
x = approx.ravel()[0]
y = approx.ravel()[1]
convvex = cv2.isContourConvex(cnt)
#print(len(cnt))
if cv2.arcLength(cnt,True) != 0:
circularity = (4*np.pi*area)/((cv2.arcLength(cnt,True))*(cv2.arcLength(cnt,True)))
if area > 500 and 0.5 <=float(circularity) <= 0.7 and 4< len(approx) <= 7 and convvex == False:
#roundness = (4*area)/(np.pi*diameter*diameter)
#print(float(roundness))
x1, y1 ,w1, h1 = cv2.boundingRect(cnt)
''' print(cv2.boundingRect(cnt))
cv2.rectangle(frame,(x1-20,y1-20),(x1+w1+20,y1+h1+20),(0,255,0),2) '''
x2 = x1 + w1
y2 = y1 + h1
if x1<0:
x1=1
if y1<0:
y1=1
if x2>639:
x2 = 639
if y2>479:
y2=479
gray = gray[(y1):(y2),(x1):(x2)]
if gray.shape[0] != 0:
if gray.shape[1] != 0:
if y2>y1:
if x2>x1:
''' print(gray.shape)
cv2.imshow("ngray", gray) '''
rows = gray.shape[0]
#cv2.imshow("daire",gray)
circles = cv2.HoughCircles(gray,cv2.HOUGH_GRADIENT,1,rows/8,param1=50,param2=15,minRadius=15,maxRadius=120)
if circles is not None:
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
center = (i[0]+x1, i[1]+y1)
radius = i[2]
#cv2.circle(frame, center, radius, (111, 23, 111), 3)
''' if len(approx.ravel()) <= 12:
yayDif = pixDis(approx.ravel()[0],approx.ravel()[1],approx.ravel()[4],approx.ravel()[5])
#yaydif cemberde ilk ve 3. nokta arasındaki mesafe ref ise olması gerek uzunluğu
referance = np.sqrt(2)*pixDis(approx.ravel()[0],approx.ravel()[1],approx.ravel()[8],approx.ravel()[9])
if referance-2<yayDif<referance+2: '''
if area >= oldArea:
M = cv2.moments(cnt)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
if abs(center[0]-cX) < 40:
#This place is where we understand that it's a half circle
cv2.drawContours(frame, [approx], 0, (0, 0, 0), 5)
cv2.line(frame,(cX,cY),(cX,cY-60),(0,255,0),thickness=2)
cv2.line(frame,(cX,cY),(cX+60,cY),(255,0,0),thickness=2)
cv2.line(frame,(cX,cY),(cX-30,cY+30),(0,0,255),thickness=2)
cv2.putText(frame,"Yarim Daire Bulundu",(x,y),font,1,(100,244,237),thickness=2)
oldArea = area
oldCnt = cnt
''' if len(approx) == 3:
cv2.putText(frame, "Triangle", (x, y), font, 1, (0, 0, 0))
elif len(approx) == 4:
cv2.putText(frame, "Rectangle", (x, y), font, 1, (0, 0, 0))
elif len(approx) == 5:
cv2.putText(frame,"Half-Circle",(x,y),font,1,(0,255,0))
elif 10 < len(approx) < 20:
cv2.putText(frame, "Circle", (x, y), font, 1, (0, 0, 0)) '''
cv2.imshow("FrameR", frame)
cv2.imshow("Maske", mask)
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()