1
1
2
2
# coding: utf-8
3
3
4
- # In[159 ]:
4
+ # In[5 ]:
5
5
6
6
7
7
import numpy as np
8
8
import matplotlib .pyplot as plt
9
9
from skimage .color import rgb2gray
10
10
from skimage .filters import gaussian
11
- import ActiveShapeModel
12
11
import scipy
13
12
import cv2
14
13
from scipy import ndimage
15
14
import Image_preperation as prep
16
15
import FileManager as fm
17
- import Image_preperation as prep
18
16
19
17
def calc_internal (p1 ,p2 ):
20
18
if (np .array_equal (p1 ,p2 )):
@@ -41,19 +39,19 @@ def calc_mean(points):
41
39
42
40
return mean_sum / size
43
41
44
- def calc_external_img (img ):
42
+ # def calc_external_img(img):
45
43
46
- img = rgb2gray (img )
44
+ # img = rgb2gray(img)
47
45
48
- sobelx64f = cv2 .Sobel (img ,cv2 .CV_64F ,1 ,0 ,ksize = 5 )
49
- abs_sobel64f = np .absolute (sobelx64f )
50
- sobelx = np .uint8 (abs_sobel64f )
46
+ # sobelx64f = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5)
47
+ # abs_sobel64f = np.absolute(sobelx64f)
48
+ # sobelx = np.uint8(abs_sobel64f)
51
49
52
- sobely64f = cv2 .Sobel (img ,cv2 .CV_64F ,0 ,1 ,ksize = 5 )
53
- abs_sobel64f = np .absolute (sobely64f )
54
- sobely = np .uint8 (abs_sobel64f )
50
+ # sobely64f = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=5)
51
+ # abs_sobel64f = np.absolute(sobely64f)
52
+ # sobely = np.uint8(abs_sobel64f)
55
53
56
- return - (sobelx + sobely )
54
+ # return -(sobelx + sobely)
57
55
58
56
def calc_external_img2 (img ):
59
57
@@ -101,7 +99,7 @@ def calc_energy3(p1, p2, mean, external_img, alpha):
101
99
internal = calc_internal_mean (p1 ,p2 , mean )
102
100
external = calc_external (p2 , external_img )
103
101
104
- return alpha * internal + external
102
+ return internal + alpha * external
105
103
106
104
107
105
@@ -154,8 +152,8 @@ def viterbi(points, img, pixel_width, alpha):
154
152
for d in range (num_states ):
155
153
p1 = get_point_state (points [i - 1 ], d , pixel_width )
156
154
p2 = get_point_state (points [i ],t , pixel_width )
157
- # energy_trans = calc_energy(p1, p2, external_img, alpha)
158
- energy_trans = calc_energy3 (p1 , p2 , mean , external_img , alpha )
155
+ energy_trans = calc_energy (p1 , p2 , external_img , alpha )
156
+ # energy_trans = calc_energy3(p1, p2, mean, external_img, alpha)
159
157
160
158
tmp = trellis [i - 1 ,d ] + energy_trans
161
159
@@ -251,12 +249,7 @@ def resolution_downscale(img, resize):
251
249
252
250
return cv2 .resize (img , (yn ,xn ))
253
251
254
-
255
- # In[169]:
256
-
257
-
258
- if __name__ == "__main__" :
259
-
252
+ def previous_test ():
260
253
dir_radiographs = "_Data\Radiographs\*.tif"
261
254
radiographs = fm .load_files (dir_radiographs )
262
255
radiograph = radiographs [0 ]
@@ -266,175 +259,32 @@ def resolution_downscale(img, resize):
266
259
tooth = init [0 ,4 ,:,:]/ 0.3
267
260
#tooth = tooth/down_sample
268
261
269
- radiograph_pre = prep . pre_processing (radiograph )
262
+ radiograph_pre = pre_processing (radiograph )
270
263
img = resolution_downscale (radiograph_pre ,down_sample )
271
264
272
265
fig , ax = plt .subplots (figsize = (15 , 15 ))
273
266
plt .imshow (radiograph )
274
267
plt .plot (tooth [:,0 ], tooth [:,1 ], 'ro' , markersize = 1 )
275
268
plt .show ()
276
269
270
+ def test_module ():
271
+ piece = fm .load_img_piece ()
272
+ tooth = fm .load_tooth_of_piece (0 )
273
+ ext = calc_external_img2 (piece )
274
+ fm .show_with_points (ext , tooth )
275
+
276
+ img , stooth = fm .resolution_scale (piece , tooth , 1 / 6 )
277
+ ext = calc_external_img2 (img )
278
+ fm .show_with_points (ext , stooth )
279
+
280
+ new_tooth = active_contour (stooth , img , 1 , 3 , 1 )
281
+ fm .show_with_points (ext , new_tooth )
277
282
278
- # In[196]:
279
-
280
-
281
- smooth = prep .gaussian_smooth1 (radiograph ,10 )
282
- grad = calc_external_img2 (smooth )
283
- grad2 = calc_external_img2 (radiograph )
284
- fig , ax = plt .subplots (figsize = (15 , 15 ))
285
- plt .imshow (grad2 )
286
- plt .show ()
287
- print (radiograph .dtype )
288
- np .histogram (grad )
289
- fig , ax = plt .subplots (figsize = (15 , 15 ))
290
- plt .imshow (grad )
291
- plt .show ()
292
-
293
-
294
- # In[185]:
295
-
296
-
297
- img = radiograph
298
-
299
- img = rgb2gray (img )
300
-
301
- sobelx64f = cv2 .Sobel (img ,cv2 .CV_16S ,1 ,0 ,ksize = 5 )
302
- abs_sobel64f = np .absolute (sobelx64f )
303
- sobelx = np .uint8 (abs_sobel64f )
304
- plt .imshow (sobelx )
305
- plt .show ()
306
-
307
- sobely64f = cv2 .Sobel (img ,cv2 .CV_16S ,0 ,1 ,ksize = 5 )
308
- abs_sobel64f = np .absolute (sobely64f )
309
- sobely = np .uint8 (abs_sobel64f )
310
- plt .imshow (sobely64f )
311
- plt .show ()
312
-
313
-
314
- # In[161]:
315
-
316
-
317
- k1 = np .array ([[1 ,1 ,1 ],[1 ,1 ,1 ],[1 ,1 ,1 ]])/ 9
318
- smoothed = cv2 .filter2D (img ,- 1 ,k1 )
319
-
320
- k2 = np .array ([[0 ,0 ,0 ],[0 ,2 ,0 ],[0 ,0 ,0 ]])
321
- dubbel = cv2 .filter2D (img ,- 1 ,k2 )
322
- sharp = dubbel - smoothed
323
- sharp2 = 2 * img - smoothed
324
-
325
- plt .imshow (sharp )
326
- plt .show ()
327
- plt .imshow (sharp2 )
328
- plt .show ()
329
- print (sharp2 .dtype )
330
-
331
-
332
- # In[103]:
333
-
334
-
335
- piece = img [175 :275 , 225 :325 ]
336
- tooth2 = tooth
337
- tooth2 [:,0 ]= tooth [:,0 ]- 225
338
- tooth2 [:,1 ]= tooth [:,1 ]- 175
339
- fig , ax = plt .subplots (figsize = (7 , 7 ))
340
- plt .imshow (piece )
341
- plt .plot (tooth2 [:,0 ], tooth2 [:,1 ], 'ro' , markersize = 2 )
342
- plt .show ()
343
-
344
-
345
- # In[141]:
346
-
347
-
348
- new_tooth = active_contour (tooth2 , prep .gaussian (piece ,1.5 ), 2 , 2 , 25 )
349
- fig , ax = plt .subplots (figsize = (7 , 7 ))
350
- plt .imshow (piece )
351
- plt .plot (new_tooth [:,0 ], new_tooth [:,1 ], 'ro' , markersize = 2 )
352
- plt .show ()
353
-
354
-
355
- # In[137]:
356
-
357
-
358
- ext3 = calc_external_img2 (piece )
359
- fig , ax = plt .subplots (figsize = (7 , 7 ))
360
- plt .imshow (ext3 )
361
- plt .plot (tooth2 [0 ,0 ], new_tooth [0 ,1 ], 'ro' , markersize = 2 )
362
- plt .show ()
363
-
364
-
365
- # In[87]:
366
-
367
-
368
- tooth [:,0 ]= tooth [:,0 ]- 225
369
- tooth [:,1 ]= tooth [:,1 ]- 175
370
-
371
-
372
- # In[88]:
373
-
374
-
375
- for p in tooth :
376
- print (ext3 [int (p [0 ]),int (p [1 ])])
377
-
378
-
379
- # In[127]:
380
-
381
-
382
- mean = calc_mean (tooth )
383
- size = len (tooth )
384
- for i in range (size - 1 ):
385
- p1 = tooth [i ]
386
- p2 = tooth [i + 1 ]
387
- print (100 * calc_internal_mean (p1 ,p2 , mean ))
388
-
389
-
390
-
391
- # In[124]:
392
-
393
-
394
- mean
395
-
396
-
397
- # In[92]:
398
-
399
-
400
- size = len (tooth )
401
- for i in range (size - 1 ):
402
- p1 = tooth [i ]
403
- p2 = tooth [i + 1 ]
404
- print (calc_energy (p1 ,p2 ,ext3 ,0.5 ))
405
-
406
-
407
-
408
- # In[34]:
409
-
410
-
411
- ext .dtype
412
-
413
-
414
- # In[173]:
415
-
416
-
417
- new_tooth = active_contour (tooth , img , 5 , 2 , 1 )
418
-
419
- fig , ax = plt .subplots (figsize = (15 , 15 ))
420
- plt .imshow (img )
421
- plt .plot (new_tooth [:,0 ], new_tooth [:,1 ], 'ro' , markersize = 1 )
422
- plt .show ()
423
-
424
-
425
- # In[143]:
426
283
284
+ # In[6]:
427
285
428
- from skimage .color import rgb2gray
429
- from skimage import data
430
- from skimage .filters import gaussian
431
- from skimage .segmentation import active_contour
432
286
433
- snake = active_contour (radiograph_pre ,
434
- tooth , alpha = 0.015 , beta = 10 , gamma = 0.001 )
287
+ if __name__ == "__main__" :
435
288
436
- fig , ax = plt .subplots (figsize = (15 , 15 ))
437
- plt .imshow (img )
438
- plt .plot (snake [:,0 ], snake [:,1 ], 'ro' , markersize = 1 )
439
- plt .show ()
289
+ test_module ()
440
290
0 commit comments