質問編集履歴

2

_

2019/01/28 09:49

投稿

gonzoshira
gonzoshira

スコア21

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

1

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

2019/01/28 09:49

投稿

gonzoshira
gonzoshira

スコア21

test CHANGED
File without changes
test CHANGED
@@ -9,3 +9,279 @@
9
9
  どなたか解決できる方いらっしゃいましたら.よろしくお願いします.
10
10
 
11
11
  python2.7,openCV4.0です.
12
+
13
+
14
+
15
+ ```python
16
+
17
+
18
+
19
+ #!/usr/bin/env python
20
+
21
+ #coding: utf-8
22
+
23
+
24
+
25
+ import csv,os,cv2,math,re
26
+
27
+ import numpy as np
28
+
29
+ import matplotlib.pyplot as plt
30
+
31
+ from matplotlib import pyplot
32
+
33
+ from operator import itemgetter
34
+
35
+ from dolfin import *
36
+
37
+ from mshr import *
38
+
39
+ from pylab import show,triplot
40
+
41
+
42
+
43
+ #読み込み,グレー設定
44
+
45
+ img = cv2.imread('/home/ubuntu/0116maps/map.pgm')
46
+
47
+ img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
48
+
49
+
50
+
51
+ #smoothing
52
+
53
+ img_preprocessed = cv2.GaussianBlur(img_gray, (5, 5), 50)
54
+
55
+ img_preprocessed = cv2.GaussianBlur(img_preprocessed, (5, 5), 50)
56
+
57
+ img_preprocessed = cv2.GaussianBlur(img_preprocessed, (5, 5), 50)
58
+
59
+ img_preprocessed = cv2.GaussianBlur(img_preprocessed, (5, 5), 50)
60
+
61
+ img_preprocessed = cv2.GaussianBlur(img_preprocessed, (5, 5), 50)
62
+
63
+ img_preprocessed = cv2.GaussianBlur(img_preprocessed, (5, 5), 50)
64
+
65
+ img_preprocessed = cv2.GaussianBlur(img_preprocessed, (5, 5), 50)
66
+
67
+ img_preprocessed = cv2.GaussianBlur(img_preprocessed, (5, 5), 50)
68
+
69
+ img_preprocessed = cv2.GaussianBlur(img_preprocessed, (5, 5), 50)
70
+
71
+ img_preprocessed = cv2.GaussianBlur(img_preprocessed, (5, 5), 50)
72
+
73
+ img_preprocessed = cv2.GaussianBlur(img_preprocessed, (5, 5), 50)
74
+
75
+
76
+
77
+ #閾値処理,輪郭検索
78
+
79
+ _, white_binary = cv2.threshold(img_preprocessed, 220, 254, cv2.THRESH_BINARY)
80
+
81
+ white_contours, _ = cv2.findContours(white_binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
82
+
83
+
84
+
85
+ #imageのコピー
86
+
87
+ white_and_contours = np.copy(img)
88
+
89
+
90
+
91
+ #最大輪郭値取得
92
+
93
+ min_white_area = 60
94
+
95
+ large_contours = [ cnt for cnt in white_contours if cv2.contourArea(cnt) > min_white_area ]
96
+
97
+
98
+
99
+ #外形の座標取得,最大値最小値取得
100
+
101
+ large_contours = np.array(large_contours)
102
+
103
+ large_contours_min = large_contours.min(axis = 1)
104
+
105
+ large_contours_max = large_contours.max(axis = 1)
106
+
107
+
108
+
109
+ #csv削除,書き込み
110
+
111
+ os.remove("/home/ubuntu/map_data/pixel_output.csv")
112
+
113
+ with open("/home/ubuntu/map_data/pixel_output.csv", 'a') as f:
114
+
115
+ writer = csv.writer(f)
116
+
117
+ writer.writerows(large_contours)
118
+
119
+
120
+
121
+ #pixel値の[]削除、,付け
122
+
123
+ os.remove("/home/ubuntu/map_data/pixel_surround.csv")
124
+
125
+ with open("/home/ubuntu/map_data/pixel_output.csv", 'rb') as f:
126
+
127
+ reader = csv.reader(f)
128
+
129
+ for row in reader:
130
+
131
+ i = ' '.join(row)
132
+
133
+ i = i.replace(' ',',')
134
+
135
+ i = i.replace('],[','')
136
+
137
+ i = i.replace('[','')
138
+
139
+ i = i.replace(']','\n')
140
+
141
+ f = open("/home/ubuntu/map_data/pixel_surround.csv", 'a')
142
+
143
+ f.write(i)
144
+
145
+ f.close()
146
+
147
+
148
+
149
+ os.remove("/home/ubuntu/map_data/minmaxoutput.csv")
150
+
151
+ with open("/home/ubuntu/map_data/minmaxoutput.csv", 'a') as f:
152
+
153
+ writer = csv.writer(f)
154
+
155
+ writer.writerows(large_contours_min)
156
+
157
+ writer.writerows(large_contours_max)
158
+
159
+
160
+
161
+ os.remove("/home/ubuntu/map_data/minmax_coordinate.csv")
162
+
163
+ with open("/home/ubuntu/map_data/minmaxoutput.csv", 'rb') as f:
164
+
165
+ reader = csv.reader(f)
166
+
167
+ for row in reader:
168
+
169
+ i = ' '.join(row)
170
+
171
+ i = i.replace('[','')
172
+
173
+ i = i.replace(']',',')
174
+
175
+ i = i.replace(' ',',')
176
+
177
+ f = open("/home/ubuntu/map_data/minmax_coordinate.csv", 'a')
178
+
179
+ f.write(i)
180
+
181
+ f.close()
182
+
183
+
184
+
185
+ #map値読み取りやすく変換
186
+
187
+ os.remove("/home/ubuntu/map_data/map_value.csv")
188
+
189
+ with open("/home/ubuntu/0116maps/map.yaml", 'rb') as f:
190
+
191
+ reader = csv.reader(f)
192
+
193
+ for words in reader:
194
+
195
+ i = ' '.join(words)
196
+
197
+ match = re.search(r': ', i)
198
+
199
+ if match:
200
+
201
+ i = i[match.end():]
202
+
203
+ i = i.strip('[]')
204
+
205
+ i = i.replace(' ',',')
206
+
207
+ f = open("/home/ubuntu/map_data/map_value.csv", 'a')
208
+
209
+ f.write(i + ",")
210
+
211
+ f.close()
212
+
213
+
214
+
215
+ #格子分割準備
216
+
217
+ with open("/home/ubuntu/map_data/pixel_surround.csv", 'rb') as f:
218
+
219
+ reader = csv.reader(f)
220
+
221
+ count = 0
222
+
223
+ for pixel in reader:
224
+
225
+ count+=1
226
+
227
+
228
+
229
+ j = count-1
230
+
231
+ k = 0
232
+
233
+ poin = range(j)
234
+
235
+ domain_vertices = []
236
+
237
+
238
+
239
+ with open("/home/ubuntu/map_data/pixel_surround.csv", 'rb') as f:
240
+
241
+ reader = csv.reader(f)
242
+
243
+ for pix in reader:
244
+
245
+ if k<j:
246
+
247
+ poin[k] = [Point(int(pix[0]),int(pix[1]))]
248
+
249
+ k+=1
250
+
251
+
252
+
253
+ for m in reversed(range(j)):
254
+
255
+ domain_vertices += poin[m]
256
+
257
+
258
+
259
+ #格子分割
260
+
261
+ domain = Polygon(domain_vertices)
262
+
263
+ mesh = generate_mesh(domain,0.1)
264
+
265
+ coords = mesh.coordinates()
266
+
267
+
268
+
269
+ os.remove("/home/ubuntu/map_data/coords.csv")
270
+
271
+ with open("/home/ubuntu/map_data/coords.csv", 'a') as f:
272
+
273
+ writer = csv.writer(f)
274
+
275
+ writer.writerows(coords)
276
+
277
+ i
278
+
279
+ #メッシュ領域plot
280
+
281
+ triplot(coords[:,0], coords[:,1], triangles=mesh.cells())
282
+
283
+ plt.imshow(white_and_contours)
284
+
285
+ plt.show()
286
+
287
+ ```