def coscia_apply(modelObj: DynamicDLModel, data: dict): from dl.common.padorcut import padorcut from dl.common.preprocess_train import split_mirror from scipy.ndimage import zoom try: np except: import numpy as np from dl.labels.thigh import long_labels as LABELS_DICT MODEL_RESOLUTION = np.array([1.037037, 1.037037]) MODEL_SIZE = (432, 432) netc = modelObj.model resolution = np.array(data['resolution']) zoomFactor = resolution / MODEL_RESOLUTION img = data['image'] originalShape = img.shape img = zoom(img, zoomFactor) # resample the image to the model resolution img = padorcut(img, MODEL_SIZE) segmentation = netc.predict( np.expand_dims(np.stack([img, np.zeros(MODEL_SIZE)], axis=-1), axis=0)) labelsMask = np.argmax(np.squeeze(segmentation[0, :, :, :13]), axis=2) if (data['split_laterality']): a1, a2, a3, a4, b1, b2 = split_mirror(img) labelsMask_left = np.zeros(MODEL_SIZE, dtype='float32') labelsMask_right = np.zeros(MODEL_SIZE, dtype='float32') labelsMask_left[int(b1):int(b2), int(a1):int(a2)] = labelsMask[int(b1):int(b2), int(a1):int(a2)] labelsMask_right[int(b1):int(b2), int(a3):int(a4)] = labelsMask[int(b1):int(b2), int(a3):int(a4)] labelsMask_left = zoom(labelsMask_left, 1 / zoomFactor, order=0) labelsMask_left = padorcut(labelsMask_left, originalShape).astype(np.int8) labelsMask_right = zoom(labelsMask_right, 1 / zoomFactor, order=0) labelsMask_right = padorcut(labelsMask_right, originalShape).astype(np.int8) outputLabels = {} for labelValue, labelName in LABELS_DICT.items(): outputLabels[labelName + '_L'] = (labelsMask_left == labelValue).astype( np.int8) outputLabels[labelName + '_R'] = (labelsMask_right == labelValue).astype( np.int8) return outputLabels else: labelsMask = zoom(labelsMask, 1 / zoomFactor, order=0) labelsMask = padorcut(labelsMask, originalShape).astype(np.int8) outputLabels = {} for labelValue, labelName in LABELS_DICT.items(): outputLabels[labelName] = (labelsMask == labelValue).astype( np.int8) return outputLabels
def class_apply(modelObj: DynamicDLModel, data: dict): from dl.common.padorcut import padorcut from scipy.ndimage import zoom try: np except: import numpy as np LABELS_DICT = { 0: 'Thigh', 1: 'Leg' } MODEL_RESOLUTION = np.array([1.037037, 1.037037])*432/128 # the resolution is for an image of 432x432 but we process 128x128 images MODEL_SIZE = (128,128) netc = modelObj.model resolution = np.array(data['resolution']) zoomFactor = resolution/MODEL_RESOLUTION img = data['image'] img = zoom(img, zoomFactor) # resample the image to the model resolution img = padorcut(img, MODEL_SIZE) categories = netc.predict(np.expand_dims(img,axis=0)) value = categories[0].argmax() try: return LABELS_DICT[value] except KeyError: return None
def calcTransform(dataFix, dataFixInfo, dataMov, dataMovInfo, doElastix=True): if type(dataFixInfo) != list: dataFixInfo = [dataFixInfo] # make sure that dataFixInfo is a list twoDMode = (len(dataFixInfo) == 1 ) # this identifies 2d mode: fix data is a single slice assert not twoDMode or not doElastix, "Elastix registration can only be used in 3D mode" #print("2D Mode", twoDMode) transform = DatasetTransform() movSz = np.array( [dataMovInfo[0].Rows, dataMovInfo[0].Columns, len(dataMovInfo)], dtype=np.uint16) fixSz = np.array( [dataFixInfo[0].Rows, dataFixInfo[0].Columns, len(dataFixInfo)], dtype=np.uint16) # print(movSz, fixSz) rowMovOrientation = np.array(dataMovInfo[0].ImageOrientationPatient[3:6]) colMovOrientation = np.array(dataMovInfo[0].ImageOrientationPatient[0:3]) slcMovOrientation = np.cross(rowMovOrientation, colMovOrientation) # get difference between two slices sliceMovDiff = np.array(dataMovInfo[1].ImagePositionPatient) - np.array( dataMovInfo[0].ImagePositionPatient) if np.dot(sliceMovDiff, slcMovOrientation) < 0: transform.swapSlicesMoving = True if doElastix: dataMov = dataMov[:, :, ::-1] # invert slice orientation dataMovInfo = dataMovInfo[::-1] rowFixOrientation = np.array(dataFixInfo[0].ImageOrientationPatient[3:6]) colFixOrientation = np.array(dataFixInfo[0].ImageOrientationPatient[0:3]) slcFixOrientation = np.cross(rowFixOrientation, colFixOrientation) if not twoDMode: # get difference between two slices sliceFixDiff = np.array( dataFixInfo[1].ImagePositionPatient) - np.array( dataFixInfo[0].ImagePositionPatient) if np.dot(sliceFixDiff, slcFixOrientation) < 0: transform.swapSlicesFix = True if doElastix: dataFix = dataFix[:, :, ::-1] # invert slice orientation dataFixInfo = dataFixInfo[::-1] # print(np.array(dataMovInfo[0].ImagePositionPatient)) # print(np.array(dataFixInfo[0].ImagePositionPatient)) ijkMov = np.array( [rowMovOrientation, colMovOrientation, slcMovOrientation]).T ijkFix = np.array( [rowFixOrientation, colFixOrientation, slcFixOrientation]).T # print(ijkMov, ijkFix) MovToFix = np.matmul(ijkFix.T, ijkMov) angles = rotationMatrixToEulerAngles(MovToFix) transform.rotationAngles = angles try: sliceResMov = dataMovInfo[0].SpacingBetweenSlices except: sliceResMov = dataMovInfo[0].SliceThickness try: sliceResFix = dataFixInfo[0].SpacingBetweenSlices except: sliceResFix = dataFixInfo[0].SliceThickness if twoDMode: sliceResFix = dataFixInfo[ 0].SliceThickness # force slice thickness in case of 2d mode MovResolution = np.hstack([dataMovInfo[0].PixelSpacing, sliceResMov]) FixResolution = np.hstack([dataFixInfo[0].PixelSpacing, sliceResFix]) MovCenterPoint = (movSz.astype(np.float32) - 1) / 2 FixCenterPoint = (fixSz.astype(np.float32) - 1) / 2 if doElastix: print("Rotating...") dataMovRotated = ndimage.rotate(dataMov, angles[0], axes=(1, 2)) dataMovRotated = ndimage.rotate(dataMovRotated, angles[1], axes=(0, 2)) dataMovRotated = ndimage.rotate(dataMovRotated, angles[2], axes=(0, 1)) rotatedMovResolution = np.matmul(MovToFix, MovResolution) zoom = np.abs(rotatedMovResolution / FixResolution) transform.zoom = zoom if doElastix: dataMovExtended = ndimage.zoom(dataMovRotated, zoom) # MovCenterPointWorld = np.matmul(ijkMov,MovCenterPoint.T*MovResolution + np.hstack([ dataMovInfo[0].PixelSpacing, dataMovInfo[0].SliceThickness ])/2 ) + dataMovInfo[0].ImagePositionPatient # FixCenterPointWorld = np.matmul(ijkFix,FixCenterPoint.T*FixResolution + np.hstack([ dataFixInfo[0].PixelSpacing, dataFixInfo[0].SliceThickness ])/2 ) + dataFixInfo[0].ImagePositionPatient MovCenterPointWorld = np.matmul( ijkMov, MovCenterPoint.T * MovResolution) + dataMovInfo[0].ImagePositionPatient FixCenterPointWorld = np.matmul( ijkFix, FixCenterPoint.T * FixResolution) + dataFixInfo[0].ImagePositionPatient #print(MovCenterPointWorld, FixCenterPointWorld) diffCenterWorld = (FixCenterPointWorld - MovCenterPointWorld) diffCenter = np.matmul(ijkFix.T, diffCenterWorld.T) / FixResolution shift = -diffCenter transform.shift = shift transform.endSize = fixSz if doElastix: dataMovShifted = ndimage.shift(dataMovExtended, shift) #dataFix2 = padorcut(dataFix, fixSz + 8) dataMov2 = padorcut(dataMovShifted, fixSz) #fixMask = np.zeros_like(dataFix2) #fixMask[dataFix2 > 1] = 1 elastixImageFilter = sitk.ElastixImageFilter() elastixImageFilter.SetLogToConsole(False) elastixImageFilter.SetLogToFile(False) elastixImageFilter.SetParameterMap( sitk.GetDefaultParameterMap("affine")) elastixImageFilter.SetFixedImage(sitk.GetImageFromArray(dataFix)) #elastixImageFilter.SetFixedMask(sitk.GetImageFromArray(fixMask.astype(np.uint8))) elastixImageFilter.SetMovingImage(sitk.GetImageFromArray(dataMov2)) print("Registering...") #elastixImageFilter.SetLogToFile(True) #elastixImageFilter.SetLogFileName('elastix.log') elastixImageFilter.Execute() transform.elastixTransform = elastixImageFilter.GetTransformParameterMap( ) print("Done") return transform
def transform(self, datasetIn, mode2d=False, maskMode=False): # the moving image (e.g. the mask) needs to have the slices flipped to be consistent if self.swapSlicesMoving: dataset = datasetIn[:, :, ::-1] else: dataset = datasetIn if maskMode: interp_order = 0 else: interp_order = 3 #print "Rotating 1" dataMovRotated = ndimage.rotate(dataset, self.rotationAngles[0], axes=(1, 2), order=interp_order) #print "Rotating 2" dataMovRotated = ndimage.rotate(dataMovRotated, self.rotationAngles[1], axes=(0, 2), order=interp_order) #print "Rotating 3" dataMovRotated = ndimage.rotate(dataMovRotated, self.rotationAngles[2], axes=(0, 1), order=interp_order) if not mode2d: dataMovExtended = ndimage.zoom(dataMovRotated, self.zoom, order=interp_order) dataMovShifted = ndimage.shift(dataMovExtended, self.shift, order=interp_order) else: #print("2D mode") dataMovExtended = ndimage.zoom(dataMovRotated, (self.zoom[0], self.zoom[1], 1), order=interp_order) dataMovShifted = ndimage.shift( dataMovExtended, (self.shift[0], self.shift[1], self.shift[2] / self.zoom[2]), order=interp_order) dataMovShifted = ndimage.zoom(dataMovShifted, (1, 1, self.zoom[2]), order=0) #dataMov2 = padorcut(dataMovShifted, self.endSize) dataMov2 = padorcut(dataMovShifted, np.array(self.endSize)) if self.elastixTransform: # apply transformix transformixImageFilter = sitk.TransformixImageFilter() transformixImageFilter.SetLogToConsole(False) transformixImageFilter.SetLogToFile(False) if maskMode: for t in self.elastixTransform: t['ResampleInterpolator'] = [ "FinalNearestNeighborInterpolator" ] transformixImageFilter.SetTransformParameterMap( self.elastixTransform) transformixImageFilter.SetMovingImage( sitk.GetImageFromArray(dataMov2)) transformixImageFilter.Execute() datasetOut = sitk.GetArrayFromImage( transformixImageFilter.GetResultImage()) else: datasetOut = dataMov2 #datasetOut = padorcut(datasetOut, self.endSize) # the original fixed image (i.e. the target of the alignment has the slices swapped, so swap back) if self.swapSlicesFix: datasetOut = datasetOut[:, :, ::-1] return datasetOut
def coscia_apply(modelObj: DynamicDLModel, data: dict): from dl.common.padorcut import padorcut import dl.common.biascorrection as biascorrection import dl.common.preprocess_train as pretrain from dl.common.preprocess_train import split_mirror from scipy.ndimage import zoom try: np except: import numpy as np from dl.labels.thigh import long_labels as LABELS_DICT MODEL_RESOLUTION = np.array([1.037037, 1.037037]) MODEL_SIZE = (432, 432) MODEL_SIZE_SPLIT = (250, 250) netc = modelObj.model resolution = np.array(data['resolution']) zoomFactor = resolution / MODEL_RESOLUTION img = data['image'] originalShape = img.shape img = zoom(img, zoomFactor) # resample the image to the model resolution img = padorcut(img, MODEL_SIZE) imgbc = biascorrection.biascorrection_image(img) a1, a2, a3, a4, b1, b2 = split_mirror(imgbc) left = imgbc[int(b1):int(b2), int(a1):int(a2)] left = padorcut(left, MODEL_SIZE_SPLIT) right = imgbc[int(b1):int(b2), int(a3):int(a4)] right = right[::1, ::-1] right = padorcut(right, MODEL_SIZE_SPLIT) segmentationleft = netc.predict( np.expand_dims(np.stack([left, np.zeros(MODEL_SIZE_SPLIT)], axis=-1), axis=0)) labelleft = np.argmax(np.squeeze(segmentationleft[0, :, :, :13]), axis=2) segmentationright = netc.predict( np.expand_dims(np.stack([right, np.zeros(MODEL_SIZE_SPLIT)], axis=-1), axis=0)) labelright = np.argmax(np.squeeze(segmentationright[0, :, :, :13]), axis=2) labelright = labelright[::1, ::-1] labelsMask_left = np.zeros(MODEL_SIZE, dtype='float32') labelsMask_right = np.zeros(MODEL_SIZE, dtype='float32') labelsMask_left[int(b1):int(b2), int(a1):int(a2)] = padorcut(labelleft, [b2 - b1, a2 - a1]) labelsMask_right[int(b1):int(b2), int(a3):int(a4)] = padorcut(labelright, [b2 - b1, a4 - a3]) labelsMask_left = zoom(labelsMask_left, 1 / zoomFactor, order=0) labelsMask_left = padorcut(labelsMask_left, originalShape).astype(np.int8) labelsMask_right = zoom(labelsMask_right, 1 / zoomFactor, order=0) labelsMask_right = padorcut(labelsMask_right, originalShape).astype(np.int8) outputLabels = {} if data['split_laterality']: for labelValue, labelName in LABELS_DICT.items(): outputLabels[labelName + '_R'] = ( labelsMask_left == labelValue).astype( np.int8) # left in the image is right in the anatomy outputLabels[labelName + '_L'] = (labelsMask_right == labelValue).astype( np.int8) return outputLabels else: for labelValue, labelName in LABELS_DICT.items(): outputLabels[labelName] = np.logical_or( labelsMask_left == labelValue, labelsMask_right == labelValue).astype(np.int8) return outputLabels
def gamba_apply(modelObj: DynamicDLModel, data: dict): from dl.common.padorcut import padorcut import dl.common.biascorrection as biascorrection import dl.common.preprocess_train as pretrain from dl.common.preprocess_train import split_mirror from scipy.ndimage import zoom try: np except: import numpy as np from dl.labels.leg import long_labels as LABELS_DICT MODEL_RESOLUTION = np.array([1.037037, 1.037037]) MODEL_SIZE = (432, 432) MODEL_SIZE_SPLIT = (216, 216) classification = data.get('classification', '') single_side = False swap = False # Note: this is anatomical right, which means it's image left! It's the image right that is swapped if classification.lower().strip().endswith('right'): single_side = True swap = False elif classification.lower().strip().endswith('left'): single_side = True swap = True # otherwise: double sided netc = modelObj.model resolution = np.array(data['resolution']) zoomFactor = resolution / MODEL_RESOLUTION img = data['image'] originalShape = img.shape img = zoom(img, zoomFactor) # resample the image to the model resolution if single_side: img = padorcut(img, MODEL_SIZE_SPLIT) if swap: img = img[::1, ::-1] segmentation = netc.predict(np.expand_dims(np.stack([img, np.zeros(MODEL_SIZE_SPLIT)], axis=-1), axis=0)) label = np.argmax(np.squeeze(segmentation[0, :, :, :13]), axis=2) if swap: label = label[::1, ::-1] labelsMask = zoom(label, 1 / zoomFactor, order=0) labelsMask = padorcut(labelsMask, originalShape).astype(np.int8) outputLabels = {} suffix = '' if data['split_laterality']: suffix = '_L' if swap else '_R' for labelValue, labelName in LABELS_DICT.items(): outputLabels[labelName + suffix] = (labelsMask == labelValue).astype( np.int8) # left in the image is right in the anatomy return outputLabels # two sides img = padorcut(img, MODEL_SIZE) imgbc=biascorrection.biascorrection_image(img) a1,a2,a3,a4,b1,b2=split_mirror(imgbc) left=imgbc[int(b1):int(b2),int(a1):int(a2)] left=padorcut(left, MODEL_SIZE_SPLIT) right=imgbc[int(b1):int(b2),int(a3):int(a4)] right=right[::1,::-1] right=padorcut(right, MODEL_SIZE_SPLIT) segmentationleft=netc.predict(np.expand_dims(np.stack([left,np.zeros(MODEL_SIZE_SPLIT)],axis=-1),axis=0)) labelleft=np.argmax(np.squeeze(segmentationleft[0,:,:,:7]), axis=2) segmentationright=netc.predict(np.expand_dims(np.stack([right,np.zeros(MODEL_SIZE_SPLIT)],axis=-1),axis=0)) labelright=np.argmax(np.squeeze(segmentationright[0,:,:,:7]), axis=2) labelright=labelright[::1,::-1] labelsMask_left=np.zeros(MODEL_SIZE,dtype='float32') labelsMask_right=np.zeros(MODEL_SIZE,dtype='float32') labelsMask_left[int(b1):int(b2),int(a1):int(a2)]=padorcut(labelleft, [b2-b1, a2-a1]) labelsMask_right[int(b1):int(b2),int(a3):int(a4)]=padorcut(labelright, [b2-b1, a4-a3]) labelsMask_left = zoom(labelsMask_left, 1/zoomFactor, order=0) labelsMask_left = padorcut(labelsMask_left, originalShape).astype(np.int8) labelsMask_right = zoom(labelsMask_right, 1/zoomFactor, order=0) labelsMask_right = padorcut(labelsMask_right, originalShape).astype(np.int8) outputLabels = {} if data['split_laterality']: for labelValue, labelName in LABELS_DICT.items(): outputLabels[labelName+'_R'] = (labelsMask_left == labelValue).astype(np.int8) outputLabels[labelName+'_L'] = (labelsMask_right == labelValue).astype(np.int8) return outputLabels else: for labelValue, labelName in LABELS_DICT.items(): outputLabels[labelName] = np.logical_or(labelsMask_left == labelValue, labelsMask_right == labelValue).astype(np.int8) return outputLabels