def load_cube(self, image, padding_strategy, padded_size=None): output_array = None if padded_size is None: padded_size = self.roi_size self.padded_size = padded_size diagonal_length = np.sqrt(np.sum(np.power(padded_size, 2))) diagonal_size = [diagonal_length , diagonal_length , diagonal_length] for idx, i in enumerate(self.roi_size): if (diagonal_size[idx] - i) % 2 != 0: diagonal_size[idx] += 1 pass # self.image_z_spacing = image.GetSpacing()[2] diagonal_size = np.array(diagonal_size) if padding_strategy == 'neighbour': max_sz = diagonal_size max_start = np.int16(np.floor(self.roi_center - max_sz / 2)) max_end = np.int16(np.floor(self.roi_center + max_sz / 2)) output_array = np.float32(ImageHelper.get_roi(image, max_start.tolist(), np.int16(max_sz).tolist())) """ output_array = np.float32(ndarray[ max_start[0]:max_end[0], max_start[1]:max_end[1], max_start[2]:max_end[2] ]) """ elif padding_strategy in [None, 'zero', 'repeat']: output_array = np.float32(ImageHelper.get_roi(image, self.roi_start_idx.tolist(), self.roi_idx_size.tolist())) output_array = ImageHelper.pad_image(output_array, diagonal_size, mode=padding_strategy) """ output_array = np.float32(ndarray[ self.roi_start_idx[0]:self.roi_end_idx[0], self.roi_start_idx[1]:self.roi_end_idx[1], self.roi_start_idx[2]:self.roi_end_idx[2] ]) """ else: raise ValueError("Unrecognized padding mode {0}".format(padding_strategy)) self.cube = output_array # self.test(image) return self.cube
def worker(self): while not self.shutdown_signal: q = self.q cube_size = self.input_cube_size cube_half_size = cube_size / 2 image_size = list(self.ndarray.shape) x_range = [[ floor(int(image_size[0] * .15) + cube_half_size[0]), floor(int(image_size[0] * .45) - cube_half_size[0]) ], [ floor(int(image_size[0] * .55) + cube_half_size[0]), floor(int(image_size[0] * .80) - cube_half_size[0]) ]] y_range = [ floor(int(image_size[1] * .25) + cube_half_size[1]), floor(int(image_size[1] * .75) - cube_half_size[1]) ] z_range = [ floor(int(image_size[2] * .3) + cube_half_size[2]), floor(int(image_size[2] * .6) - cube_half_size[2]) ] random_center = [ random.randint(*x_range[random.randint(0, 1)]), random.randint(*y_range), random.randint(*z_range) ] cube = self.ndarray[ floor(random_center[0] - cube_half_size[0]):floor(random_center[0] + cube_half_size[0]), floor(random_center[1] - cube_half_size[1]):floor(random_center[1] + cube_half_size[1]), floor(random_center[2] - cube_half_size[2]):floor(random_center[2] + cube_half_size[2]), ] cube = ImageHelper.pad_image(cube, self.model_cube_size) cube = ImageHelper.add_dim(cube) result = self.model.test({'image': np.array([cube])}) q.put([random_center, cube, result])
def get_cube(self, rotation=None, padding=True, as_nd=True): cube_shape = np.array(self.cube.shape) half_cube_shape = cube_shape/2 half_roi_size = (self.roi_idx_size/2) idx_start = np.int16(np.floor(half_cube_shape - half_roi_size)) idx_end = np.int16(np.floor(half_cube_shape + half_roi_size)) actual_size = self.roi_idx_size padded_size = self.padded_size # print(cube_shape) # print(padded_size) # print(actual_size) if rotation is not None: if sum(rotation) == 0: rotation = None ndarray = None if rotation is None: # No rotation, skip all these cpu intensive operations ndarray = self.cube[ idx_start[0]:idx_end[0], idx_start[1]:idx_end[1], idx_start[2]:idx_end[2] ] else: # We have rotation rotation = np.float32(rotation) * (math.pi/180) rotation_center = half_cube_shape rigid_euler = sitk.Euler3DTransform() # rigid_euler.SetRotation(float(rotation[0]), float(rotation[1]), float(rotation[2])) rigid_euler.SetRotation(float(rotation[0]), float(rotation[1]), float(rotation[2])) rigid_euler.SetCenter(rotation_center - 0.5) ndarray = np.swapaxes( sitk.GetArrayFromImage( sitk.Resample( sitk.GetImageFromArray(np.swapaxes(self.cube, 0, 2)), rigid_euler, sitk.sitkLinear, 0, sitk.sitkFloat32 ) ), 0, 2) new_center = (np.array(ndarray.shape) / 2) new_start = np.int16(new_center - half_roi_size) new_end = np.int16(new_center + half_roi_size) ndarray = np.float32(ndarray[ new_start[0]:new_end[0], new_start[1]:new_end[1], new_start[2]:new_end[2] ]) # Padding is done after rotation, maybe try to pad before rotation if padding: # print(ndarray.shape) # print(padded_size) ndarray = ImageHelper.pad_image(ndarray, padded_size, mode='zero') # print(ndarray.shape) return ndarray if as_nd else sitk.GetImageFromArray(np.swapaxes(ndarray, 0, 2))