-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy path1285.py
233 lines (173 loc) · 8.05 KB
/
1285.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import cv2 as cv
import numpy as np
color1 = 0
color2 = 0
ranges = 20
set_color = False
step = 0
def nothing(x):
global color1, color2
global lower_blueA1, lower_blueA2, lower_blueA3
global upper_blueA1, upper_blueA2, upper_blueA3
global lower_blueB1, lower_blueB2, lower_blueB3
global upper_blueB1, upper_blueB2, upper_blueB3
saturation_th1 = cv.getTrackbarPos('saturation_th1', 'img_result')
value_th1 = cv.getTrackbarPos('value_th1', 'img_result')
saturation_th2 = cv.getTrackbarPos('saturation_th2', 'img_result')
value_th2 = cv.getTrackbarPos('value_th2', 'img_result')
color1 = int(color1)
color2 = int(color2)
# HSV 색공간에서 마우스 클릭으로 얻은 픽셀값과 유사한 필셀값의 범위를 정합니다.
if color1 < ranges:
lower_blueA1 = np.array([color1 - ranges + 180, saturation_th1, value_th1])
upper_blueA1 = np.array([180, 255, 255])
lower_blueA2 = np.array([0, saturation_th1, value_th1])
upper_blueA2 = np.array([color1, 255, 255])
lower_blueA3 = np.array([color1, saturation_th1, value_th1])
upper_blueA3 = np.array([color1 + ranges, 255, 255])
# print(i-range+180, 180, 0, i)
# print(i, i+range)
elif color1 > 180 - ranges:
lower_blueA1 = np.array([color1, saturation_th1, value_th1])
upper_blueA1 = np.array([180, 255, 255])
lower_blueA2 = np.array([0, saturation_th1, value_th1])
upper_blueA2 = np.array([color1 + ranges - 180, 255, 255])
lower_blueA3 = np.array([color1 - ranges, saturation_th1, value_th1])
upper_blueA3 = np.array([color1, 255, 255])
# print(i, 180, 0, i+range-180)
# print(i-range, i)
else:
lower_blueA1 = np.array([color1, saturation_th1, value_th1])
upper_blueA1 = np.array([color1 + ranges, 255, 255])
lower_blueA2 = np.array([color1 - ranges, saturation_th1, value_th1])
upper_blueA2 = np.array([color1, 255, 255])
lower_blueA3 = np.array([color1 - ranges, saturation_th1, value_th1])
upper_blueA3 = np.array([color1, 255, 255])
# print(i, i+range)
# print(i-range, i)
if color2 < ranges:
lower_blueB1 = np.array([color2 - ranges + 180, saturation_th2, value_th2])
upper_blueB1 = np.array([180, 255, 255])
lower_blueB2 = np.array([0, saturation_th2, value_th2])
upper_blueB2 = np.array([color2, 255, 255])
lower_blueB3 = np.array([color2, saturation_th2, value_th2])
upper_blueB3 = np.array([color2 + ranges, 255, 255])
# print(i-range+180, 180, 0, i)
# print(i, i+range)
elif color2 > 180 - ranges:
lower_blueB1 = np.array([color2, saturation_th2, value_th2])
upper_blueB1 = np.array([180, 255, 255])
lower_blueB2 = np.array([0, saturation_th2, value_th2])
upper_blueB2 = np.array([color2 + ranges - 180, 255, 255])
lower_blueB3 = np.array([color2 - ranges, saturation_th2, value_th2])
upper_blueB3 = np.array([color2, 255, 255])
# print(i, 180, 0, i+range-180)
# print(i-range, i)
else:
lower_blueB1 = np.array([color2, saturation_th2, value_th2])
upper_blueB1 = np.array([color2 + ranges, 255, 255])
lower_blueB2 = np.array([color2 - ranges, saturation_th2, value_th2])
upper_blueB2 = np.array([color2, 255, 255])
lower_blueB3 = np.array([color2 - ranges, saturation_th2, value_th2])
upper_blueB3 = np.array([color2, 255, 255])
# print(i, i+range)
# print(i-range, i)
cv.namedWindow('img_color')
cv.namedWindow('img_result')
cv.createTrackbar('saturation_th1', 'img_result', 0, 255, nothing)
cv.setTrackbarPos('saturation_th1', 'img_result', 30)
cv.createTrackbar('value_th1', 'img_result', 0, 255, nothing)
cv.setTrackbarPos('value_th1', 'img_result', 30)
cv.createTrackbar('saturation_th2', 'img_result', 0, 255, nothing)
cv.setTrackbarPos('saturation_th2', 'img_result', 30)
cv.createTrackbar('value_th2', 'img_result', 0, 255, nothing)
cv.setTrackbarPos('value_th2', 'img_result', 30)
cap = cv.VideoCapture(0)
while(True):
ret,img_color = cap.read()
img_color = cv.flip(img_color, 1)
if ret == False:
continue;
img_color2 = img_color.copy()
img_hsv = cv.cvtColor(img_color2, cv.COLOR_BGR2HSV)
height, width = img_color.shape[:2]
cx = int(width / 2)
cy = int(height / 2)
if set_color == False:
rectangle_color = (0, 255, 0)
if step == 1:
rectangle_color = (0, 0, 255)
cv.rectangle(img_color, (cx - 20, cy - 20), (cx + 20, cy + 20), rectangle_color, 5)
else:
# 범위 값으로 HSV 이미지에서 마스크를 생성합니다.
img_maskA1 = cv.inRange(img_hsv, lower_blueA1, upper_blueA1)
img_maskA2 = cv.inRange(img_hsv, lower_blueA2, upper_blueA2)
img_maskA3 = cv.inRange(img_hsv, lower_blueA3, upper_blueA3)
temp = cv.bitwise_or(img_maskA1, img_maskA2)
img_maskA = cv.bitwise_or(img_maskA3, temp)
img_maskB1 = cv.inRange(img_hsv, lower_blueB1, upper_blueB1)
img_maskB2 = cv.inRange(img_hsv, lower_blueB2, upper_blueB2)
img_maskB3 = cv.inRange(img_hsv, lower_blueB3, upper_blueB3)
temp = cv.bitwise_or(img_maskB1, img_maskB2)
img_maskB = cv.bitwise_or(temp, img_maskB3)
# 모폴로지 연산
kernel = np.ones((11,11), np.uint8)
img_maskA = cv.morphologyEx(img_maskA, cv.MORPH_OPEN, kernel)
img_maskA = cv.morphologyEx(img_maskA, cv.MORPH_CLOSE, kernel)
kernel = np.ones((11,11), np.uint8)
img_maskB = cv.morphologyEx(img_maskB, cv.MORPH_OPEN, kernel)
img_maskB = cv.morphologyEx(img_maskB, cv.MORPH_CLOSE, kernel)
# 마스크 이미지로 원본 이미지에서 범위값에 해당되는 영상 부분을 획득합니다.
img_maskC = cv.bitwise_or(img_maskA, img_maskB)
img_result = cv.bitwise_and(img_color, img_color, mask=img_maskC)
# 라벨링
numOfLabelsA, img_labelA, statsA, centroidsA = cv.connectedComponentsWithStats(img_maskA)
for idx, centroid in enumerate(centroidsA):
if statsA[idx][0] == 0 and statsA[idx][1] == 0:
continue
if np.any(np.isnan(centroid)):
continue
x,y,width,height,area = statsA[idx]
centerX,centerY = int(centroid[0]), int(centroid[1])
if area > 1500:
cv.circle(img_color, (centerX, centerY), 10, (0,0,255), 10)
cv.rectangle(img_color, (x,y), (x+width,y+height), (0,0,255))
numOfLabelsB, img_labelB, statsB, centroidsB = cv.connectedComponentsWithStats(img_maskB)
for idx, centroid in enumerate(centroidsB):
if statsB[idx][0] == 0 and statsB[idx][1] == 0:
continue
if np.any(np.isnan(centroid)):
continue
x,y,width,height,area = statsB[idx]
centerX,centerY = int(centroid[0]), int(centroid[1])
if area > 1500:
cv.circle(img_color, (centerX, centerY), 10, (255,0,0), 10)
cv.rectangle(img_color, (x,y), (x+width,y+height), (255,0,0))
cv.imshow('img_result', img_result)
cv.imshow('img_color', img_color)
key = cv.waitKey(1) & 0xFF
if key == 27: # esc
break
elif key == 32: # space
if step == 0:
roi = img_color2[cy-20:cy+20, cx-20:cx+20]
roi = cv.medianBlur(roi, 3)
cv.imshow("roi1", roi)
hsv = cv.cvtColor(roi, cv.COLOR_BGR2HSV)
h,s,v = cv.split(hsv)
color1 = h.mean()
print(color1)
step += 1
elif step == 1:
roi = img_color2[cy-20:cy+20, cx-20:cx+20]
roi = cv.medianBlur(roi, 3)
cv.imshow("roi2", roi)
hsv = cv.cvtColor(roi, cv.COLOR_BGR2HSV)
h,s,v = cv.split(hsv)
color2 = h.mean()
set_color = True
nothing(0)
print(color2)
step += 1
cap.release()
cv.destroyAllWindows()