|
a |
|
b/patient_loader.py |
|
|
1 |
import os |
|
|
2 |
import cv2 |
|
|
3 |
import numpy as np |
|
|
4 |
from random import randrange |
|
|
5 |
|
|
|
6 |
import utils |
|
|
7 |
import config |
|
|
8 |
import lung_segmentation as ls |
|
|
9 |
|
|
|
10 |
|
|
|
11 |
segmentation_algo = ls.get_segmentation_algorithm() |
|
|
12 |
|
|
|
13 |
|
|
|
14 |
class PatientImageLoader(object): |
|
|
15 |
def __init__(self, images_dir): |
|
|
16 |
self._images_input = images_dir or config.SEGMENTED_LUNGS_DIR |
|
|
17 |
self._augment = False |
|
|
18 |
|
|
|
19 |
def load_scans(self, patient): |
|
|
20 |
if 'augm' in patient: |
|
|
21 |
self._augment = True |
|
|
22 |
patient = patient.split('-')[0] |
|
|
23 |
return utils.load_patient_image(self._images_input, patient) |
|
|
24 |
|
|
|
25 |
@property |
|
|
26 |
def images_input(self): |
|
|
27 |
return self._images_input |
|
|
28 |
|
|
|
29 |
@property |
|
|
30 |
def name(self): |
|
|
31 |
return 'base_image_loader' |
|
|
32 |
|
|
|
33 |
|
|
|
34 |
# Tests with the mean scans loader are not using |
|
|
35 |
# lung segmentation, only compressed sorted slices in HU units. |
|
|
36 |
class MeanScansLoader(PatientImageLoader): |
|
|
37 |
def __init__(self, images_dir=None): |
|
|
38 |
super(MeanScansLoader, self).__init__(images_dir) |
|
|
39 |
|
|
|
40 |
def load_scans(self, patient): |
|
|
41 |
image = utils.load_patient_image(self._images_input, patient) |
|
|
42 |
image = utils.resize(image) |
|
|
43 |
|
|
|
44 |
return utils.get_mean_chunk_slices(image) |
|
|
45 |
|
|
|
46 |
@property |
|
|
47 |
def name(self): |
|
|
48 |
return 'mean_scans_loader' |
|
|
49 |
|
|
|
50 |
|
|
|
51 |
class SegmentedGaussianLungsLoader(PatientImageLoader): |
|
|
52 |
def __init__(self, images_dir=config.SEGMENTED_LUNGS_DIR): |
|
|
53 |
super(SegmentedGaussianLungsLoader, self).__init__(images_dir) |
|
|
54 |
|
|
|
55 |
def process_scans(self, image): |
|
|
56 |
image = np.stack([cv2.GaussianBlur(scan, (5, 5), 0) for scan in image]) |
|
|
57 |
image = utils.resize(image) |
|
|
58 |
|
|
|
59 |
return utils.trim_pad_slices(image, pad_with_existing=False) |
|
|
60 |
|
|
|
61 |
def load_scans(self, patient): |
|
|
62 |
image = utils.load_patient_image(self._images_input, patient) |
|
|
63 |
return self.process_scans(image) |
|
|
64 |
|
|
|
65 |
@property |
|
|
66 |
def name(self): |
|
|
67 |
return 'segmented_gaussian_lungs_loader' |
|
|
68 |
|
|
|
69 |
|
|
|
70 |
# Default loader |
|
|
71 |
class SegmentedLungsScansLoader(PatientImageLoader): |
|
|
72 |
def __init__(self, images_dir=config.SEGMENTED_LUNGS_DIR): |
|
|
73 |
super(SegmentedLungsScansLoader, self).__init__(images_dir) |
|
|
74 |
|
|
|
75 |
def process_scans(self, image): |
|
|
76 |
image = segmentation_algo.get_slices_with_nodules(image) |
|
|
77 |
image = utils.resize(image) |
|
|
78 |
|
|
|
79 |
if self._augment: |
|
|
80 |
angle = randrange(-15, 15) |
|
|
81 |
image = utils.rotate_scans(image, angle) |
|
|
82 |
|
|
|
83 |
return utils.trim_pad_slices(image, pad_with_existing=True) |
|
|
84 |
|
|
|
85 |
def load_scans(self, patient): |
|
|
86 |
image = super(SegmentedLungsScansLoader, self).load_scans(patient) |
|
|
87 |
return self.process_scans(image) |
|
|
88 |
|
|
|
89 |
@property |
|
|
90 |
def name(self): |
|
|
91 |
return 'segmented_lungs_loader' |
|
|
92 |
|
|
|
93 |
|
|
|
94 |
class NodulesScansLoader(PatientImageLoader): |
|
|
95 |
def __init__(self, images_dir=config.SEGMENTED_LUNGS_DIR): |
|
|
96 |
super(NodulesScansLoader, self).__init__(images_dir) |
|
|
97 |
|
|
|
98 |
def process_scans(self, patient): |
|
|
99 |
image = utils.load_patient_image(self._images_input, patient) |
|
|
100 |
nodules = segmentation_algo.get_lung_nodules_candidates(image) |
|
|
101 |
nodules = utils.resize(nodules) |
|
|
102 |
|
|
|
103 |
return utils.trim_pad_slices(nodules) |
|
|
104 |
|
|
|
105 |
def load_scans(self, patient): |
|
|
106 |
return self.process_scans(patient) |
|
|
107 |
|
|
|
108 |
@property |
|
|
109 |
def name(self): |
|
|
110 |
return 'nodules_scans_loader' |