-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocessing.py
109 lines (49 loc) · 1.95 KB
/
preprocessing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import numpy as np
import skimage
import cv2
import matplotlib.pyplot as plt
import os
def normalize_scan(scan):
scan = (scan - scan.min()) / (scan.max() - scan.min())
scan = 2.0 * scan - 1.0
return scan
def preprocess_scan(scan_path='.', dataset='areds'):
if dataset=='areds':
scan = preprocess_scan_areds(scan_path)
else:
scan = preprocess_scan_daamd(scan_path)
return scan
def preprocess_scan_areds(scan_path):
img_names = [img for img in os.listdir(scan_path) if img.endswith('.png')]
scan = []
for img_name in img_names[2:-2]:
img_path = os.path.join(scan_path, img_name)
img = cv2.imread(img_path)
if len(img.shape) > 2:
img = img[:,:,0]
if (img.shape[0] != 224) or (img.shape[1] != 448):
img = cv2.resize(img, (448, 224), cv2.INTER_CUBIC)
scan.append(img)
scan = np.stack(scan, axis=-1)
scan = normalize_scan(scan)
return scan
def preprocess_scan_daamd(scan_path):
img_names = [img for img in os.listdir(scan_path) if img.endswith('.png')]
scan = []
for img_name in img_names:
img_path = os.path.join(scan_path, img_name)
img = cv2.imread(img_path)
if len(img.shape) > 2:
img = img[:,:,0]
if (img.shape[0] != 224) or (img.shape[1] != 448):
img = cv2.resize(img, (448, 224), cv2.INTER_CUBIC)
scan.append(img)
scan = np.stack(scan, axis=-1)
scan = np.pad(scan, ((0, 0), (0, 0), (3, 4)), 'constant', constant_values=0)
output_shape = (224, 448, 128)
scan = normalize_scan(scan)
return scan
def match_dims(scan):
scan = np.expand_dims(scan, axis=0)
scan = np.expand_dims(scan, axis=-1)
return scan