質問編集履歴
2
改善
title
CHANGED
|
File without changes
|
body
CHANGED
|
@@ -121,9 +121,7 @@
|
|
|
121
121
|
cv2.imshow('btarget', btarget)
|
|
122
122
|
cv2.waitKey(0)
|
|
123
123
|
cv2.destroyAllWindows()
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
124
|
+
|
|
127
125
|
|
|
128
126
|
|
|
129
127
|
|
1
書式の変更
title
CHANGED
|
File without changes
|
body
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
python3上で物体の画像抽出→大きさの計測を行うプログラムを作成しています。
|
|
5
5
|
使用:Jupyter Notebook
|
|
6
6
|
### 発生している問題・エラーメッセージ
|
|
7
|
-
|
|
7
|
+
```ここに言語を入力
|
|
8
8
|
ValueError Traceback (most recent call last)
|
|
9
9
|
<ipython-input-7-b16976612a1a> in <module>
|
|
10
10
|
161
|
|
@@ -26,7 +26,9 @@
|
|
|
26
26
|
20 x1 = 0
|
|
27
27
|
|
|
28
28
|
ValueError: too many values to unpack (expected 2)
|
|
29
|
+
```
|
|
29
|
-
#
|
|
30
|
+
#コード
|
|
31
|
+
```ここに言語を入力
|
|
30
32
|
import cv2
|
|
31
33
|
import math
|
|
32
34
|
import numpy as np
|
|
@@ -45,7 +47,7 @@
|
|
|
45
47
|
|
|
46
48
|
def getPoints(self, binimg):
|
|
47
49
|
h,w = binimg.shape
|
|
48
|
-
|
|
50
|
+
# 中央線より上をスキャン
|
|
49
51
|
x1 = 0
|
|
50
52
|
y1 = 0
|
|
51
53
|
for y in range(self._imy, 0, -1):
|
|
@@ -56,7 +58,7 @@
|
|
|
56
58
|
x1 = x
|
|
57
59
|
y1 = y
|
|
58
60
|
|
|
59
|
-
|
|
61
|
+
# 中央線より下をスキャン
|
|
60
62
|
x2 = 0
|
|
61
63
|
y2 = 0
|
|
62
64
|
for y in range(self._imy, h, 1):
|
|
@@ -76,14 +78,14 @@
|
|
|
76
78
|
_, bw = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
|
|
77
79
|
bw = cv2.bitwise_not(bw)
|
|
78
80
|
|
|
79
|
-
|
|
81
|
+
# モルフォロジー変換
|
|
80
82
|
kernel5x5 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
|
|
81
83
|
bw = cv2.morphologyEx(bw, cv2.MORPH_CLOSE, kernel5x5, iterations=2)
|
|
82
84
|
|
|
83
85
|
target_lb_id = 1
|
|
84
86
|
|
|
85
87
|
|
|
86
|
-
ラベリング
|
|
88
|
+
# ラベリング
|
|
87
89
|
nLabels, labelImages, data, center = cv2.connectedComponentsWithStats(bw)
|
|
88
90
|
tobjx = data[target_lb_id, 0]
|
|
89
91
|
tobjy = data[target_lb_id, 1]
|
|
@@ -99,12 +101,12 @@
|
|
|
99
101
|
|
|
100
102
|
|
|
101
103
|
|
|
102
|
-
|
|
104
|
+
# 輪郭取得
|
|
103
105
|
contours, _ = cv2.findContours(btarget, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
|
104
106
|
for i, cnt in enumerate(contours):
|
|
105
107
|
cv2.drawContours(res,[cnt],0,(0,0,255),1)
|
|
106
108
|
|
|
107
|
-
|
|
109
|
+
# 回帰計算
|
|
108
110
|
ypts, xpts = np.where(btarget == 255)
|
|
109
111
|
rv = np.polyfit(xpts ,ypts, 3)
|
|
110
112
|
expr = np.poly1d(rv)
|
|
@@ -112,15 +114,22 @@
|
|
|
112
114
|
v = expr(x)
|
|
113
115
|
res[int(v),x,:] = (255,255,0) # 曲線描画
|
|
114
116
|
|
|
115
|
-
|
|
117
|
+
# ターゲットオブジェクト認識領域描画
|
|
116
118
|
cv2.rectangle(res,(tobjx,tobjy),(tobjx+tobjw,tobjy+tobjh),(0,255,255),1)
|
|
117
119
|
cv2.imshow('res', res)
|
|
118
120
|
cv2.imshow('bw', bw)
|
|
119
121
|
cv2.imshow('btarget', btarget)
|
|
120
122
|
cv2.waitKey(0)
|
|
121
123
|
cv2.destroyAllWindows()
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
#ここより上はOK
|
|
122
127
|
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
123
|
-
|
|
132
|
+
# 枠を描画
|
|
124
133
|
cv2.rectangle(res,(tobjx,tobjy),(tobjx+tobjw,tobjy+tobjh),(0,255,255),1)
|
|
125
134
|
|
|
126
135
|
cv2.imshow('t', cv2.rectangle(res,(tobjx,tobjy),(tobjx+tobjw,tobjy+tobjh),(0,255,255),1))
|
|
@@ -128,8 +137,10 @@
|
|
|
128
137
|
cv2.destroyAllWindows()
|
|
129
138
|
|
|
130
139
|
|
|
131
|
-
|
|
140
|
+
# for x in range(tobjx, tobjx+tobjw):
|
|
141
|
+
# v = expr(x)
|
|
142
|
+
# res[int(v),x,:] = (255,255,0)
|
|
132
|
-
幅を測る
|
|
143
|
+
# 幅を測る
|
|
133
144
|
offsetx = 10;incx = 16;lbno = 1
|
|
134
145
|
cary = np.zeros((incx,), np.float32)
|
|
135
146
|
carx = np.zeros((incx,), np.float32)
|
|
@@ -141,11 +152,11 @@
|
|
|
141
152
|
cary[sx-x] = vy
|
|
142
153
|
rv1 = np.polyfit(carx ,cary,1)
|
|
143
154
|
expr1 = np.poly1d(rv1)
|
|
144
|
-
|
|
155
|
+
# スキャンセグメントの中間座標
|
|
145
156
|
mx = sx + incx // 2
|
|
146
157
|
my = expr(mx)
|
|
147
158
|
|
|
148
|
-
|
|
159
|
+
# 直行を求める
|
|
149
160
|
ex = WidthScanner(rv1, mx, my)
|
|
150
161
|
spt, ept, dist = ex.getPoints(res)
|
|
151
162
|
cv2.line(res, (int(spt[0]), int(spt[1])),
|
|
@@ -156,7 +167,7 @@
|
|
|
156
167
|
print('%2d.直径 %.2f' % (lbno, dist))
|
|
157
168
|
lbno += 1
|
|
158
169
|
|
|
159
|
-
|
|
170
|
+
# 長さを測る
|
|
160
171
|
total_len = 0
|
|
161
172
|
vy = expr(x)
|
|
162
173
|
scan_flag = False
|
|
@@ -181,6 +192,7 @@
|
|
|
181
192
|
|
|
182
193
|
if __name__ == '__main__':
|
|
183
194
|
main()
|
|
195
|
+
```
|
|
184
196
|
|
|
185
197
|
|
|
186
198
|
|