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) logger.info("Selected viscous type is {}".format(args.type)) # iterate over input images for image in args.images: # get and prepare image data logger.info("Loading image {} using NiBabel...".format(image)) image_gradient = load(image) # get and prepare image data image_gradient_data = scipy.squeeze(image_gradient.get_data()) logger.debug( "Intensity range of gradient image is ({}, {})".format(image_gradient_data.min(), image_gradient_data.max()) ) # build output file name and check for its existence, if not in sections mode if "sections" != args.type: # build output file name image_viscous_name = ( args.folder + "/" + image.split("/")[-1][:-4] + "_viscous_{}_sec_{}_ds_{}".format(args.type, args.sections, args.dsize) ) image_viscous_name += image.split("/")[-1][-4:] # check if output file exists if not args.force: if os.path.exists(image_viscous_name): logger.warning("The output file {} already exists. Skipping this image.".format(image_viscous_name)) continue # execute plain closing i.e. a closing operation over the whole image, if in plain mode if "plain" == args.type: # prepare the disc structure (a ball with a diameter of (args.dsize * 2 + 1)) disc = iterate_structure(generate_binary_structure(3, 1), args.dsize).astype(scipy.int_) # apply closing logger.info("Applying the morphology over whole image at once...") image_viscous_data = grey_closing(image_gradient_data, footprint=disc) # save resulting gradient image logger.info("Saving resulting gradient image as {}...".format(image_viscous_name)) image_viscous = image_like(image_viscous_data, image_gradient) save(image_viscous, image_viscous_name) # skip other morphologies continue # create gradient images flattened histogram bins = hist_flatened(image_gradient_data, args.sections) logger.debug("{} bins created".format(len(bins) - 1)) # check if the number of bins is consistent if args.sections != len(bins) - 1: raise Exception( "Inconsistency between the number of requested and created bins ({} to {})".format( args.sections, len(bins) - 1 ) ) # prepare result file image_viscous_data = image_gradient_data # transform the gradient images topography (Note: the content of one bin is: bins[slice - 1] <= content < bins[slice] logger.info("Applying the viscous morphological operations {} times...".format(args.sections)) for slice in range(1, args.sections + 1): # build output file name and check for its existence, if in sections mode if "sections" == args.type: # build output file name image_viscous_name = ( args.folder + "/" + image.split("/")[-1][:-4] + "_viscous_{}_sec_{}_ds_{}_sl_{}".format(args.type, args.sections, args.dsize, slice) ) image_viscous_name += image.split("/")[-1][-4:] # check if output file exists if not args.force: if os.path.exists(image_viscous_name): logger.warning( "The output file {} already exists. Skipping this slice.".format(image_viscous_name) ) continue # prepare result file image_viscous_data = image_gradient_data # create masks to extract the affected voxels (i.e. the current slice of the topographic image representation) mask_greater = image_gradient_data >= bins[slice] # all voxels with are over the current slice mask_lower = image_gradient_data < bins[slice - 1] # all voxels which are under the current slice mask_equal = scipy.invert(mask_greater | mask_lower) # all voxels in the current slice if "mercury" == args.type: dsize = int((args.dsize / float(args.sections)) * (slice)) disc = iterate_structure(generate_binary_structure(3, 1), dsize).astype(scipy.int_) mask_equal_or_greater = mask_equal | mask_greater image_threshold_data = image_gradient_data * mask_equal_or_greater elif "oil" == args.type: dsize = int((args.dsize / float(args.sections)) * (args.sections - slice + 1)) disc = iterate_structure(generate_binary_structure(3, 1), dsize).astype(scipy.int_) image_threshold_data = image_gradient_data.copy() mask_equal_or_lower = mask_equal | mask_lower # set all voxels over the current slice to the max of all voxels in the current slice image_threshold_data[mask_greater] = image_threshold_data[mask_equal_or_lower].max() elif "sections" == args.type: dsize = args.dsize disc = iterate_structure(generate_binary_structure(3, 1), args.dsize).astype(scipy.int_) image_threshold_data = image_gradient_data.copy() # set all voxels under the current slice to zero image_threshold_data[mask_lower] = 0 # set all voxels over the current slice to the max of all voxels in the current slice image_threshold_data[mask_greater] = image_threshold_data[mask_equal].max() logger.debug( "{} of {} voxels belong to this level.".format( len(mask_equal.nonzero()[0]), scipy.prod(image_threshold_data.shape) ) ) # apply the closing with the appropriate disc size logger.debug( "Applying a disk of {} to all values >= {} and < {}...".format(dsize, bins[slice - 1], bins[slice]) ) image_closed_data = grey_closing(image_threshold_data, footprint=disc) # add result of this slice to the general results image_viscous_data = scipy.maximum(image_viscous_data, image_closed_data) # save created output file, if in sections mode if "sections" == args.type: # save resulting gradient image logger.info("Saving resulting gradient image as {}...".format(image_viscous_name)) image_viscous = image_like(image_viscous_data, image_gradient) save(image_viscous, image_viscous_name) # save created output file, if not in sections mode if "sections" != args.type: # save resulting gradient image logger.info("Saving resulting gradient image as {}...".format(image_viscous_name)) image_viscous = image_like(image_viscous_data, image_gradient) save(image_viscous, image_viscous_name) 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) logger.info('Selected viscous type is {}'.format(args.type)) # iterate over input images for image in args.images: # get and prepare image data logger.info('Loading image {} using NiBabel...'.format(image)) image_gradient = load(image) # get and prepare image data image_gradient_data = scipy.squeeze(image_gradient.get_data()) logger.debug('Intensity range of gradient image is ({}, {})'.format( image_gradient_data.min(), image_gradient_data.max())) # build output file name and check for its existence, if not in sections mode if 'sections' != args.type: # build output file name image_viscous_name = args.folder + '/' + image.split( '/')[-1][:-4] + '_viscous_{}_sec_{}_ds_{}'.format( args.type, args.sections, args.dsize) image_viscous_name += image.split('/')[-1][-4:] # check if output file exists if not args.force: if os.path.exists(image_viscous_name): logger.warning( 'The output file {} already exists. Skipping this image.' .format(image_viscous_name)) continue # execute plain closing i.e. a closing operation over the whole image, if in plain mode if 'plain' == args.type: # prepare the disc structure (a ball with a diameter of (args.dsize * 2 + 1)) disc = iterate_structure(generate_binary_structure(3, 1), args.dsize).astype(scipy.int_) # apply closing logger.info('Applying the morphology over whole image at once...') image_viscous_data = grey_closing(image_gradient_data, footprint=disc) # save resulting gradient image logger.info('Saving resulting gradient image as {}...'.format( image_viscous_name)) image_viscous = image_like(image_viscous_data, image_gradient) save(image_viscous, image_viscous_name) # skip other morphologies continue # create gradient images flattened histogram bins = hist_flatened(image_gradient_data, args.sections) logger.debug('{} bins created'.format(len(bins) - 1)) # check if the number of bins is consistent if args.sections != len(bins) - 1: raise Exception( 'Inconsistency between the number of requested and created bins ({} to {})' .format(args.sections, len(bins) - 1)) # prepare result file image_viscous_data = image_gradient_data # transform the gradient images topography (Note: the content of one bin is: bins[slice - 1] <= content < bins[slice] logger.info( 'Applying the viscous morphological operations {} times...'.format( args.sections)) for slice in range(1, args.sections + 1): # build output file name and check for its existence, if in sections mode if 'sections' == args.type: # build output file name image_viscous_name = args.folder + '/' + image.split( '/')[-1][:-4] + '_viscous_{}_sec_{}_ds_{}_sl_{}'.format( args.type, args.sections, args.dsize, slice) image_viscous_name += image.split('/')[-1][-4:] # check if output file exists if not args.force: if os.path.exists(image_viscous_name): logger.warning( 'The output file {} already exists. Skipping this slice.' .format(image_viscous_name)) continue # prepare result file image_viscous_data = image_gradient_data # create masks to extract the affected voxels (i.e. the current slice of the topographic image representation) mask_greater = (image_gradient_data >= bins[slice] ) # all voxels with are over the current slice mask_lower = (image_gradient_data < bins[slice - 1] ) # all voxels which are under the current slice mask_equal = scipy.invert( mask_greater | mask_lower) # all voxels in the current slice if 'mercury' == args.type: dsize = int((args.dsize / float(args.sections)) * (slice)) disc = iterate_structure(generate_binary_structure(3, 1), dsize).astype(scipy.int_) mask_equal_or_greater = mask_equal | mask_greater image_threshold_data = image_gradient_data * mask_equal_or_greater elif 'oil' == args.type: dsize = int((args.dsize / float(args.sections)) * (args.sections - slice + 1)) disc = iterate_structure(generate_binary_structure(3, 1), dsize).astype(scipy.int_) image_threshold_data = image_gradient_data.copy() mask_equal_or_lower = mask_equal | mask_lower # set all voxels over the current slice to the max of all voxels in the current slice image_threshold_data[mask_greater] = image_threshold_data[ mask_equal_or_lower].max() elif 'sections' == args.type: dsize = args.dsize disc = iterate_structure(generate_binary_structure(3, 1), args.dsize).astype(scipy.int_) image_threshold_data = image_gradient_data.copy() # set all voxels under the current slice to zero image_threshold_data[mask_lower] = 0 # set all voxels over the current slice to the max of all voxels in the current slice image_threshold_data[mask_greater] = image_threshold_data[ mask_equal].max() logger.debug('{} of {} voxels belong to this level.'.format( len(mask_equal.nonzero()[0]), scipy.prod(image_threshold_data.shape))) # apply the closing with the appropriate disc size logger.debug( 'Applying a disk of {} to all values >= {} and < {}...'.format( dsize, bins[slice - 1], bins[slice])) image_closed_data = grey_closing(image_threshold_data, footprint=disc) # add result of this slice to the general results image_viscous_data = scipy.maximum(image_viscous_data, image_closed_data) # save created output file, if in sections mode if 'sections' == args.type: # save resulting gradient image logger.info('Saving resulting gradient image as {}...'.format( image_viscous_name)) image_viscous = image_like(image_viscous_data, image_gradient) save(image_viscous, image_viscous_name) # save created output file, if not in sections mode if 'sections' != args.type: # save resulting gradient image logger.info('Saving resulting gradient image as {}...'.format( image_viscous_name)) image_viscous = image_like(image_viscous_data, image_gradient) save(image_viscous, image_viscous_name) 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) logger.info( 'Executing weighted viscous morphology with {} ({} bins).'.format( ','.join(map(str, args.func)), len(args.func))) # iterate over input images for image in args.images: # build output file name image_viscous_name = args.folder + '/' + image.split( '/')[-1][:-4] + '_wviscous_' + '_'.join(map(str, args.func)) image_viscous_name += image.split('/')[-1][-4:] # check if output file exists if not args.force: if os.path.exists(image_viscous_name): logger.warning( 'The output file {} already exists. Skipping this image.'. format(image_viscous_name)) continue # get and prepare image data logger.info('Loading image {} using NiBabel...'.format(image)) image_gradient = load(image) # get and prepare image data image_gradient_data = scipy.squeeze(image_gradient.get_data()) # prepare result image and extract required attributes of input image if args.debug: logger.debug( 'Intensity range of gradient image is ({}, {})'.format( image_gradient_data.min(), image_gradient_data.max())) # create gradient images flattened histogram bins = hist_flatened(image_gradient_data, len(args.func)) logger.debug('{} bins created'.format(len(bins) - 1)) # check if the number of bins is consistent if len(args.func) != len(bins) - 1: raise Exception( 'Inconsistency between the number of requested and created bins ({} to {})' .format(args.sections, len(bins) - 1)) # prepare result file image_viscous_data = image_gradient_data # transform the gradient images topography logger.info( 'Applying the viscous morphological operations on {} sections...'. format(len(args.func))) for sl in range(1, len(args.func) + 1): # create sphere to use in this step if 0 >= args.func[sl - 1]: continue # sphere of sizes 0 or below lead to no changes and are not executed sphere = iterate_structure(generate_binary_structure(3, 1), args.func[sl - 1]).astype(scipy.int_) # create masks to extract the affected voxels (i.e. the current slice of the topographic image representation) mask_greater = (image_gradient_data >= bins[sl] ) # all voxels with are over the current slice mask_lower = (image_gradient_data < bins[sl - 1] ) # all voxels which are under the current slice mask_equal = scipy.invert( mask_greater | mask_lower) # all voxels in the current slice # extract slice image_threshold_data = image_gradient_data.copy() image_threshold_data[ mask_lower] = 0 # set all voxels under the current slice to zero image_threshold_data[mask_greater] = image_threshold_data[ mask_equal].max( ) # set all voxels over the current slice to the max of all voxels in the current slice logger.debug('{} of {} voxels belong to this level.'.format( len(mask_equal.nonzero()[0]), scipy.prod(image_threshold_data.shape))) # apply the closing with the appropriate sphere logger.debug( 'Applying a disk of {} to all values >= {} and < {} (sec {})...' .format(args.func[sl - 1], bins[sl - 1], bins[sl], sl)) image_closed_data = grey_closing(image_threshold_data, footprint=sphere) # add result of this slice to the general results image_viscous_data = scipy.maximum(image_viscous_data, image_closed_data) # save resulting gradient image logger.info('Saving resulting gradient image as {}...'.format( image_viscous_name)) image_viscous = image_like(image_viscous_data, image_gradient) save(image_viscous, image_viscous_name) 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) logger.info('Executing weighted viscous morphology with {} ({} bins).'.format(','.join(map(str, args.func)), len(args.func))) # iterate over input images for image in args.images: # build output file name image_viscous_name = args.folder + '/' + image.split('/')[-1][:-4] + '_wviscous_' + '_'.join(map(str, args.func)) image_viscous_name += image.split('/')[-1][-4:] # check if output file exists if not args.force: if os.path.exists(image_viscous_name): logger.warning('The output file {} already exists. Skipping this image.'.format(image_viscous_name)) continue # get and prepare image data logger.info('Loading image {} using NiBabel...'.format(image)) image_gradient = load(image) # get and prepare image data image_gradient_data = scipy.squeeze(image_gradient.get_data()) # prepare result image and extract required attributes of input image if args.debug: logger.debug('Intensity range of gradient image is ({}, {})'.format(image_gradient_data.min(), image_gradient_data.max())) # create gradient images flattened histogram bins = hist_flatened(image_gradient_data, len(args.func)) logger.debug('{} bins created'.format(len(bins) -1)) # check if the number of bins is consistent if len(args.func) != len(bins) - 1: raise Exception('Inconsistency between the number of requested and created bins ({} to {})'.format(args.sections, len(bins) - 1)) # prepare result file image_viscous_data = image_gradient_data # transform the gradient images topography logger.info('Applying the viscous morphological operations on {} sections...'.format(len(args.func))) for sl in range(1, len(args.func) + 1): # create sphere to use in this step if 0 >= args.func[sl - 1]: continue # sphere of sizes 0 or below lead to no changes and are not executed sphere = iterate_structure(generate_binary_structure(3, 1), args.func[sl - 1]).astype(scipy.int_) # create masks to extract the affected voxels (i.e. the current slice of the topographic image representation) mask_greater = (image_gradient_data >= bins[sl]) # all voxels with are over the current slice mask_lower = (image_gradient_data < bins[sl - 1]) # all voxels which are under the current slice mask_equal = scipy.invert(mask_greater | mask_lower) # all voxels in the current slice # extract slice image_threshold_data = image_gradient_data.copy() image_threshold_data[mask_lower] = 0 # set all voxels under the current slice to zero image_threshold_data[mask_greater] = image_threshold_data[mask_equal].max() # set all voxels over the current slice to the max of all voxels in the current slice logger.debug('{} of {} voxels belong to this level.'.format(len(mask_equal.nonzero()[0]), scipy.prod(image_threshold_data.shape))) # apply the closing with the appropriate sphere logger.debug('Applying a disk of {} to all values >= {} and < {} (sec {})...'.format(args.func[sl - 1], bins[sl - 1], bins[sl], sl)) image_closed_data = grey_closing(image_threshold_data, footprint=sphere) # add result of this slice to the general results image_viscous_data = scipy.maximum(image_viscous_data, image_closed_data) # save resulting gradient image logger.info('Saving resulting gradient image as {}...'.format(image_viscous_name)) image_viscous = image_like(image_viscous_data, image_gradient) save(image_viscous, image_viscous_name) logger.info('Successfully terminated.')