Skip to content, Skip to search

Changes

SNT: Step-By-Step Instructions

2,007 bytes added, 18:17, 10 June 2019
Multiple Images
<source lang="python">
# @ImageJ ij
# @String(value="<HTML>This script applies the Frangi Vesselness filter to all TIFF images in a directory.<br>Only 2D or 3D grayscale images are supported. Processing log is shown in Console.", visibility="MESSAGE") msg# @File(label="Directory containing your images", style="directory") image_dirinput_dir# @String(label="Consider only filenames containing",description="Clear field for no filtering",value="") name_filter# @boolean(label="Include subdirectories") recursive# @float(label="Size of structures to be filtered (spatially calibrated units)",description="The filter kernel will be determined from the average voxel size") scale# @LogService log# @StatusService status# @UIService uiservice """file: Filter_Multiple_Images.pyauthor: Tiago Ferreira, Cameron Arshadiinfo: Bulk filtering of image files using Frangi Vesselness""" from net.imagej.axis import Axes 
import os
def run(image_directory):
if image_directory is not None:
# Get all files in the chosen directory
image_directory = str(image_directory)
file_list = os.listdir(image_directory)
extension = ".tif"
def get_image_files(directory, filtering_string, extension): """Returns a list containing the paths of files in the specified directory. The list will only include files with the supplied extension whose filename contains the specified string.""" files = [] for (dirpath, dirnames, filenames) in os.walk(directory): for filename f in file_listfilenames: if os.path.basename(f).startswith('.'): continue if filenamefiltering_string in f and f.lower().endswith(extension): files.append(os.path.join(dirpath, f)) if not recursive: break # Only do not process subdirectories  return files  def run(): # First check that scale parameter is > 0, exiting if not if scale <= 0: log.error('Please select a value > 0 for the scale parameter. Exiting...') return  # Get all files with specified name filter and extension in the input directory d = str(input_dir) extension = ".tif" files= get_image_files(d, name_filter, extension) print if not files or len(files) == 0: uiservice.showDialog("No files matched the specified criteria", "Error") return  processed = 0 skipped = 0 for f in files:  basename = os.path.basename(f) msg = 'Processing file %s: %s...' % (processed + skipped + str1, basename) status.showStatus(filenamemsg) log.info(msg) # Load the input image image_path = image_directory + '/' + filename input_image = ij.io().open(image_pathf) # Verify that the image is 2D/3D and grayscale, skipping it if not num_dimensions = input_image.numDimensions() if num_dimensions > 3 or int(input_image.getChannels()) > 1: log.error('Could not process %s...Only 2D/3D grayscale images are supported' % basename) skipped += 1 continue  # Convert input image to float, since we are dealing with derivatives converted_input float_input = ij.op().run("convert.float32", input_image)  # Obtain spatial calibration of the image x_spacing = float_input.averageScale(0) y_spacing = float_input.averageScale(1) spacing = [x_spacing, y_spacing]
# Obtain spatial calibration of the image x_spacing if num_dimensions == converted_input3 and float_input.averageScaleaxis(02) y_spacing = converted_input.averageScaletype(1)== Axes.Z: z_spacing = converted_inputfloat_input.averageScale(2) spacing.append(z_spacing)
# Create placeholder image for the output then run the Frangi Vesselness parametersop spacing output = [x_spacing, y_spacingij.op().run("create.img", z_spacing]float_input) pixel_scale = scale = 2 / (sum(spacing) / len(spacing)) # Twice the x-average voxel separationsize: convert scale to pixel units ij.op().run("frangiVesselness", output, float_input, spacing, pixel_scale)
# Create placeholder Save the result using the same basename as the image then run , adding "[Frangi].tif" # For example, the output for "OP_1.tif" would be named "OP_1[Frangi op].tif" output l = ij.oplen(f).run el = len(extension) output_filepath = f[0:l - el] + "create[Frangi].imgtif", converted_input) ij.opio().runsave("frangiVesselness", output, converted_input, spacing, scaleoutput_filepath)
# Save the result using the same base name as the image, adding "_Frangi.tif" # For example, the output for "OP_1.tif" would be named "OP_1_Frangi.tif" l = len(filename) el = len(extension) basename processed += filename[0:l - el] output_filepath = image_directory + '/' + basename + "_Frangi.tif" ij.io().save(output, output_filepath)1
print log.info('Done. %s file(s) processed. %s file(s) skipped...'% (processed, skipped))
else:
print('No image directory was selected, exiting...')
run()
run(image_dir)
</source>
TBD:
758
edits