def main(): args = getArguments(getParser()) # prepare logger logger = Logger.getInstance() if args.debug: logger.setLevel(logging.DEBUG) elif args.verbose: logger.setLevel(logging.INFO) # load input image data_input, header_input = load(args.input) # transform to uin8 data_input = data_input.astype(scipy.uint8) # reduce to 3D, if larger dimensionality if data_input.ndim > 3: for _ in range(data_input.ndim - 3): data_input = data_input[...,0] # iter over slices (2D) until first with content is detected for plane in data_input: if scipy.any(plane): # set pixel spacing spacing = list(header.get_pixel_spacing(header_input)) spacing = spacing[1:3] __update_header_from_array_nibabel(header_input, plane) header.set_pixel_spacing(header_input, spacing) # save image save(plane, args.output, header_input, args.force) break logger.info("Successfully terminated.")
def main(): # parse cmd arguments parser = getParser() parser.parse_args() args = getArguments(parser) # prepare logger logger = Logger.getInstance() if args.debug: logger.setLevel(logging.DEBUG) elif args.verbose: logger.setLevel(logging.INFO) # load first input image as example example_data, example_header = load(args.inputs[0]) # test if the supplied position is valid if args.position > example_data.ndim or args.position < 0: raise ArgumentError('The supplied position for the new dimension is invalid. It has to be between 0 and {}.'.format(example_data.ndim)) # prepare empty output volume output_data = scipy.zeros([len(args.inputs)] + list(example_data.shape), dtype=example_data.dtype) # add first image to output volume output_data[0] = example_data # load input images and add to output volume for idx, image in enumerate(args.inputs[1:]): image_data, _ = load(image) if not args.ignore and image_data.dtype != example_data.dtype: raise ArgumentError('The dtype {} of image {} differs from the one of the first image {}, which is {}.'.format(image_data.dtype, image, args.inputs[0], example_data.dtype)) if image_data.shape != example_data.shape: raise ArgumentError('The shape {} of image {} differs from the one of the first image {}, which is {}.'.format(image_data.shape, image, args.inputs[0], example_data.shape)) output_data[idx + 1] = image_data # move new dimension to the end or to target position for dim in range(output_data.ndim - 1): if dim >= args.position: break output_data = scipy.swapaxes(output_data, dim, dim + 1) # set pixel spacing spacing = list(header.get_pixel_spacing(example_header)) spacing = tuple(spacing[:args.position] + [args.spacing] + spacing[args.position:]) # !TODO: Find a way to enable this also for PyDicom and ITK images if __is_header_nibabel(example_header): __update_header_from_array_nibabel(example_header, output_data) header.set_pixel_spacing(example_header, spacing) else: raise ArgumentError("Sorry. Setting the voxel spacing of the new dimension only works with NIfTI images. See the description of this program for more details.") # save created volume save(output_data, args.output, example_header, args.force) logger.info("Successfully terminated.")
def main(): # parse cmd arguments parser = getParser() parser.parse_args() args = getArguments(parser) # prepare logger logger = Logger.getInstance() if args.debug: logger.setLevel(logging.DEBUG) elif args.verbose: logger.setLevel(logging.INFO) # load input image data_input, header_input = load(args.input) # check if the supplied dimension is valid if args.dimension >= data_input.ndim or args.dimension < 0: raise ArgumentError( 'The supplied cut-dimension {} exceeds the image dimensionality of 0 to {}.' .format(args.dimension, data_input.ndim - 1)) # prepare output file string name_output = args.output.replace('{}', '{:03d}') # compute the new the voxel spacing spacing = list(header.get_pixel_spacing(header_input)) del spacing[args.dimension] # iterate over the cut dimension slices = data_input.ndim * [slice(None)] for idx in range(data_input.shape[args.dimension]): # cut the current slice from the original image slices[args.dimension] = slice(idx, idx + 1) data_output = scipy.squeeze(data_input[slices]) # update the header and set the voxel spacing __update_header_from_array_nibabel(header_input, data_output) header.set_pixel_spacing(header_input, spacing) # save current slice save(data_output, name_output.format(idx), header_input, args.force) logger.info("Successfully terminated.")
def main(): # parse cmd arguments parser = getParser() parser.parse_args() args = getArguments(parser) # prepare logger logger = Logger.getInstance() if args.debug: logger.setLevel(logging.DEBUG) elif args.verbose: logger.setLevel(logging.INFO) # load input image data_input, header_input = load(args.input) # check if the supplied dimension is valid if args.dimension >= data_input.ndim or args.dimension < 0: raise ArgumentError('The supplied cut-dimension {} exceeds the image dimensionality of 0 to {}.'.format(args.dimension, data_input.ndim - 1)) # prepare output file string name_output = args.output.replace('{}', '{:03d}') # compute the new the voxel spacing spacing = list(header.get_pixel_spacing(header_input)) del spacing[args.dimension] # iterate over the cut dimension slices = data_input.ndim * [slice(None)] for idx in range(data_input.shape[args.dimension]): # cut the current slice from the original image slices[args.dimension] = slice(idx, idx + 1) data_output = scipy.squeeze(data_input[slices]) # update the header and set the voxel spacing __update_header_from_array_nibabel(header_input, data_output) header.set_pixel_spacing(header_input, spacing) # save current slice save(data_output, name_output.format(idx), header_input, args.force) logger.info("Successfully terminated.")
def main(): # parse cmd arguments parser = getParser() parser.parse_args() args = getArguments(parser) # prepare logger logger = Logger.getInstance() if args.debug: logger.setLevel(logging.DEBUG) elif args.verbose: logger.setLevel(logging.INFO) # load first input image as example example_data, example_header = load(args.inputs[0]) # test if the supplied position is valid if args.position > example_data.ndim or args.position < 0: raise ArgumentError( 'The supplied position for the new dimension is invalid. It has to be between 0 and {}.' .format(example_data.ndim)) # prepare empty output volume output_data = scipy.zeros([len(args.inputs)] + list(example_data.shape), dtype=example_data.dtype) # add first image to output volume output_data[0] = example_data # load input images and add to output volume for idx, image in enumerate(args.inputs[1:]): image_data, _ = load(image) if not args.ignore and image_data.dtype != example_data.dtype: raise ArgumentError( 'The dtype {} of image {} differs from the one of the first image {}, which is {}.' .format(image_data.dtype, image, args.inputs[0], example_data.dtype)) if image_data.shape != example_data.shape: raise ArgumentError( 'The shape {} of image {} differs from the one of the first image {}, which is {}.' .format(image_data.shape, image, args.inputs[0], example_data.shape)) output_data[idx + 1] = image_data # move new dimension to the end or to target position for dim in range(output_data.ndim - 1): if dim >= args.position: break output_data = scipy.swapaxes(output_data, dim, dim + 1) # set pixel spacing spacing = list(header.get_pixel_spacing(example_header)) spacing = tuple(spacing[:args.position] + [args.spacing] + spacing[args.position:]) # !TODO: Find a way to enable this also for PyDicom and ITK images if __is_header_nibabel(example_header): __update_header_from_array_nibabel(example_header, output_data) header.set_pixel_spacing(example_header, spacing) else: raise ArgumentError( "Sorry. Setting the voxel spacing of the new dimension only works with NIfTI images. See the description of this program for more details." ) # save created volume save(output_data, args.output, example_header, args.force) logger.info("Successfully terminated.")