回答編集履歴

2

d

2018/10/31 08:27

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -207,3 +207,51 @@
207
207
  print(within(point, rect)) # True
208
208
 
209
209
  ```
210
+
211
+
212
+
213
+ ## 追記
214
+
215
+
216
+
217
+ とりあえず、定義した四角の中にボールの中心が入っている場合は赤色、入っていない場合は青色になるように、以下の関数を変更してみました。
218
+
219
+ rect の4点の座標値はラインに合わせて変えてください。
220
+
221
+
222
+
223
+ ```python
224
+
225
+ def displayCircle(image, ballList, thickness=5):
226
+
227
+ # ここはラインに合わせて変えてください。
228
+
229
+ rect = np.array([[170, 113], [167, 57], [317, 50], [319, 105]])
230
+
231
+
232
+
233
+ for i in range(len(ballList)):
234
+
235
+ x = int(ballList[i][0])
236
+
237
+ y = int(ballList[i][1])
238
+
239
+
240
+
241
+ if cv2.pointPolygonTest(rect, (x, y), False) >= 0:
242
+
243
+ # 線の中にボールの中心が入っている赤色で表示。
244
+
245
+ cv2.circle(image, (x, y), 10, (0, 0, 255), thickness)
246
+
247
+ else:
248
+
249
+ # 線の中にボールの中心が入っていない場合、青色で表示。
250
+
251
+ cv2.circle(image, (x, y), 10, (255, 0, 0), thickness)
252
+
253
+
254
+
255
+ return image
256
+
257
+ ```

1

d

2018/10/31 08:27

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -171,3 +171,39 @@
171
171
  ball_pos.append([x, y])
172
172
 
173
173
  ```
174
+
175
+
176
+
177
+ ## 追記
178
+
179
+
180
+
181
+ タプルで表現された点が4点で構成される四角に含まれるかどうかは以下で判定すればよいかと思います。
182
+
183
+
184
+
185
+ ```python
186
+
187
+ import cv2
188
+
189
+
190
+
191
+ def within(point, rect):
192
+
193
+ '''点 point が四角形 rect に含まれているかどうか
194
+
195
+ '''
196
+
197
+ return cv2.pointPolygonTest(rect, point, False) >= 0
198
+
199
+
200
+
201
+ point = (200, 100)
202
+
203
+ rect = np.array([[170, 113], [167, 57], [317, 50], [319, 105]])
204
+
205
+
206
+
207
+ print(within(point, rect)) # True
208
+
209
+ ```