質問編集履歴

1

文を全て書いた

2017/09/01 05:12

投稿

kee
kee

スコア7

test CHANGED
File without changes
test CHANGED
@@ -10,19 +10,147 @@
10
10
 
11
11
  ###該当のソースコード
12
12
 
13
- ~~~~~~~
14
13
 
15
- ~~~~~~~
16
14
 
17
- green_image = extract_color(image,
18
15
 
19
- for num in range(40,81):
20
16
 
21
- print(num),0,90,45)
22
17
 
23
- ~~~~~~~
24
18
 
19
+
20
+
21
+
22
+
23
+ import cv2
24
+
25
+
26
+
27
+ def extract_color( src, h_th_low, h_th_up, s_th, v_th ):
28
+
29
+
30
+
31
+ hsv = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)
32
+
33
+ h, s, v = cv2.split(hsv)
34
+
35
+
36
+
37
+ if h_th_low > h_th_up:
38
+
39
+ ret, h_dst_1 = cv2.threshold(h, h_th_low, 255, cv2.THRESH_BINARY)
40
+
41
+ ret, h_dst_2 = cv2.threshold(h, h_th_up, 255, cv2.THRESH_BINARY_INV)
42
+
43
+
44
+
45
+ dst = cv2.bitwise_or(h_dst_1, h_dst_2)
46
+
47
+
48
+
49
+ else:
50
+
51
+ ret, dst = cv2.threshold(h, h_th_low, 255, cv2.THRESH_TOZERO)
52
+
53
+ ret, dst = cv2.threshold(dst, h_th_up, 255, cv2.THRESH_TOZERO_INV)
54
+
55
+
56
+
57
+ ret, dst = cv2.threshold(dst, 0, 255, cv2.THRESH_BINARY)
58
+
59
+
60
+
61
+ ret, s_dst = cv2.threshold(s, s_th, 255, cv2.THRESH_BINARY)
62
+
63
+ ret, v_dst = cv2.threshold(v, v_th, 255, cv2.THRESH_BINARY)
64
+
65
+
66
+
67
+ dst = cv2.bitwise_and(dst, s_dst)
68
+
69
+ dst = cv2.bitwise_and(dst, v_dst)
70
+
71
+
72
+
73
+ return dst
74
+
75
+
76
+
77
+ if __name__=="__main__":
78
+
79
+
80
+
81
+ capture = cv2.VideoCapture(0)
82
+
83
+
84
+
85
+ if capture.isOpened() is False:
86
+
87
+
88
+
89
+ raise("IO Error")
90
+
91
+
92
+
93
+ cv2.namedWindow("Capture", cv2.WINDOW_AUTOSIZE)
94
+
95
+ #cv2.namedWindow("Red", cv2.WINDOW_AUTOSIZE)
96
+
97
+ #cv2.namedWindow("yellow", cv2.WINDOW_AUTOSIZE)
98
+
99
+ cv2.namedWindow("green", cv2.WINDOW_AUTOSIZE)
100
+
101
+
102
+
103
+ while True:
104
+
105
+
106
+
107
+ ret, image = capture.read()
108
+
109
+ if ret == False:
110
+
25
- ~~~~~~~
111
+ continue
112
+
113
+
114
+
115
+ #red_image = extract_color(image, 170, 5, 190, 200)
116
+
117
+ #yellow_image = extract_color(image, 10, 25, 50, 50)
118
+
119
+ green_image = extract_color(image,
120
+
121
+ for num in range(40, 81):
122
+
123
+ print(num), 0, 90, 45)
124
+
125
+
126
+
127
+
128
+
129
+
130
+
131
+ cv2.imshow("Capture", image)
132
+
133
+ #cv2.imshow("Red", red_image)
134
+
135
+ #cv2.imshow("Yellow", yellow_image)
136
+
137
+ cv2.imshow("Green", green_image)
138
+
139
+
140
+
141
+ if cv2.waitKey(33) >= 0:
142
+
143
+ #cv2.imwrite("image.png", image)
144
+
145
+ #cv2.imwrite("red_image.png", red_image)
146
+
147
+ #cv2.imwrite("yellow_image.png", yellow_image)
148
+
149
+ #cv2.imwrite("green_image.png", green_image)
150
+
151
+
152
+
153
+ break
26
154
 
27
155
  ###試したこと
28
156