質問編集履歴
2
改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -244,144 +244,140 @@
|
|
244
244
|
|
245
245
|
cv2.destroyAllWindows()
|
246
246
|
|
247
|
+
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
# 枠を描画
|
260
|
+
|
261
|
+
cv2.rectangle(res,(tobjx,tobjy),(tobjx+tobjw,tobjy+tobjh),(0,255,255),1)
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
cv2.imshow('t', cv2.rectangle(res,(tobjx,tobjy),(tobjx+tobjw,tobjy+tobjh),(0,255,255),1))
|
266
|
+
|
267
|
+
cv2.waitKey(0)
|
268
|
+
|
269
|
+
cv2.destroyAllWindows()
|
270
|
+
|
247
271
|
|
248
272
|
|
273
|
+
|
274
|
+
|
275
|
+
# for x in range(tobjx, tobjx+tobjw):
|
276
|
+
|
277
|
+
# v = expr(x)
|
278
|
+
|
279
|
+
# res[int(v),x,:] = (255,255,0)
|
280
|
+
|
281
|
+
# 幅を測る
|
282
|
+
|
283
|
+
offsetx = 10;incx = 16;lbno = 1
|
284
|
+
|
285
|
+
cary = np.zeros((incx,), np.float32)
|
286
|
+
|
287
|
+
carx = np.zeros((incx,), np.float32)
|
288
|
+
|
289
|
+
for sx in range(tobjx+incx, tobjx+tobjw-incx, incx):
|
290
|
+
|
291
|
+
cary = np.zeros((incx,), np.uint8)
|
292
|
+
|
293
|
+
for x in range(sx, sx+incx):
|
294
|
+
|
295
|
+
vy = expr(x)
|
296
|
+
|
297
|
+
carx[sx-x] = x
|
298
|
+
|
299
|
+
cary[sx-x] = vy
|
300
|
+
|
301
|
+
rv1 = np.polyfit(carx ,cary,1)
|
302
|
+
|
303
|
+
expr1 = np.poly1d(rv1)
|
304
|
+
|
305
|
+
# スキャンセグメントの中間座標
|
306
|
+
|
307
|
+
mx = sx + incx // 2
|
308
|
+
|
309
|
+
my = expr(mx)
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
# 直行を求める
|
314
|
+
|
315
|
+
ex = WidthScanner(rv1, mx, my)
|
316
|
+
|
317
|
+
spt, ept, dist = ex.getPoints(res)
|
318
|
+
|
319
|
+
cv2.line(res, (int(spt[0]), int(spt[1])),
|
320
|
+
|
321
|
+
(int(ept[0]), int(ept[1])), (255,0,0), 1, lineType=cv2.LINE_AA)
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
cv2.putText(res, str(lbno), (int(spt[0]-6), int(spt[1])-8),
|
326
|
+
|
327
|
+
cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0,255,255), 1,cv2.LINE_AA)
|
328
|
+
|
329
|
+
print('%2d.直径 %.2f' % (lbno, dist))
|
330
|
+
|
331
|
+
lbno += 1
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
# 長さを測る
|
336
|
+
|
337
|
+
total_len = 0
|
338
|
+
|
339
|
+
vy = expr(x)
|
340
|
+
|
341
|
+
scan_flag = False
|
342
|
+
|
343
|
+
by = 0
|
344
|
+
|
345
|
+
for x in range(tobjx, tobjx+tobjw):
|
346
|
+
|
347
|
+
if scan_flag == False :
|
348
|
+
|
349
|
+
bx = x
|
350
|
+
|
351
|
+
by = vy
|
352
|
+
|
353
|
+
elif scan_flag == True :
|
354
|
+
|
355
|
+
if res[int(vy), x] == 255:
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
sdist = math.sqrt((bx - x)** 2 + (by - vy) ** 2)
|
360
|
+
|
361
|
+
total_len += sdist
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
cv2.line(res, (int(x), int(vy)), (int(bx), int(by)), (255,0,255), 2, lineType=cv2.LINE_AA)
|
368
|
+
|
369
|
+
bx = x
|
370
|
+
|
371
|
+
by = vy
|
372
|
+
|
373
|
+
else:
|
374
|
+
|
375
|
+
break
|
376
|
+
|
377
|
+
print('トータル長(pixel)=%.2f' %(total_len))
|
378
|
+
|
249
379
|
|
250
380
|
|
251
|
-
#ここより上はOK
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
# 枠を描画
|
264
|
-
|
265
|
-
cv2.rectangle(res,(tobjx,tobjy),(tobjx+tobjw,tobjy+tobjh),(0,255,255),1)
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
cv2.imshow('t', cv2.rectangle(res,(tobjx,tobjy),(tobjx+tobjw,tobjy+tobjh),(0,255,255),1))
|
270
|
-
|
271
|
-
cv2.waitKey(0)
|
272
|
-
|
273
|
-
cv2.destroyAllWindows()
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
# for x in range(tobjx, tobjx+tobjw):
|
280
|
-
|
281
|
-
# v = expr(x)
|
282
|
-
|
283
|
-
# res[int(v),x,:] = (255,255,0)
|
284
|
-
|
285
|
-
# 幅を測る
|
286
|
-
|
287
|
-
offsetx = 10;incx = 16;lbno = 1
|
288
|
-
|
289
|
-
cary = np.zeros((incx,), np.float32)
|
290
|
-
|
291
|
-
carx = np.zeros((incx,), np.float32)
|
292
|
-
|
293
|
-
for sx in range(tobjx+incx, tobjx+tobjw-incx, incx):
|
294
|
-
|
295
|
-
cary = np.zeros((incx,), np.uint8)
|
296
|
-
|
297
|
-
for x in range(sx, sx+incx):
|
298
|
-
|
299
|
-
vy = expr(x)
|
300
|
-
|
301
|
-
carx[sx-x] = x
|
302
|
-
|
303
|
-
cary[sx-x] = vy
|
304
|
-
|
305
|
-
rv1 = np.polyfit(carx ,cary,1)
|
306
|
-
|
307
|
-
expr1 = np.poly1d(rv1)
|
308
|
-
|
309
|
-
# スキャンセグメントの中間座標
|
310
|
-
|
311
|
-
mx = sx + incx // 2
|
312
|
-
|
313
|
-
my = expr(mx)
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
# 直行を求める
|
318
|
-
|
319
|
-
ex = WidthScanner(rv1, mx, my)
|
320
|
-
|
321
|
-
spt, ept, dist = ex.getPoints(res)
|
322
|
-
|
323
|
-
cv2.line(res, (int(spt[0]), int(spt[1])),
|
324
|
-
|
325
|
-
(int(ept[0]), int(ept[1])), (255,0,0), 1, lineType=cv2.LINE_AA)
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
cv2.putText(res, str(lbno), (int(spt[0]-6), int(spt[1])-8),
|
330
|
-
|
331
|
-
cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0,255,255), 1,cv2.LINE_AA)
|
332
|
-
|
333
|
-
print('%2d.直径 %.2f' % (lbno, dist))
|
334
|
-
|
335
|
-
lbno += 1
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
# 長さを測る
|
340
|
-
|
341
|
-
total_len = 0
|
342
|
-
|
343
|
-
vy = expr(x)
|
344
|
-
|
345
|
-
scan_flag = False
|
346
|
-
|
347
|
-
by = 0
|
348
|
-
|
349
|
-
for x in range(tobjx, tobjx+tobjw):
|
350
|
-
|
351
|
-
if scan_flag == False :
|
352
|
-
|
353
|
-
bx = x
|
354
|
-
|
355
|
-
by = vy
|
356
|
-
|
357
|
-
elif scan_flag == True :
|
358
|
-
|
359
|
-
if res[int(vy), x] == 255:
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
sdist = math.sqrt((bx - x)** 2 + (by - vy) ** 2)
|
364
|
-
|
365
|
-
total_len += sdist
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
cv2.line(res, (int(x), int(vy)), (int(bx), int(by)), (255,0,255), 2, lineType=cv2.LINE_AA)
|
372
|
-
|
373
|
-
bx = x
|
374
|
-
|
375
|
-
by = vy
|
376
|
-
|
377
|
-
else:
|
378
|
-
|
379
|
-
break
|
380
|
-
|
381
|
-
print('トータル長(pixel)=%.2f' %(total_len))
|
382
|
-
|
383
|
-
|
384
|
-
|
385
381
|
if __name__ == '__main__':
|
386
382
|
|
387
383
|
main()
|
1
書式の変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
|
11
11
|
### 発生している問題・エラーメッセージ
|
12
12
|
|
13
|
-
|
13
|
+
```ここに言語を入力
|
14
14
|
|
15
15
|
ValueError Traceback (most recent call last)
|
16
16
|
|
@@ -54,7 +54,11 @@
|
|
54
54
|
|
55
55
|
ValueError: too many values to unpack (expected 2)
|
56
56
|
|
57
|
+
```
|
58
|
+
|
57
|
-
#
|
59
|
+
#コード
|
60
|
+
|
61
|
+
```ここに言語を入力
|
58
62
|
|
59
63
|
import cv2
|
60
64
|
|
@@ -92,7 +96,7 @@
|
|
92
96
|
|
93
97
|
h,w = binimg.shape
|
94
98
|
|
95
|
-
中央線より上をスキャン
|
99
|
+
# 中央線より上をスキャン
|
96
100
|
|
97
101
|
x1 = 0
|
98
102
|
|
@@ -114,7 +118,7 @@
|
|
114
118
|
|
115
119
|
|
116
120
|
|
117
|
-
中央線より下をスキャン
|
121
|
+
# 中央線より下をスキャン
|
118
122
|
|
119
123
|
x2 = 0
|
120
124
|
|
@@ -154,7 +158,7 @@
|
|
154
158
|
|
155
159
|
|
156
160
|
|
157
|
-
モルフォロジー変換
|
161
|
+
# モルフォロジー変換
|
158
162
|
|
159
163
|
kernel5x5 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
|
160
164
|
|
@@ -168,7 +172,7 @@
|
|
168
172
|
|
169
173
|
|
170
174
|
|
171
|
-
ラベリング
|
175
|
+
# ラベリング
|
172
176
|
|
173
177
|
nLabels, labelImages, data, center = cv2.connectedComponentsWithStats(bw)
|
174
178
|
|
@@ -200,7 +204,7 @@
|
|
200
204
|
|
201
205
|
|
202
206
|
|
203
|
-
輪郭取得
|
207
|
+
# 輪郭取得
|
204
208
|
|
205
209
|
contours, _ = cv2.findContours(btarget, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
206
210
|
|
@@ -210,7 +214,7 @@
|
|
210
214
|
|
211
215
|
|
212
216
|
|
213
|
-
回帰計算
|
217
|
+
# 回帰計算
|
214
218
|
|
215
219
|
ypts, xpts = np.where(btarget == 255)
|
216
220
|
|
@@ -226,7 +230,7 @@
|
|
226
230
|
|
227
231
|
|
228
232
|
|
229
|
-
ターゲットオブジェクト認識領域描画
|
233
|
+
# ターゲットオブジェクト認識領域描画
|
230
234
|
|
231
235
|
cv2.rectangle(res,(tobjx,tobjy),(tobjx+tobjw,tobjy+tobjh),(0,255,255),1)
|
232
236
|
|
@@ -240,9 +244,23 @@
|
|
240
244
|
|
241
245
|
cv2.destroyAllWindows()
|
242
246
|
|
247
|
+
|
248
|
+
|
249
|
+
|
250
|
+
|
243
|
-
|
251
|
+
#ここより上はOK
|
244
|
-
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
|
262
|
+
|
245
|
-
|
263
|
+
# 枠を描画
|
246
264
|
|
247
265
|
cv2.rectangle(res,(tobjx,tobjy),(tobjx+tobjw,tobjy+tobjh),(0,255,255),1)
|
248
266
|
|
@@ -258,9 +276,13 @@
|
|
258
276
|
|
259
277
|
|
260
278
|
|
261
|
-
|
279
|
+
# for x in range(tobjx, tobjx+tobjw):
|
280
|
+
|
262
|
-
|
281
|
+
# v = expr(x)
|
282
|
+
|
283
|
+
# res[int(v),x,:] = (255,255,0)
|
284
|
+
|
263
|
-
幅を測る
|
285
|
+
# 幅を測る
|
264
286
|
|
265
287
|
offsetx = 10;incx = 16;lbno = 1
|
266
288
|
|
@@ -284,7 +306,7 @@
|
|
284
306
|
|
285
307
|
expr1 = np.poly1d(rv1)
|
286
308
|
|
287
|
-
スキャンセグメントの中間座標
|
309
|
+
# スキャンセグメントの中間座標
|
288
310
|
|
289
311
|
mx = sx + incx // 2
|
290
312
|
|
@@ -292,7 +314,7 @@
|
|
292
314
|
|
293
315
|
|
294
316
|
|
295
|
-
直行を求める
|
317
|
+
# 直行を求める
|
296
318
|
|
297
319
|
ex = WidthScanner(rv1, mx, my)
|
298
320
|
|
@@ -314,7 +336,7 @@
|
|
314
336
|
|
315
337
|
|
316
338
|
|
317
|
-
長さを測る
|
339
|
+
# 長さを測る
|
318
340
|
|
319
341
|
total_len = 0
|
320
342
|
|
@@ -364,6 +386,8 @@
|
|
364
386
|
|
365
387
|
main()
|
366
388
|
|
389
|
+
```
|
390
|
+
|
367
391
|
|
368
392
|
|
369
393
|
|