teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

画像の追加

2019/11/25 07:22

投稿

reonald
reonald

スコア32

title CHANGED
File without changes
body CHANGED
@@ -32,7 +32,8 @@
32
32
  plt.savefig('./rinnkaku/.jpg')
33
33
  コード
34
34
  ```
35
- このコードで下のような画像が得られるのですが,
35
+ このコードで元画像から下のような画像が得られるのですが,
36
+ ![イメージ説明](4fefee8083d68d421fd565b1364e5124.jpeg)
36
37
  ![イメージ説明](6b97370455fa5fe84250d5aa0787111e.png)
37
38
 
38
39
  面積の小さな部分の輪郭の座標も抽出してしまいます。

1

コード修正しました。

2019/11/25 07:22

投稿

reonald
reonald

スコア32

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
- files = glob.glob("./pu1/*")
6
+ img = cv2.imread("./pu1/1124_2_0.jpg")
8
- for i, f in enumerate(files):
9
- img = cv2.imread(f)
10
- gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
7
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
11
- ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
8
+ ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
12
9
 
13
- kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
10
+ kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
14
- binary = cv2.dilate(binary, kernel)
11
+ binary = cv2.dilate(binary, kernel)
15
- contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
12
+ contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
16
13
 
17
- def draw_contours(ax, img, contours):
14
+ def draw_contours(ax, img, contours):
18
- ax.imshow(img)
15
+ ax.imshow(img)
19
- ax.set_axis_off()
16
+ ax.set_axis_off()
20
17
 
21
- for i, cnt in enumerate(contours):
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
- fig, ax = plt.subplots(figsize=(8, 8))
29
+ fig, ax = plt.subplots(figsize=(8, 8))
29
- draw_contours(ax, img, contours)
30
+ draw_contours(ax, img, contours)
30
31
 
31
- plt.savefig('./rinnkaku/_%d.jpg' % i)
32
+ plt.savefig('./rinnkaku/.jpg')
32
33
  コード
33
34
  ```
34
35
  このコードで下のような画像が得られるのですが,
35
- ![イメージ説明](0501feb567e8efff7b494be0c5599126.jpeg)
36
+ ![イメージ説明](6b97370455fa5fe84250d5aa0787111e.png)
36
37
 
37
- 1.面積の小さいところ(これら0-7の輪郭)を輪郭抽出したくない。
38
+ 面積の小さな部分の輪郭の座標も抽出してしまます
38
- 2.赤点の座標知りたいです。
39
+ 一番大きな輪郭(ここでいう2ばん)ところだけの座標群を知りたいです。
39
40
 
41
+
40
42
  どなたかよろしくお願いします。
41
43
 
42
44
  pythonのversionは3.7.3で,