回答編集履歴
2
d
test
CHANGED
@@ -50,6 +50,10 @@
|
|
50
50
|
|
51
51
|
|
52
52
|
|
53
|
+
contours = list(filter(lambda x: cv2.contourArea(x) > 100, contours))
|
54
|
+
|
55
|
+
|
56
|
+
|
53
57
|
dists = []
|
54
58
|
|
55
59
|
for cnt in contours:
|
@@ -86,7 +90,7 @@
|
|
86
90
|
|
87
91
|
x, y, w, h = cv2.boundingRect(target_cnt)
|
88
92
|
|
89
|
-
cropped = img[y:y + h, x:x + w]
|
93
|
+
cropped = img[y : y + h, x : x + w]
|
90
94
|
|
91
95
|
|
92
96
|
|
1
d
test
CHANGED
@@ -14,27 +14,11 @@
|
|
14
14
|
|
15
15
|
import numpy as np
|
16
16
|
|
17
|
-
#from IPython.display import Image, display
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
#def imshow(img):
|
24
|
-
|
25
|
-
# img = cv2.imencode(".png", img)[1]
|
26
|
-
|
27
|
-
# display(Image(img))
|
28
|
-
|
29
|
-
|
30
|
-
|
31
17
|
|
32
18
|
|
33
19
|
# 画像を読み込む。
|
34
20
|
|
35
21
|
img = cv2.imread("sample.jpg")
|
36
|
-
|
37
|
-
#imshow(img)
|
38
22
|
|
39
23
|
|
40
24
|
|
@@ -46,8 +30,6 @@
|
|
46
30
|
|
47
31
|
retval, binary = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY_INV)
|
48
32
|
|
49
|
-
#imshow(binary)
|
50
|
-
|
51
33
|
|
52
34
|
|
53
35
|
# 輪郭の検出
|
@@ -57,12 +39,6 @@
|
|
57
39
|
binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
|
58
40
|
|
59
41
|
)
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
cv2.drawContours(img, contours, -1, color=(0, 255, 0))
|
64
|
-
|
65
|
-
#imshow(img)
|
66
42
|
|
67
43
|
|
68
44
|
|
@@ -98,18 +74,42 @@
|
|
98
74
|
|
99
75
|
|
100
76
|
|
101
|
-
# 中心から最も距離が近い輪郭
|
77
|
+
# 中心から最も距離が近い輪郭を取得する。
|
102
78
|
|
103
79
|
idx = np.argmin(dists)
|
104
80
|
|
105
|
-
|
81
|
+
target_cnt = contours[idx]
|
106
82
|
|
107
|
-
cv2.drawContours(img, contours, idx, color=(0, 0, 255), thickness=5)
|
108
83
|
|
84
|
+
|
109
|
-
#
|
85
|
+
# その輪郭の外接矩形を取得する。
|
86
|
+
|
87
|
+
x, y, w, h = cv2.boundingRect(target_cnt)
|
88
|
+
|
89
|
+
cropped = img[y:y + h, x:x + w]
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
cv2.imwrite("result.png", cropped)
|
110
94
|
|
111
95
|
```
|
112
96
|
|
113
97
|
|
114
98
|
|
99
|
+
![イメージ説明](6205f6b3d5b15b9c0ae70e03335058cb.jpeg)
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
入力画像
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
↓
|
108
|
+
|
109
|
+
|
110
|
+
|
115
|
-
![イメージ説明](1
|
111
|
+
![イメージ説明](6916cb77c4436f000fa313ad4f4ab7e2.png)
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
切り抜き画像
|