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

質問編集履歴

2

_

2019/01/28 09:49

投稿

gonzoshira
gonzoshira

スコア21

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,4 @@
1
- 皆様の助けもあり画像から複数輪郭抽出を行うまでになりました.(図1)![イメージ説明](c67ccf071f4aa213a5f0f4957bc2aeda.png)
2
- ここで白の領域においてドロネー分割をおこないたいのですが,
1
+ 皆様の助けもあり画像から複数輪郭抽出を行うまになりました.白の領域においてドロネー分割をおこないたいのですが,
3
- どうしても図のように黒の領域にも分割が及んでいます.
4
- ![イ![イメージ説明](f193767b22d474069f982d8abdd1ea54.png)
5
2
  どなたか解決できる方いらっしゃいましたら.よろしくお願いします.
6
3
  python2.7,openCV4.0です.
7
4
 

1

プログラムを載せました.まだ頂いた輪郭抽出のものを載せていません.

2019/01/28 09:49

投稿

gonzoshira
gonzoshira

スコア21

title CHANGED
File without changes
body CHANGED
@@ -3,4 +3,142 @@
3
3
  どうしても図のように黒の領域にも分割が及んでいます.
4
4
  ![イ![イメージ説明](f193767b22d474069f982d8abdd1ea54.png)
5
5
  どなたか解決できる方いらっしゃいましたら.よろしくお願いします.
6
- python2.7,openCV4.0です.
6
+ python2.7,openCV4.0です.
7
+
8
+ ```python
9
+
10
+ #!/usr/bin/env python
11
+ #coding: utf-8
12
+
13
+ import csv,os,cv2,math,re
14
+ import numpy as np
15
+ import matplotlib.pyplot as plt
16
+ from matplotlib import pyplot
17
+ from operator import itemgetter
18
+ from dolfin import *
19
+ from mshr import *
20
+ from pylab import show,triplot
21
+
22
+ #読み込み,グレー設定
23
+ img = cv2.imread('/home/ubuntu/0116maps/map.pgm')
24
+ img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
25
+
26
+ #smoothing
27
+ img_preprocessed = cv2.GaussianBlur(img_gray, (5, 5), 50)
28
+ img_preprocessed = cv2.GaussianBlur(img_preprocessed, (5, 5), 50)
29
+ img_preprocessed = cv2.GaussianBlur(img_preprocessed, (5, 5), 50)
30
+ img_preprocessed = cv2.GaussianBlur(img_preprocessed, (5, 5), 50)
31
+ img_preprocessed = cv2.GaussianBlur(img_preprocessed, (5, 5), 50)
32
+ img_preprocessed = cv2.GaussianBlur(img_preprocessed, (5, 5), 50)
33
+ img_preprocessed = cv2.GaussianBlur(img_preprocessed, (5, 5), 50)
34
+ img_preprocessed = cv2.GaussianBlur(img_preprocessed, (5, 5), 50)
35
+ img_preprocessed = cv2.GaussianBlur(img_preprocessed, (5, 5), 50)
36
+ img_preprocessed = cv2.GaussianBlur(img_preprocessed, (5, 5), 50)
37
+ img_preprocessed = cv2.GaussianBlur(img_preprocessed, (5, 5), 50)
38
+
39
+ #閾値処理,輪郭検索
40
+ _, white_binary = cv2.threshold(img_preprocessed, 220, 254, cv2.THRESH_BINARY)
41
+ white_contours, _ = cv2.findContours(white_binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
42
+
43
+ #imageのコピー
44
+ white_and_contours = np.copy(img)
45
+
46
+ #最大輪郭値取得
47
+ min_white_area = 60
48
+ large_contours = [ cnt for cnt in white_contours if cv2.contourArea(cnt) > min_white_area ]
49
+
50
+ #外形の座標取得,最大値最小値取得
51
+ large_contours = np.array(large_contours)
52
+ large_contours_min = large_contours.min(axis = 1)
53
+ large_contours_max = large_contours.max(axis = 1)
54
+
55
+ #csv削除,書き込み
56
+ os.remove("/home/ubuntu/map_data/pixel_output.csv")
57
+ with open("/home/ubuntu/map_data/pixel_output.csv", 'a') as f:
58
+ writer = csv.writer(f)
59
+ writer.writerows(large_contours)
60
+
61
+ #pixel値の[]削除、,付け
62
+ os.remove("/home/ubuntu/map_data/pixel_surround.csv")
63
+ with open("/home/ubuntu/map_data/pixel_output.csv", 'rb') as f:
64
+ reader = csv.reader(f)
65
+ for row in reader:
66
+ i = ' '.join(row)
67
+ i = i.replace(' ',',')
68
+ i = i.replace('],[','')
69
+ i = i.replace('[','')
70
+ i = i.replace(']','\n')
71
+ f = open("/home/ubuntu/map_data/pixel_surround.csv", 'a')
72
+ f.write(i)
73
+ f.close()
74
+
75
+ os.remove("/home/ubuntu/map_data/minmaxoutput.csv")
76
+ with open("/home/ubuntu/map_data/minmaxoutput.csv", 'a') as f:
77
+ writer = csv.writer(f)
78
+ writer.writerows(large_contours_min)
79
+ writer.writerows(large_contours_max)
80
+
81
+ os.remove("/home/ubuntu/map_data/minmax_coordinate.csv")
82
+ with open("/home/ubuntu/map_data/minmaxoutput.csv", 'rb') as f:
83
+ reader = csv.reader(f)
84
+ for row in reader:
85
+ i = ' '.join(row)
86
+ i = i.replace('[','')
87
+ i = i.replace(']',',')
88
+ i = i.replace(' ',',')
89
+ f = open("/home/ubuntu/map_data/minmax_coordinate.csv", 'a')
90
+ f.write(i)
91
+ f.close()
92
+
93
+ #map値読み取りやすく変換
94
+ os.remove("/home/ubuntu/map_data/map_value.csv")
95
+ with open("/home/ubuntu/0116maps/map.yaml", 'rb') as f:
96
+ reader = csv.reader(f)
97
+ for words in reader:
98
+ i = ' '.join(words)
99
+ match = re.search(r': ', i)
100
+ if match:
101
+ i = i[match.end():]
102
+ i = i.strip('[]')
103
+ i = i.replace(' ',',')
104
+ f = open("/home/ubuntu/map_data/map_value.csv", 'a')
105
+ f.write(i + ",")
106
+ f.close()
107
+
108
+ #格子分割準備
109
+ with open("/home/ubuntu/map_data/pixel_surround.csv", 'rb') as f:
110
+ reader = csv.reader(f)
111
+ count = 0
112
+ for pixel in reader:
113
+ count+=1
114
+
115
+ j = count-1
116
+ k = 0
117
+ poin = range(j)
118
+ domain_vertices = []
119
+
120
+ with open("/home/ubuntu/map_data/pixel_surround.csv", 'rb') as f:
121
+ reader = csv.reader(f)
122
+ for pix in reader:
123
+ if k<j:
124
+ poin[k] = [Point(int(pix[0]),int(pix[1]))]
125
+ k+=1
126
+
127
+ for m in reversed(range(j)):
128
+ domain_vertices += poin[m]
129
+
130
+ #格子分割
131
+ domain = Polygon(domain_vertices)
132
+ mesh = generate_mesh(domain,0.1)
133
+ coords = mesh.coordinates()
134
+
135
+ os.remove("/home/ubuntu/map_data/coords.csv")
136
+ with open("/home/ubuntu/map_data/coords.csv", 'a') as f:
137
+ writer = csv.writer(f)
138
+ writer.writerows(coords)
139
+ i
140
+ #メッシュ領域plot
141
+ triplot(coords[:,0], coords[:,1], triangles=mesh.cells())
142
+ plt.imshow(white_and_contours)
143
+ plt.show()
144
+ ```