質問編集履歴
1
エラーメッセージを追加しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,19 +1,62 @@
|
|
1
|
-
以前こちら
|
2
|
-
|
1
|
+
下記の画像から青色の矢印の座標を特定し、最小の円を描画するソフトを作りました。
|
3
|
-
|
2
|
+
その円の中心座標と半径をcsvに保存したいのですが、以下のようなエラーが出ます。
|
3
|
+
改善点を教えていただきたいです。
|
4
4
|

|
5
|
-
|
5
|
+

|
6
|
+
|
7
|
+
|
6
8
|
```Python
|
9
|
+
# -*- coding:utf-8 -*-
|
7
|
-
|
10
|
+
import cv2
|
11
|
+
import numpy as np
|
12
|
+
import sys
|
8
13
|
|
14
|
+
|
15
|
+
|
16
|
+
# 青色の検出
|
17
|
+
def detect_blue_color(img):
|
18
|
+
# HSV色空間に変換
|
19
|
+
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
|
20
|
+
|
21
|
+
# 青色のHSVの値域1
|
22
|
+
hsv_min = np.array([40,140,120])
|
23
|
+
hsv_max = np.array([150,255,255])
|
24
|
+
|
25
|
+
# 青色領域のマスク(255:赤色、0:赤色以外)
|
26
|
+
mask = cv2.inRange(hsv, hsv_min, hsv_max)
|
27
|
+
|
28
|
+
# マスキング処理
|
29
|
+
masked_img = cv2.bitwise_and(img, img, mask=mask)
|
30
|
+
|
31
|
+
return mask, masked_img
|
32
|
+
|
33
|
+
for i in range(2, 35):
|
34
|
+
img = cv2.imread('CoD1/filename_{0:05d}.png'.format(i))
|
35
|
+
|
36
|
+
# 色検出(赤、緑、青)
|
37
|
+
mask, masked_img = detect_blue_color(img)
|
38
|
+
|
39
|
+
# 輪郭を抽出する。
|
40
|
+
contours, hierarchy = cv2.findContours(
|
41
|
+
mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
|
42
|
+
)
|
43
|
+
|
44
|
+
# 小さい輪郭は誤検出として削除する
|
45
|
+
contours = list(filter(lambda x: cv2.contourArea(x) > 10, contours))
|
46
|
+
|
47
|
+
|
9
48
|
def get_center(contour):
|
49
|
+
"""輪郭の中心を取得する。
|
50
|
+
"""
|
51
|
+
# 輪郭のモーメントを計算する。
|
52
|
+
M = cv2.moments(contour)
|
53
|
+
# モーメントから重心を計算する。
|
54
|
+
cx = M["m10"] / M["m00"]
|
55
|
+
cy = M["m01"] / M["m00"]
|
10
56
|
|
11
|
-
|
57
|
+
return cx, cy
|
12
58
|
|
13
|
-
#return cx, 127-cy
|
14
|
-
return cx, cy # 修正
|
15
|
-
|
16
|
-
|
59
|
+
# 輪郭の中心を取り出す。
|
17
60
|
centers = [get_center(x) for x in contours]
|
18
61
|
|
19
62
|
# 確認用に各中心を描画
|
@@ -25,15 +68,17 @@
|
|
25
68
|
cs = centers.reshape(len(centers), 1, 2).astype(int) # カタチを整える
|
26
69
|
(x,y),radius = cv2.minEnclosingCircle(cs)
|
27
70
|
print(x,y,radius) # 中心座標と半径
|
71
|
+
|
28
72
|
|
29
|
-
|
73
|
+
# 結果を CSV で保存する。
|
30
|
-
img = cv2.circle( img, (int(x), int(y)), int(radius), (255,0,0), 1)
|
31
|
-
|
74
|
+
with open('centers2.csv','a') as f_handle:
|
75
|
+
np.savetxt(f_handle, (x,y), radius , delimiter=",", fmt="%.0f")
|
32
76
|
```
|
33
77
|
|
34
|
-
|
78
|
+
エラーコード
|
79
|
+
96.0 93.0 9.999999747378752e-05
|
80
|
+
Traceback (most recent call last):
|
35
|
-
|
81
|
+
File "Blue.py", line 67, in <module>
|
36
|
-
のようになります。
|
37
|
-
|
38
|
-
|
82
|
+
np.savetxt(f_handle, (x,y), radius , delimiter=",", fmt="%.0f")
|
39
|
-
|
83
|
+
File "<__array_function__ internals>", line 4, in savetxt
|
84
|
+
TypeError: _savetxt_dispatcher() got multiple values for argument 'fmt'
|