質問編集履歴

6

tuiki

2019/12/03 03:36

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -140,11 +140,11 @@
140
140
 
141
141
  ```
142
142
 
143
- 追記のコード
143
+ 面積
144
144
 
145
145
  ```ここに言語を入力
146
146
 
147
- # -*- coding: utf-8 -*-
147
+ -*- coding: utf-8 -*-
148
148
 
149
149
  import cv2
150
150
 
@@ -154,72 +154,44 @@
154
154
 
155
155
 
156
156
 
157
- def find_rect_of_target_color(img):
157
+ img = cv2.imread("/home/pi/picture/alarm9-1.png")
158
158
 
159
- hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
159
+ hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV_FULL)
160
160
 
161
- h, s, v = cv2.split(hsv)
161
+ h, s, v = cv2.split(hsv)
162
162
 
163
163
 
164
164
 
165
- mask = np.zeros_like(h)
165
+ mask = np.zeros_like(h)
166
166
 
167
- mask[(h > 240) & ((100 < s) & (s < 200)) & (v > 180)] = 255
167
+ mask[(h > 240) & ((100 < s) & (s < 200)) & (v > 180)] = 255
168
168
 
169
169
 
170
170
 
171
171
 
172
172
 
173
- # 輪郭を抽出する。
173
+ # 輪郭を抽出する。
174
174
 
175
- mask, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
175
+ mask, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
176
176
 
177
177
 
178
178
 
179
- # 各輪郭の面積を出力する。(デバッグ用)
179
+ # 各輪郭の面積を出力する。(デバッグ用)
180
180
 
181
- print(list(map(cv2.contourArea, contours)))
181
+ print(list(map(cv2.contourArea, contours)))
182
182
 
183
183
 
184
184
 
185
- # 指定した範囲の面積を持つ輪郭のみ、抽出する。
185
+ cv2.waitKey(0)
186
186
 
187
- contours = list(filter(lambda x: 10 <= cv2.contourArea(x) <= 10000, contours))
188
-
189
-
190
-
191
- # 輪郭に外接する長方形を取得する。
192
-
193
- rects = map(cv2.boundingRect, contours)
194
-
195
-
196
-
197
- return rects
198
-
199
-
200
-
201
-
202
-
203
- img = cv2.imread("/home/pi/picture/alarm9-1.png")
204
-
205
-
206
-
207
- rects = find_rect_of_target_color(img)
208
-
209
-
210
-
211
- for x, y, w, h in rects:
212
-
213
- cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255), thickness=2)
214
-
215
-
216
-
217
- cv2.imwrite("result.png", img)
187
+ cv2.destroyAllWindows()
218
-
219
-
220
188
 
221
189
  ```
222
190
 
223
191
  ・出力結果
224
192
 
225
- ![イメージ説明](e47861350c8fdeb65f5449abe880c8b6.png)
193
+ ```ここに言語を入力
194
+
195
+ [1.5, 0.0, 0.0, 3.0, 2.5, 0.0, 0.5, 7.5, 49.0, 12.5, 0.0, 3.5, 0.0, 0.0, 0.0, 0.0, 4.5, 0.0, 0.0, 3.5, 19.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.5, 0.0, 0.5, 1.0, 3.5, 5052.0, 8.5, 5.5, 2.0, 4.0, 4.0, 15.0, 25.5, 2.0, 8.5, 26.0, 5.5, 18.5, 2.0, 4.0, 44.5, 13.5, 33.0, 2.0, 2.0, 22.5, 7.0, 6.0, 5.5, 4.0, 14.5, 8.5, 2.0, 4.0, 2.0, 831.5, 4.5, 0.0, 0.0, 2.0, 4.0, 10.0, 6.0, 2.0, 6.0, 9.5, 2.0, 2.0, 17.0, 14.0, 12.0, 4.0, 2.0, 61.5, 4.0, 4.0, 23.0, 14.5, 4.0, 2.0, 11.5, 2.0, 2.0, 7.5, 43.5, 4.0, 13.0, 4.0, 2.0, 11.0, 4.0, 2.0, 6.0, 4.0, 2.0, 5.5, 6.0, 11.5]
196
+
197
+ ```

5

追記した

2019/12/03 03:36

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -4,12 +4,6 @@
4
4
 
5
5
 
6
6
 
7
- ○ 追記
8
-
9
-  試しにコードを書いてみて実行したのですが、下に載せている写真のように何も起こらないで、動画が流れるわけでもなく、どのキーを押しても反応しなくなりました。コードが違うのか、それ以外に問題があるのかよくわからないので教えていただきたいです。
10
-
11
-
12
-
13
7
  ```ここに言語を入力
14
8
 
15
9
  import cv2
@@ -146,6 +140,86 @@
146
140
 
147
141
  ```
148
142
 
143
+ ・追記のコード
144
+
145
+ ```ここに言語を入力
146
+
147
+ # -*- coding: utf-8 -*-
148
+
149
+ import cv2
150
+
151
+ import numpy as np
152
+
153
+
154
+
155
+
156
+
157
+ def find_rect_of_target_color(img):
158
+
159
+ hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
160
+
161
+ h, s, v = cv2.split(hsv)
162
+
163
+
164
+
165
+ mask = np.zeros_like(h)
166
+
167
+ mask[(h > 240) & ((100 < s) & (s < 200)) & (v > 180)] = 255
168
+
169
+
170
+
171
+
172
+
173
+ # 輪郭を抽出する。
174
+
175
+ mask, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
176
+
177
+
178
+
179
+ # 各輪郭の面積を出力する。(デバッグ用)
180
+
181
+ print(list(map(cv2.contourArea, contours)))
182
+
183
+
184
+
185
+ # 指定した範囲の面積を持つ輪郭のみ、抽出する。
186
+
187
+ contours = list(filter(lambda x: 10 <= cv2.contourArea(x) <= 10000, contours))
188
+
189
+
190
+
191
+ # 輪郭に外接する長方形を取得する。
192
+
193
+ rects = map(cv2.boundingRect, contours)
194
+
195
+
196
+
197
+ return rects
198
+
199
+
200
+
201
+
202
+
203
+ img = cv2.imread("/home/pi/picture/alarm9-1.png")
204
+
205
+
206
+
207
+ rects = find_rect_of_target_color(img)
208
+
209
+
210
+
211
+ for x, y, w, h in rects:
212
+
213
+ cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255), thickness=2)
214
+
215
+
216
+
217
+ cv2.imwrite("result.png", img)
218
+
219
+
220
+
221
+ ```
222
+
149
- ・結果
223
+ 出力結果
150
-
224
+
151
- ![イメージ説明](7e8bc077d5cfa6f4d04d9fb2c9ca6ae3.png)
225
+ ![イメージ説明](e47861350c8fdeb65f5449abe880c8b6.png)

4

sasa

2019/12/02 08:26

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -136,7 +136,7 @@
136
136
 
137
137
  cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), thickness=2)
138
138
 
139
- cv2.imshow('red', frame)
139
+ cv2.imshow('red', frame)
140
140
 
141
141
  capture.release()
142
142
 

3

イラン部分消した

2019/11/27 07:26

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -69,80 +69,6 @@
69
69
  capture.release()
70
70
 
71
71
  cv2.destroyAllWindows()
72
-
73
- ```
74
-
75
-
76
-
77
- ```ここに言語を入力
78
-
79
- import cv2
80
-
81
- import numpy as np
82
-
83
-
84
-
85
- def find_rect_of_target_color(image):
86
-
87
- hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV_FULL)
88
-
89
- h = hsv[:, :, 0]
90
-
91
- s = hsv[:, :, 1]
92
-
93
- v = hsv[:, :, 2]
94
-
95
- mask = np.zeros(h.shape, dtype=np.uint8)
96
-
97
- mask[(h > 240) & ((100 < s) & (s < 200)) & (v > 180)] = 255
98
-
99
- mask, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
100
-
101
- rects = []
102
-
103
- for contour in contours:
104
-
105
- approx = cv2.convexHull(contour)
106
-
107
- rect = cv2.boundingRect(approx)
108
-
109
- rects.append(np.array(rect))
110
-
111
- return rects
112
-
113
-
114
-
115
- capture = cv2.VideoCapture(0)
116
-
117
- while cv2.waitKey(30) < 0:
118
-
119
- _, frame = capture.read()
120
-
121
- rects = find_rect_of_target_color(frame)
122
-
123
- for x, y, w, h in rects:
124
-
125
- area = w * h
126
-
127
- if area < 100:
128
-
129
- continue
130
-
131
- elif area >500:
132
-
133
- continue
134
-
135
- cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), thickness=2)
136
-
137
- cv2.imshow('red', frame)
138
-
139
- capture.release()
140
-
141
- cv2.destroyAllWindows()
142
-
143
-
144
-
145
-
146
72
 
147
73
  ```
148
74
 

2

ついき

2019/11/27 04:42

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- 面積を指定して動画からその面積を持つ特定色のものを認識したい
1
+ 面積(画素数)を指定して動画からその面積(画素数)を持つ特定色のものを認識したい
test CHANGED
File without changes

1

追記した

2019/11/27 03:59

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -4,6 +4,12 @@
4
4
 
5
5
 
6
6
 
7
+ ○ 追記
8
+
9
+  試しにコードを書いてみて実行したのですが、下に載せている写真のように何も起こらないで、動画が流れるわけでもなく、どのキーを押しても反応しなくなりました。コードが違うのか、それ以外に問題があるのかよくわからないので教えていただきたいです。
10
+
11
+
12
+
7
13
  ```ここに言語を入力
8
14
 
9
15
  import cv2
@@ -65,3 +71,155 @@
65
71
  cv2.destroyAllWindows()
66
72
 
67
73
  ```
74
+
75
+
76
+
77
+ ```ここに言語を入力
78
+
79
+ import cv2
80
+
81
+ import numpy as np
82
+
83
+
84
+
85
+ def find_rect_of_target_color(image):
86
+
87
+ hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV_FULL)
88
+
89
+ h = hsv[:, :, 0]
90
+
91
+ s = hsv[:, :, 1]
92
+
93
+ v = hsv[:, :, 2]
94
+
95
+ mask = np.zeros(h.shape, dtype=np.uint8)
96
+
97
+ mask[(h > 240) & ((100 < s) & (s < 200)) & (v > 180)] = 255
98
+
99
+ mask, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
100
+
101
+ rects = []
102
+
103
+ for contour in contours:
104
+
105
+ approx = cv2.convexHull(contour)
106
+
107
+ rect = cv2.boundingRect(approx)
108
+
109
+ rects.append(np.array(rect))
110
+
111
+ return rects
112
+
113
+
114
+
115
+ capture = cv2.VideoCapture(0)
116
+
117
+ while cv2.waitKey(30) < 0:
118
+
119
+ _, frame = capture.read()
120
+
121
+ rects = find_rect_of_target_color(frame)
122
+
123
+ for x, y, w, h in rects:
124
+
125
+ area = w * h
126
+
127
+ if area < 100:
128
+
129
+ continue
130
+
131
+ elif area >500:
132
+
133
+ continue
134
+
135
+ cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), thickness=2)
136
+
137
+ cv2.imshow('red', frame)
138
+
139
+ capture.release()
140
+
141
+ cv2.destroyAllWindows()
142
+
143
+
144
+
145
+
146
+
147
+ ```
148
+
149
+
150
+
151
+ ・試したコード
152
+
153
+ ```ここに言語を入力
154
+
155
+ import cv2
156
+
157
+ import numpy as np
158
+
159
+
160
+
161
+ def find_rect_of_target_color(image):
162
+
163
+ hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV_FULL)
164
+
165
+ h = hsv[:, :, 0]
166
+
167
+ s = hsv[:, :, 1]
168
+
169
+ v = hsv[:, :, 2]
170
+
171
+ mask = np.zeros(h.shape, dtype=np.uint8)
172
+
173
+ mask[(h > 240) & ((100 < s) & (s < 200)) & (v > 180)] = 255
174
+
175
+ mask, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
176
+
177
+ rects = []
178
+
179
+ for contour in contours:
180
+
181
+ approx = cv2.convexHull(contour)
182
+
183
+ rect = cv2.boundingRect(approx)
184
+
185
+ rects.append(np.array(rect))
186
+
187
+ return rects
188
+
189
+
190
+
191
+ capture = cv2.VideoCapture(0)
192
+
193
+ while cv2.waitKey(30) < 0:
194
+
195
+ _, frame = capture.read()
196
+
197
+ rects = find_rect_of_target_color(frame)
198
+
199
+ for x, y, w, h in rects:
200
+
201
+ area = w * h
202
+
203
+ if area < 100:
204
+
205
+ continue
206
+
207
+ elif area >500:
208
+
209
+ continue
210
+
211
+ cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), thickness=2)
212
+
213
+ cv2.imshow('red', frame)
214
+
215
+ capture.release()
216
+
217
+ cv2.destroyAllWindows()
218
+
219
+
220
+
221
+ ```
222
+
223
+ ・結果
224
+
225
+ ![イメージ説明](7e8bc077d5cfa6f4d04d9fb2c9ca6ae3.png)