質問編集履歴

6

コード修正

2018/09/13 11:09

投稿

uriuri
uriuri

スコア47

test CHANGED
File without changes
test CHANGED
@@ -389,3 +389,159 @@
389
389
 
390
390
 
391
391
  ```
392
+
393
+ 修正後
394
+
395
+ ```python
396
+
397
+ import cv2
398
+
399
+ import math
400
+
401
+ import numpy as np
402
+
403
+
404
+
405
+ img_src = cv2.imread("test.jpg")
406
+
407
+
408
+
409
+ gauss = cv2.GaussianBlur(img_src, (11, 11), 0)
410
+
411
+ gray = cv2.cvtColor(gauss, cv2.COLOR_BGR2GRAY)
412
+
413
+ ret,th1 = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)
414
+
415
+ edges = cv2.Canny(th1, 50, 150)
416
+
417
+
418
+
419
+ img, contours, hierarchy = cv2.findContours(th1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1)
420
+
421
+ for i in range(0, len(contours)):
422
+
423
+ #for cnt in contours:
424
+
425
+ cnt = contours[i]
426
+
427
+ area = cv2.contourArea(cnt)
428
+
429
+ #print(area)
430
+
431
+
432
+
433
+ if 500 < area < 5000:
434
+
435
+ rect = cv2.minAreaRect(cnt)
436
+
437
+ box = cv2.boxPoints(rect)
438
+
439
+
440
+
441
+ (cx, cy), (width, height), angle = rect
442
+
443
+ diag = np.linalg.norm([width, height])
444
+
445
+ #print(width, height, diag)
446
+
447
+ # 161.91236877441406 16.144147872924805 162.715237986
448
+
449
+
450
+
451
+ #
452
+
453
+ A = width
454
+
455
+ B = height
456
+
457
+ C = diag
458
+
459
+
460
+
461
+ if A < B:
462
+
463
+ min = A
464
+
465
+ else:
466
+
467
+ min = B
468
+
469
+ if C < min:
470
+
471
+ min = C
472
+
473
+ print("min", min)
474
+
475
+ if 5 < min < 25:
476
+
477
+ #
478
+
479
+ cv2.drawContours(img_src, [box.astype(int)], -1, (0, 255, 0), 2)
480
+
481
+
482
+
483
+ def within(line, rect):
484
+
485
+
486
+
487
+ p1, p2 = tuple(line[:2]), tuple(line[2:])
488
+
489
+ #print("p1",p1)
490
+
491
+ #print("p2",p2)
492
+
493
+ #print("line",line)
494
+
495
+ return cv2.pointPolygonTest(box, p1, False) >= 0 and \
496
+
497
+ cv2.pointPolygonTest(box, p2, False) >= 0
498
+
499
+
500
+
501
+ LSD = cv2.createLineSegmentDetector()
502
+
503
+ lines, width, prec, nfa = LSD.detect(edges)
504
+
505
+ if lines is not None:
506
+
507
+ lines = np.squeeze(lines,axis=1)
508
+
509
+ #if lines.dims == 1:
510
+
511
+ # lines = np.expand_dims(lines, axis=0)
512
+
513
+ for line in lines:
514
+
515
+ p1, p2 = tuple(line[:2]), tuple(line[2:])
516
+
517
+ print("line",line)
518
+
519
+
520
+
521
+ if within(line, rect):
522
+
523
+
524
+
525
+ cv2.line(img_src, p1, p2, (0, 0, 255), 2)
526
+
527
+ #else:
528
+
529
+
530
+
531
+ # cv2.line(img_src, p1, p2, (255, 0, 0), 2)
532
+
533
+
534
+
535
+
536
+
537
+
538
+
539
+ cv2.imshow('src', img_src)
540
+
541
+ cv2.waitKey()
542
+
543
+ cv2.destroyAllWindows()
544
+
545
+
546
+
547
+ ```

5

コードの追加

2018/09/13 11:09

投稿

uriuri
uriuri

スコア47

test CHANGED
File without changes
test CHANGED
@@ -207,3 +207,185 @@
207
207
  ```
208
208
 
209
209
  コードが見づらくて申し訳ありません
210
+
211
+
212
+
213
+ ```python
214
+
215
+ import cv2
216
+
217
+ import math
218
+
219
+ import numpy as np
220
+
221
+
222
+
223
+ img_src = cv2.imread("./haku/sample/image1.ppm")
224
+
225
+
226
+
227
+ gauss = cv2.GaussianBlur(img_src,(11,11),0)#9,9
228
+
229
+ gray = cv2.cvtColor(gauss,cv2.COLOR_BGR2GRAY)
230
+
231
+ ret,th1 = cv2.threshold(gray,200,255,cv2.THRESH_BINARY)
232
+
233
+ edges = cv2.Canny(th1,50,150)#(100,150)
234
+
235
+
236
+
237
+
238
+
239
+ img, contours, hierarchy = cv2.findContours(th1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1)
240
+
241
+ if len(contours) > 0:
242
+
243
+ for i in range(0, len(contours)):
244
+
245
+ cnt = contours[i]
246
+
247
+ M = cv2.moments(cnt)
248
+
249
+ #print(M)
250
+
251
+ area = cv2.contourArea(cnt)
252
+
253
+ print(area)
254
+
255
+ if 500 < area < 5000:
256
+
257
+ rect = cv2.minAreaRect(contours[i])
258
+
259
+ box = cv2.boxPoints(rect)
260
+
261
+ box = np.int0(box)
262
+
263
+ x1,x2,x3,x4 = np.int0(box)
264
+
265
+ a = np.array(x1)
266
+
267
+ b = np.array(x2)
268
+
269
+ c = np.array(x3)
270
+
271
+ d = np.array(x4)
272
+
273
+
274
+
275
+ A = np.linalg.norm(a-b)
276
+
277
+ B = np.linalg.norm(a-c)
278
+
279
+ C = np.linalg.norm(a-d)
280
+
281
+
282
+
283
+ print("頂点",x1,x2,x3,x4)
284
+
285
+ #print("abcd", a,b,c,d)
286
+
287
+ #print("Aの長さ", A)
288
+
289
+ #print("Bの長さ",B)
290
+
291
+ #print("Cの長さ",C)
292
+
293
+
294
+
295
+ if A < B:
296
+
297
+ min = A
298
+
299
+ else:
300
+
301
+ min = B
302
+
303
+ if C < min:
304
+
305
+ min = C
306
+
307
+ print("最小",min)
308
+
309
+ if 5 < min < 25:
310
+
311
+ im = cv2.drawContours(img_src,[box],0,(0,255,0),2)
312
+
313
+
314
+
315
+
316
+
317
+ def within(line, rect):
318
+
319
+ """line が rect に含まれるかどうか
320
+
321
+ """
322
+
323
+ p1, p2 = tuple(line[:2]), tuple(line[2:])
324
+
325
+ # 線の始点と終点が長方形内に含まれるかどうか
326
+
327
+ return cv2.pointPolygonTest(box, p1, False) >= 0 and \
328
+
329
+ cv2.pointPolygonTest(box, p2, False) >= 0
330
+
331
+
332
+
333
+ LSD = cv2.createLineSegmentDetector()
334
+
335
+ lines, width, prec, nfa = LSD.detect(edges)
336
+
337
+ print(lines.shape)
338
+
339
+ lines = np.squeeze(lines)
340
+
341
+
342
+
343
+ if lines is not None:
344
+
345
+ for i in range(len(lines)):
346
+
347
+
348
+
349
+ #for x1,y1,x2,y2 in lines[i] :
350
+
351
+ # cv2.line(img_src,(x1,y1),(x2,y2),(0,0,255),2)
352
+
353
+ # X1 = (x1, y1)
354
+
355
+ # X2 = (x2, y2)
356
+
357
+ # print("X1", X1)
358
+
359
+ #print("X2", X2)
360
+
361
+ for line in lines:
362
+
363
+ p1, p2 = tuple(lines[:2]), tuple(line[2:])
364
+
365
+ if within(line, rect):
366
+
367
+ # 長方形に含まれる場合は赤色で描画
368
+
369
+ cv2.line(img_src, p1, p2, (0, 0, 255), 2)
370
+
371
+ else:
372
+
373
+ # 長方形に含まれない場合は赤色で描画
374
+
375
+ cv2.line(img_src, p1, p2, (255, 0, 0), 2)
376
+
377
+
378
+
379
+
380
+
381
+ #cv2.imwrite("teratail.jpg", img_src)
382
+
383
+ cv2.imshow('src', img_src)
384
+
385
+ cv2.waitKey()
386
+
387
+ cv2.destroyAllWindows()
388
+
389
+
390
+
391
+ ```

4

コードの追記

2018/09/13 04:36

投稿

uriuri
uriuri

スコア47

test CHANGED
File without changes
test CHANGED
@@ -122,7 +122,45 @@
122
122
 
123
123
  d = np.array(x4)
124
124
 
125
+
126
+
127
+ A = np.linalg.norm(a-b)
128
+
129
+ B = np.linalg.norm(a-c)
130
+
131
+ C = np.linalg.norm(a-d)
132
+
133
+
134
+
135
+ print("頂点",x1,x2,x3,x4)
136
+
137
+ #print("abcd", a,b,c,d)
138
+
139
+ #print("Aの長さ", A)
140
+
141
+ #print("Bの長さ",B)
142
+
143
+ #print("Cの長さ",C)
144
+
145
+
146
+
147
+ if A < B:
148
+
149
+ min = A
150
+
151
+ else:
152
+
153
+ min = B
154
+
155
+ if C < min:
156
+
157
+ min = C
158
+
159
+ print("最小",min)
160
+
161
+ if 5 < min < 25:
162
+
125
- im = cv2.drawContours(img_src,[box],0,(0,255,0),2)
163
+ im = cv2.drawContours(img_src,[box],0,(0,255,0),2)
126
164
 
127
165
 
128
166
 

3

画像の挿入

2018/09/13 02:03

投稿

uriuri
uriuri

スコア47

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,6 @@
1
+ ![イメージ説明](af90568d86afbbd802d0a1a1a7166b27.jpeg)
2
+
1
- - ![リスト](5f231c7b127334bc591e3a97ce5cbb53.jpeg)
3
+ ![リスト](5f231c7b127334bc591e3a97ce5cbb53.jpeg)
2
4
 
3
5
 
4
6
 
@@ -62,11 +64,27 @@
62
64
 
63
65
  ```python
64
66
 
65
- .
67
+ import cv2
66
68
 
67
- .
69
+ import math
68
70
 
69
- .
71
+ import numpy as np
72
+
73
+
74
+
75
+ img_src = cv2.imread("./haku/sample/image1.ppm")
76
+
77
+
78
+
79
+ gauss = cv2.GaussianBlur(img_src,(11,11),0)#9,9
80
+
81
+ gray = cv2.cvtColor(gauss,cv2.COLOR_BGR2GRAY)
82
+
83
+ ret,th1 = cv2.threshold(gray,200,255,cv2.THRESH_BINARY)
84
+
85
+ edges = cv2.Canny(th1,50,150)#(100,150)
86
+
87
+
70
88
 
71
89
  #矩形検出
72
90
 

2

コードの追加

2018/09/12 12:19

投稿

uriuri
uriuri

スコア47

test CHANGED
File without changes
test CHANGED
@@ -57,3 +57,97 @@
57
57
  これは条件文に対してX1がどの値かが明確ではないため起こる問題であっていますでしょうか
58
58
 
59
59
  この場合条件文で比較するためのアドバイスをいただけたら幸いです
60
+
61
+
62
+
63
+ ```python
64
+
65
+ .
66
+
67
+ .
68
+
69
+ .
70
+
71
+ #矩形検出
72
+
73
+ img, contours, hierarchy = cv2.findContours(th1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1)
74
+
75
+ if len(contours) > 0:
76
+
77
+ for i in range(0, len(contours)):
78
+
79
+ cnt = contours[i]
80
+
81
+ M = cv2.moments(cnt)
82
+
83
+ #print(M)
84
+
85
+ area = cv2.contourArea(cnt)
86
+
87
+ print(area)
88
+
89
+ if 500 < area < 5000:
90
+
91
+ rect = cv2.minAreaRect(contours[i])
92
+
93
+ box = cv2.boxPoints(rect)
94
+
95
+ box = np.int0(box)
96
+
97
+ x1,x2,x3,x4 = np.int0(box)
98
+
99
+ a = np.array(x1)
100
+
101
+ b = np.array(x2)
102
+
103
+ c = np.array(x3)
104
+
105
+ d = np.array(x4)
106
+
107
+ im = cv2.drawContours(img_src,[box],0,(0,255,0),2)
108
+
109
+
110
+
111
+ #直線検出
112
+
113
+ LSD = cv2.createLineSegmentDetector()
114
+
115
+ lines, width, prec, nfa = LSD.detect(edges)
116
+
117
+ if lines is not None:
118
+
119
+ for i in range(len(lines)):
120
+
121
+
122
+
123
+ for x1,y1,x2,y2 in lines[i] :
124
+
125
+ cv2.line(img_src,(x1,y1),(x2,y2),(0,0,255),2)
126
+
127
+ X1 = (x1, y1)
128
+
129
+ X2 = (x2, y2)
130
+
131
+ print("X1", X1)
132
+
133
+ print("X2", X2)
134
+
135
+
136
+
137
+ jug1 = cv2.pointPolygonTest(rect, X1, False)
138
+
139
+ jug2 = cv2.pointPolygonTest(rect, X2, False)
140
+
141
+ print(jug1)
142
+
143
+ print(jug2)
144
+
145
+ .
146
+
147
+ .
148
+
149
+ .
150
+
151
+ ```
152
+
153
+ コードが見づらくて申し訳ありません

1

誤字

2018/09/12 11:24

投稿

uriuri
uriuri

スコア47

test CHANGED
File without changes
test CHANGED
@@ -44,7 +44,7 @@
44
44
 
45
45
  Traceback (most recent call last):
46
46
 
47
- File "SARA_hakukaizen2.py", line 101, in <module>
47
+ File "○○○.py", line 101, in <module>
48
48
 
49
49
  if a < X1 < b:
50
50