質問編集履歴
2
画像の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -32,7 +32,8 @@
|
|
32
32
|
plt.savefig('./rinnkaku/.jpg')
|
33
33
|
コード
|
34
34
|
```
|
35
|
-
このコードで下のような画像が得られるのですが,
|
35
|
+
このコードで元画像から下のような画像が得られるのですが,
|
36
|
+

|
36
37
|

|
37
38
|
|
38
39
|
面積の小さな部分の輪郭の座標も抽出してしまいます。
|
1
コード修正しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,41 +2,43 @@
|
|
2
2
|
```import cv2
|
3
3
|
import matplotlib.pyplot as plt
|
4
4
|
from matplotlib.patches import Polygon
|
5
|
-
import glob
|
6
5
|
|
7
|
-
|
6
|
+
img = cv2.imread("./pu1/1124_2_0.jpg")
|
8
|
-
for i, f in enumerate(files):
|
9
|
-
img = cv2.imread(f)
|
10
|
-
|
7
|
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
11
|
-
|
8
|
+
ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
|
12
9
|
|
13
|
-
|
10
|
+
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
|
14
|
-
|
11
|
+
binary = cv2.dilate(binary, kernel)
|
15
|
-
|
12
|
+
contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
16
13
|
|
17
|
-
|
14
|
+
def draw_contours(ax, img, contours):
|
18
|
-
|
15
|
+
ax.imshow(img)
|
19
|
-
|
16
|
+
ax.set_axis_off()
|
20
17
|
|
21
|
-
|
18
|
+
for i, cnt in enumerate(contours):
|
19
|
+
if cv2.contourArea(cnt) < 1900:
|
20
|
+
continue
|
21
|
+
else :
|
22
22
|
cnt = cnt.squeeze(axis=1)
|
23
23
|
ax.add_patch(Polygon(cnt, color="b", fill=None, lw=2))
|
24
24
|
ax.plot(cnt[:, 0], cnt[:, 1], "ro", mew=0, ms=4)
|
25
25
|
ax.text(cnt[0][0], cnt[0][1], i, color="orange", size="20")
|
26
|
+
print(contours)
|
26
27
|
|
27
28
|
|
28
|
-
|
29
|
+
fig, ax = plt.subplots(figsize=(8, 8))
|
29
|
-
|
30
|
+
draw_contours(ax, img, contours)
|
30
31
|
|
31
|
-
|
32
|
+
plt.savefig('./rinnkaku/.jpg')
|
32
33
|
コード
|
33
34
|
```
|
34
35
|
このコードで下のような画像が得られるのですが,
|
35
|
-

|
36
37
|
|
37
|
-
|
38
|
+
面積の小さな部分の輪郭の座標も抽出してしまいます。
|
38
|
-
2
|
39
|
+
一番大きな輪郭(ここでいう2ばん)のところだけの座標群を知りたいです。
|
39
40
|
|
41
|
+
|
40
42
|
どなたかよろしくお願いします。
|
41
43
|
|
42
44
|
pythonのversionは3.7.3で,
|