質問するログイン新規登録

質問編集履歴

1

文を全て書いた

2017/09/01 05:12

投稿

kee
kee

スコア7

title CHANGED
File without changes
body CHANGED
@@ -4,13 +4,77 @@
4
4
  SyntaxError: invalid syntax
5
5
 
6
6
  ###該当のソースコード
7
+
8
+
9
+
10
+
11
+
12
+ import cv2
13
+
14
+ def extract_color( src, h_th_low, h_th_up, s_th, v_th ):
15
+
16
+ hsv = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)
17
+ h, s, v = cv2.split(hsv)
18
+
19
+ if h_th_low > h_th_up:
20
+ ret, h_dst_1 = cv2.threshold(h, h_th_low, 255, cv2.THRESH_BINARY)
21
+ ret, h_dst_2 = cv2.threshold(h, h_th_up, 255, cv2.THRESH_BINARY_INV)
22
+
23
+ dst = cv2.bitwise_or(h_dst_1, h_dst_2)
24
+
25
+ else:
26
+ ret, dst = cv2.threshold(h, h_th_low, 255, cv2.THRESH_TOZERO)
27
+ ret, dst = cv2.threshold(dst, h_th_up, 255, cv2.THRESH_TOZERO_INV)
28
+
29
+ ret, dst = cv2.threshold(dst, 0, 255, cv2.THRESH_BINARY)
30
+
31
+ ret, s_dst = cv2.threshold(s, s_th, 255, cv2.THRESH_BINARY)
32
+ ret, v_dst = cv2.threshold(v, v_th, 255, cv2.THRESH_BINARY)
33
+
34
+ dst = cv2.bitwise_and(dst, s_dst)
35
+ dst = cv2.bitwise_and(dst, v_dst)
36
+
37
+ return dst
38
+
39
+ if __name__=="__main__":
40
+
41
+ capture = cv2.VideoCapture(0)
42
+
43
+ if capture.isOpened() is False:
44
+
45
+ raise("IO Error")
46
+
47
+ cv2.namedWindow("Capture", cv2.WINDOW_AUTOSIZE)
48
+ #cv2.namedWindow("Red", cv2.WINDOW_AUTOSIZE)
49
+ #cv2.namedWindow("yellow", cv2.WINDOW_AUTOSIZE)
50
+ cv2.namedWindow("green", cv2.WINDOW_AUTOSIZE)
51
+
52
+ while True:
53
+
54
+ ret, image = capture.read()
55
+ if ret == False:
7
- ~~~~~~~
56
+ continue
57
+
58
+ #red_image = extract_color(image, 170, 5, 190, 200)
8
- ~~~~~~~
59
+ #yellow_image = extract_color(image, 10, 25, 50, 50)
9
- green_image = extract_color(image,
60
+ green_image = extract_color(image,
10
- for num in range(40,81):
61
+ for num in range(40, 81):
11
- print(num),0,90,45)
62
+ print(num), 0, 90, 45)
63
+
64
+
65
+
66
+ cv2.imshow("Capture", image)
67
+ #cv2.imshow("Red", red_image)
68
+ #cv2.imshow("Yellow", yellow_image)
69
+ cv2.imshow("Green", green_image)
70
+
71
+ if cv2.waitKey(33) >= 0:
72
+ #cv2.imwrite("image.png", image)
73
+ #cv2.imwrite("red_image.png", red_image)
74
+ #cv2.imwrite("yellow_image.png", yellow_image)
75
+ #cv2.imwrite("green_image.png", green_image)
76
+
12
- ~~~~~~~
77
+ break
13
- ~~~~~~~
14
78
  ###試したこと
15
79
  空白などを確認した。
16
80