質問編集履歴
1
プログラム全文を掲載しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -146,7 +146,225 @@
|
|
146
146
|
|
147
147
|
|
148
148
|
|
149
|
+
```analyze.py
|
150
|
+
|
151
|
+
import cv2
|
152
|
+
|
153
|
+
import numpy as np
|
154
|
+
|
155
|
+
import matplotlib.pyplot as plt
|
156
|
+
|
157
|
+
import methods
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
# 入力画像読込
|
164
|
+
|
165
|
+
# 画像サイズ(500(width) x 218(height) pix)
|
166
|
+
|
167
|
+
img = cv2.imread('images/madslide12.jpg', cv2.IMREAD_COLOR)
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
# オリジナル画像保存
|
172
|
+
|
173
|
+
# import pdb; pdb.set_trace()
|
174
|
+
|
175
|
+
org = img.copy()
|
176
|
+
|
177
|
+
cv2.imwrite('results/original.png', org)
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
# 画像ファイル
|
182
|
+
|
183
|
+
# - 画像データを処理プログラムに送る
|
184
|
+
|
185
|
+
methods.image(org,img)
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
# PyMeanShift
|
190
|
+
|
191
|
+
# - 第1引数:探索範囲、第2引数:探索色相、第3引数:粗さ
|
192
|
+
|
193
|
+
methods.meanshift(12,3,200)
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
# ヒストグラム均一化
|
198
|
+
|
199
|
+
methods.contrast()
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
# 類似色統合
|
204
|
+
|
205
|
+
methods.clustering()
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
# ブロック分割
|
210
|
+
|
211
|
+
methods.division()
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
# カラーラベリング
|
216
|
+
|
217
|
+
methods.labeling()
|
218
|
+
|
219
|
+
```
|
220
|
+
|
221
|
+
|
222
|
+
|
149
|
-
```
|
223
|
+
```methods.py
|
224
|
+
|
225
|
+
import cv2
|
226
|
+
|
227
|
+
import matplotlib.pyplot as plt
|
228
|
+
|
229
|
+
import matplotlib as mpl
|
230
|
+
|
231
|
+
import numpy as np
|
232
|
+
|
233
|
+
import pymeanshift as pms
|
234
|
+
|
235
|
+
import os
|
236
|
+
|
237
|
+
import sys
|
238
|
+
|
239
|
+
from PIL import Image
|
240
|
+
|
241
|
+
# sys.setrecursionlimit(8000) # 200 x 113 pix
|
242
|
+
|
243
|
+
sys.setrecursionlimit(30000) # 500 x 281 pix
|
244
|
+
|
245
|
+
plt.gray()
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
def image(_org,_img):
|
252
|
+
|
253
|
+
global org,img,h,w,c
|
254
|
+
|
255
|
+
global bo,go,ro,al
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
org,img = _org,_img
|
260
|
+
|
261
|
+
h,w,c = img.shape
|
262
|
+
|
263
|
+
bo,go,ro = cv2.split(org)
|
264
|
+
|
265
|
+
al = 0.55
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
def meanshift(spatial_radius,range_radius,min_density):
|
270
|
+
|
271
|
+
global img
|
272
|
+
|
273
|
+
(img,labels,num) = pms.segment(cv2.cvtColor(img,cv2.COLOR_BGR2Lab),spatial_radius,range_radius,min_density)
|
274
|
+
|
275
|
+
img = cv2.cvtColor(img, cv2.COLOR_Lab2BGR)
|
276
|
+
|
277
|
+
cv2.imwrite('results/meanshift.png',img)
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
def contrast():
|
282
|
+
|
283
|
+
global img
|
284
|
+
|
285
|
+
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
|
286
|
+
|
287
|
+
h,s,v = cv2.split(hsv)
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(3, 3))
|
292
|
+
|
293
|
+
result = clahe.apply(v)
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
hsv = cv2.merge((h,s,result))
|
298
|
+
|
299
|
+
img = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
|
300
|
+
|
301
|
+
cv2.imwrite("results/contrast.png", img)
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
def division():
|
306
|
+
|
307
|
+
global block
|
308
|
+
|
309
|
+
hs,ws,cnt = 113,200,1
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
if ((h>hs)&(w>ws)):
|
314
|
+
|
315
|
+
for j in range(0,h,hs):
|
316
|
+
|
317
|
+
for i in range(0,w,ws):
|
318
|
+
|
319
|
+
if (((j+hs)<h)&((i+ws)<w)):
|
320
|
+
|
321
|
+
div = np.zeros((hs,ws,c),dtype=int)
|
322
|
+
|
323
|
+
div[0:hs,0:ws] = img[j:j+hs,i:i+ws]
|
324
|
+
|
325
|
+
else:
|
326
|
+
|
327
|
+
if (((i+ws)>w)&((j+hs)>h)):
|
328
|
+
|
329
|
+
div = np.zeros((h-j,w-i,c),dtype=int)
|
330
|
+
|
331
|
+
div[0:h-j,0:w-i] = img[j:h,i:w]
|
332
|
+
|
333
|
+
elif ((j+hs)>h):
|
334
|
+
|
335
|
+
div = np.zeros((h-j,ws,c),dtype=int)
|
336
|
+
|
337
|
+
div[0:h-j,0:ws] = img[j:h,i:i+ws]
|
338
|
+
|
339
|
+
else:
|
340
|
+
|
341
|
+
div = np.zeros((hs,w-i,c),dtype=int)
|
342
|
+
|
343
|
+
div[0:hs,0:w-i] = img[j:j+hs,i:w]
|
344
|
+
|
345
|
+
cv2.imwrite('results/division/division{}.png'.format(cnt),div)
|
346
|
+
|
347
|
+
cnt += 1
|
348
|
+
|
349
|
+
block = cnt
|
350
|
+
|
351
|
+
print('block number :',block)
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
def clustering():
|
356
|
+
|
357
|
+
global img
|
358
|
+
|
359
|
+
im = Image.open('results/contrast.png')
|
360
|
+
|
361
|
+
im_q = im.quantize(colors=128, method=0, dither=1)
|
362
|
+
|
363
|
+
im_q.save('results/clustering.png')
|
364
|
+
|
365
|
+
img = cv2.imread('results/clustering.png', cv2.IMREAD_COLOR)
|
366
|
+
|
367
|
+
|
150
368
|
|
151
369
|
def approximation(pix1,pix2):
|
152
370
|
|