|
| 1 | +import numpy as np |
| 2 | +import SimpleITK as sitk |
| 3 | +from os import listdir |
| 4 | +from os.path import isfile, join, splitext |
| 5 | + |
| 6 | +class DataManager(object): |
| 7 | + params=None |
| 8 | + srcFolder=None |
| 9 | + resultsDir=None |
| 10 | + |
| 11 | + fileList=None |
| 12 | + gtList=None |
| 13 | + |
| 14 | + sitkImages=None |
| 15 | + sitkGT=None |
| 16 | + meanIntensityTrain = None |
| 17 | + |
| 18 | + def __init__(self,srcFolder,resultsDir,parameters): |
| 19 | + self.params=parameters |
| 20 | + self.srcFolder=srcFolder |
| 21 | + self.resultsDir=resultsDir |
| 22 | + |
| 23 | + def createImageFileList(self): |
| 24 | + self.fileList = [f for f in listdir(self.srcFolder) if isfile(join(self.srcFolder, f)) and 'segmentation' not in f and 'raw' not in f] |
| 25 | + print 'FILE LIST: ' + str(self.fileList) |
| 26 | + |
| 27 | + |
| 28 | + def createGTFileList(self): |
| 29 | + self.gtList=list() |
| 30 | + for f in self.fileList: |
| 31 | + filename, ext = splitext(f) |
| 32 | + self.gtList.append(join(filename + '_segmentation' + ext)) |
| 33 | + |
| 34 | + |
| 35 | + def loadImages(self): |
| 36 | + self.sitkImages=dict() |
| 37 | + rescalFilt=sitk.RescaleIntensityImageFilter() |
| 38 | + rescalFilt.SetOutputMaximum(1) |
| 39 | + rescalFilt.SetOutputMinimum(0) |
| 40 | + |
| 41 | + stats = sitk.StatisticsImageFilter() |
| 42 | + m = 0. |
| 43 | + for f in self.fileList: |
| 44 | + self.sitkImages[f]=rescalFilt.Execute(sitk.Cast(sitk.ReadImage(join(self.srcFolder, f)),sitk.sitkFloat32)) |
| 45 | + stats.Execute(self.sitkImages[f]) |
| 46 | + m += stats.GetMean() |
| 47 | + |
| 48 | + self.meanIntensityTrain=m/len(self.sitkImages) |
| 49 | + |
| 50 | + |
| 51 | + def loadGT(self): |
| 52 | + self.sitkGT=dict() |
| 53 | + |
| 54 | + for f in self.gtList: |
| 55 | + self.sitkGT[f]=sitk.Cast(sitk.ReadImage(join(self.srcFolder, f))>0.5,sitk.sitkFloat32) |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + def loadTrainingData(self): |
| 60 | + self.createImageFileList() |
| 61 | + self.createGTFileList() |
| 62 | + self.loadImages() |
| 63 | + self.loadGT() |
| 64 | + |
| 65 | + |
| 66 | + def loadTestData(self): |
| 67 | + self.createImageFileList() |
| 68 | + self.loadImages() |
| 69 | + |
| 70 | + def getNumpyImages(self): |
| 71 | + dat = self.getNumpyData(self.sitkImages,sitk.sitkLinear) |
| 72 | + return dat |
| 73 | + |
| 74 | + |
| 75 | + def getNumpyGT(self): |
| 76 | + dat = self.getNumpyData(self.sitkGT,sitk.sitkLinear) |
| 77 | + |
| 78 | + for key in dat: |
| 79 | + dat[key] = (dat[key]>0.5).astype(dtype=np.float32) |
| 80 | + |
| 81 | + return dat |
| 82 | + |
| 83 | + |
| 84 | + def getNumpyData(self,dat,method): |
| 85 | + ret=dict() |
| 86 | + for key in dat: |
| 87 | + ret[key] = np.zeros([self.params['VolSize'][0], self.params['VolSize'][1], self.params['VolSize'][2]], dtype=np.float32) |
| 88 | + |
| 89 | + img=dat[key] |
| 90 | + |
| 91 | + #we rotate the image according to its transformation using the direction and according to the final spacing we want |
| 92 | + factor = np.asarray(img.GetSpacing()) / [self.params['dstRes'][0], self.params['dstRes'][1], |
| 93 | + self.params['dstRes'][2]] |
| 94 | + |
| 95 | + factorSize = np.asarray(img.GetSize() * factor, dtype=float) |
| 96 | + |
| 97 | + newSize = np.max([factorSize, self.params['VolSize']], axis=0) |
| 98 | + |
| 99 | + newSize = newSize.astype(dtype=int) |
| 100 | + |
| 101 | + T=sitk.AffineTransform(3) |
| 102 | + T.SetMatrix(img.GetDirection()) |
| 103 | + |
| 104 | + resampler = sitk.ResampleImageFilter() |
| 105 | + resampler.SetReferenceImage(img) |
| 106 | + resampler.SetOutputSpacing([self.params['dstRes'][0], self.params['dstRes'][1], self.params['dstRes'][2]]) |
| 107 | + resampler.SetSize(newSize) |
| 108 | + resampler.SetInterpolator(method) |
| 109 | + if self.params['normDir']: |
| 110 | + resampler.SetTransform(T.GetInverse()) |
| 111 | + |
| 112 | + imgResampled = resampler.Execute(img) |
| 113 | + |
| 114 | + |
| 115 | + imgCentroid = np.asarray(newSize, dtype=float) / 2.0 |
| 116 | + |
| 117 | + imgStartPx = (imgCentroid - self.params['VolSize'] / 2.0).astype(dtype=int) |
| 118 | + |
| 119 | + regionExtractor = sitk.RegionOfInterestImageFilter() |
| 120 | + regionExtractor.SetSize(list(self.params['VolSize'].astype(dtype=int))) |
| 121 | + regionExtractor.SetIndex(list(imgStartPx)) |
| 122 | + |
| 123 | + imgResampledCropped = regionExtractor.Execute(imgResampled) |
| 124 | + |
| 125 | + ret[key] = np.transpose(sitk.GetArrayFromImage(imgResampledCropped).astype(dtype=float), [2, 1, 0]) |
| 126 | + |
| 127 | + return ret |
| 128 | + |
| 129 | + |
| 130 | + def writeResultsFromNumpyLabel(self,result,key): |
| 131 | + img = self.sitkImages[key] |
| 132 | + |
| 133 | + toWrite=sitk.Image(img.GetSize()[0],img.GetSize()[1],img.GetSize()[2],sitk.sitkFloat32) |
| 134 | + |
| 135 | + factor = np.asarray(img.GetSpacing()) / [self.params['dstRes'][0], self.params['dstRes'][1], |
| 136 | + self.params['dstRes'][2]] |
| 137 | + |
| 138 | + factorSize = np.asarray(img.GetSize() * factor, dtype=float) |
| 139 | + |
| 140 | + newSize = np.max([factorSize, self.params['VolSize']], axis=0) |
| 141 | + |
| 142 | + newSize = newSize.astype(dtype=int) |
| 143 | + |
| 144 | + T = sitk.AffineTransform(3) |
| 145 | + T.SetMatrix(img.GetDirection()) |
| 146 | + |
| 147 | + resampler = sitk.ResampleImageFilter() |
| 148 | + resampler.SetReferenceImage(img) |
| 149 | + resampler.SetOutputSpacing([self.params['dstRes'][0], self.params['dstRes'][1], self.params['dstRes'][2]]) |
| 150 | + resampler.SetSize(newSize) |
| 151 | + resampler.SetInterpolator(sitk.sitkNearestNeighbor) |
| 152 | + |
| 153 | + if self.params['normDir']: |
| 154 | + resampler.SetTransform(T.GetInverse()) |
| 155 | + |
| 156 | + toWrite = resampler.Execute(toWrite) |
| 157 | + |
| 158 | + imgCentroid = np.asarray(newSize, dtype=float) / 2.0 |
| 159 | + |
| 160 | + imgStartPx = (imgCentroid - self.params['VolSize'] / 2.0).astype(dtype=int) |
| 161 | + |
| 162 | + for dstX, srcX in zip(range(0, result.shape[0]), range(imgStartPx[0],int(imgStartPx[0]+self.params['VolSize'][0]))): |
| 163 | + for dstY, srcY in zip(range(0, result.shape[1]), range(imgStartPx[1], int(imgStartPx[1]+self.params['VolSize'][1]))): |
| 164 | + for dstZ, srcZ in zip(range(0, result.shape[2]), range(imgStartPx[2], int(imgStartPx[2]+self.params['VolSize'][2]))): |
| 165 | + try: |
| 166 | + toWrite.SetPixel(int(srcX),int(srcY),int(srcZ),float(result[dstX,dstY,dstZ])) |
| 167 | + except: |
| 168 | + pass |
| 169 | + |
| 170 | + |
| 171 | + resampler.SetOutputSpacing([img.GetSpacing()[0], img.GetSpacing()[1], img.GetSpacing()[2]]) |
| 172 | + resampler.SetSize(img.GetSize()) |
| 173 | + |
| 174 | + if self.params['normDir']: |
| 175 | + resampler.SetTransform(T) |
| 176 | + |
| 177 | + toWrite = resampler.Execute(toWrite) |
| 178 | + |
| 179 | + thfilter=sitk.BinaryThresholdImageFilter() |
| 180 | + thfilter.SetInsideValue(1) |
| 181 | + thfilter.SetOutsideValue(0) |
| 182 | + thfilter.SetLowerThreshold(0.5) |
| 183 | + toWrite = thfilter.Execute(toWrite) |
| 184 | + |
| 185 | + #connected component analysis (better safe than sorry) |
| 186 | + |
| 187 | + cc = sitk.ConnectedComponentImageFilter() |
| 188 | + toWritecc = cc.Execute(sitk.Cast(toWrite,sitk.sitkUInt8)) |
| 189 | + |
| 190 | + arrCC=np.transpose(sitk.GetArrayFromImage(toWritecc).astype(dtype=float), [2, 1, 0]) |
| 191 | + |
| 192 | + lab=np.zeros(int(np.max(arrCC)+1),dtype=float) |
| 193 | + |
| 194 | + for i in range(1,int(np.max(arrCC)+1)): |
| 195 | + lab[i]=np.sum(arrCC==i) |
| 196 | + |
| 197 | + activeLab=np.argmax(lab) |
| 198 | + |
| 199 | + toWrite = (toWritecc==activeLab) |
| 200 | + |
| 201 | + toWrite = sitk.Cast(toWrite,sitk.sitkUInt8) |
| 202 | + |
| 203 | + writer = sitk.ImageFileWriter() |
| 204 | + filename, ext = splitext(key) |
| 205 | + #print join(self.resultsDir, filename + '_result' + ext) |
| 206 | + writer.SetFileName(join(self.resultsDir, filename + '_result' + ext)) |
| 207 | + writer.Execute(toWrite) |
| 208 | + |
0 commit comments