comparison imagej2_find_maxima_jython_script.py @ 1:29a4d422f32a draft

"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
author imgteam
date Mon, 28 Sep 2020 16:41:15 +0000
parents f1ba33cd9edf
children da29019170f1
comparison
equal deleted inserted replaced
0:f1ba33cd9edf 1:29a4d422f32a
1 import sys 1 import sys
2 import jython_utils 2
3 from ij import ImagePlus, IJ 3 from ij import IJ, ImagePlus
4 from ij.plugin.filter import Analyzer, MaximumFinder 4 from ij.plugin.filter import Analyzer, MaximumFinder
5 from ij.process import ImageProcessor 5 from ij.process import ImageProcessor
6 from jarray import array
7 6
8 # Fiji Jython interpreter implements Python 2.5 which does not 7 # Fiji Jython interpreter implements Python 2.5 which does not
9 # provide support for argparse. 8 # provide support for argparse.
10 error_log = sys.argv[ -10 ] 9 error_log = sys.argv[-10]
11 input = sys.argv[ -9 ] 10 input_file = sys.argv[-9]
12 scale_when_converting = jython_utils.asbool( sys.argv[ -8 ] ) 11 scale_when_converting = sys.argv[-8] == 'yes'
13 weighted_rgb_conversions = jython_utils.asbool( sys.argv[ -7 ] ) 12 weighted_rgb_conversions = sys.argv[-7] == 'yes'
14 noise_tolerance = int( sys.argv[ -6 ] ) 13 noise_tolerance = int(sys.argv[-6])
15 output_type = sys.argv[ -5 ] 14 output_type = sys.argv[-5]
16 exclude_edge_maxima = jython_utils.asbool( sys.argv[ -4 ] ) 15 exclude_edge_maxima = sys.argv[-4] == 'yes'
17 light_background = jython_utils.asbool( sys.argv[ -3 ] ) 16 light_background = sys.argv[-3]
18 tmp_output_path = sys.argv[ -2 ] 17 tmp_output_path = sys.argv[-2]
19 output_datatype = sys.argv[ -1 ] 18 output_datatype = sys.argv[-1]
20 19
21 # Open the input image file. 20 # Open the input image file.
22 input_image_plus = IJ.openImage( input ) 21 input_image_plus = IJ.openImage(input_file)
23 22
24 # Create a copy of the image. 23 # Create a copy of the image.
25 input_image_plus_copy = input_image_plus.duplicate() 24 input_image_plus_copy = input_image_plus.duplicate()
26 image_processor_copy = input_image_plus_copy.getProcessor() 25 image_processor_copy = input_image_plus_copy.getProcessor()
27 bit_depth = image_processor_copy.getBitDepth() 26 bit_depth = image_processor_copy.getBitDepth()
28 analyzer = Analyzer( input_image_plus_copy ) 27 analyzer = Analyzer(input_image_plus_copy)
29 28
30 try: 29 # Set the conversion options.
31 # Set the conversion options. 30 options = []
32 options = [] 31 # The following 2 options are applicable only to RGB images.
33 # The following 2 options are applicable only to RGB images. 32 if bit_depth == 24:
34 if bit_depth == 24: 33 if scale_when_converting:
35 if scale_when_converting: 34 options.append("scale")
36 option.append( "scale" ) 35 if weighted_rgb_conversions:
37 if weighted_rgb_conversions: 36 options.append("weighted")
38 options.append( "weighted" ) 37 # Perform conversion - must happen even if no options are set.
39 # Perform conversion - must happen even if no options are set. 38 IJ.run(input_image_plus_copy, "Conversions...", "%s" % " ".join(options))
40 IJ.run( input_image_plus_copy, "Conversions...", "%s" % " ".join( options ) ) 39 if output_type in ['List', 'Count']:
41 if output_type in [ 'List', 'Count' ]: 40 # W're generating a tabular file for the output.
42 # W're generating a tabular file for the output. 41 # Set the Find Maxima options.
43 # Set the Find Maxima options. 42 options = ['noise=%d' % noise_tolerance]
44 options = [ 'noise=%d' % noise_tolerance ] 43 if output_type.find('_') > 0:
45 if output_type.find( '_' ) > 0: 44 output_type_str = 'output=[%s]' % output_type.replace('_', ' ')
46 output_type_str = 'output=[%s]' % output_type.replace( '_', ' ' )
47 else:
48 output_type_str = 'output=%s' % output_type
49 options.append( output_type_str )
50 if exclude_edge_maxima:
51 options.append( 'exclude' )
52 if light_background:
53 options.append( 'light' )
54 # Run the command.
55 IJ.run( input_image_plus_copy, "Find Maxima...", "%s" % " ".join( options ) )
56 results_table = analyzer.getResultsTable()
57 results_table.saveAs( tmp_output_path )
58 else: 45 else:
59 # Find the maxima of an image (does not find minima). 46 output_type_str = 'output=%s' % output_type
60 # LIMITATIONS: With output_type=Segmented_Particles 47 options.append(output_type_str)
61 # (watershed segmentation), some segmentation lines 48 if exclude_edge_maxima:
62 # may be improperly placed if local maxima are suppressed 49 options.append('exclude')
63 # by the tolerance. 50 if light_background:
64 mf = MaximumFinder() 51 options.append('light')
65 if output_type == 'Single_Points': 52 # Run the command.
66 output_type_param = mf.SINGLE_POINTS 53 IJ.run(input_image_plus_copy, "Find Maxima...", "%s" % " ".join(options))
67 elif output_type == 'Maxima_Within_Tolerance': 54 results_table = analyzer.getResultsTable()
68 output_type_param = mf.IN_TOLERANCE 55 results_table.saveAs(tmp_output_path)
69 elif output_type == 'Segmented_Particles': 56 else:
70 output_type_param = mf.SEGMENTED 57 # Find the maxima of an image (does not find minima).
71 elif output_type == 'List': 58 # LIMITATIONS: With output_type=Segmented_Particles
72 output_type_param = mf.LIST 59 # (watershed segmentation), some segmentation lines
73 elif output_type == 'Count': 60 # may be improperly placed if local maxima are suppressed
74 output_type_param = mf.COUNT 61 # by the tolerance.
75 # Get a new byteProcessor with a normal (uninverted) LUT where 62 mf = MaximumFinder()
76 # the marked points are set to 255 (Background 0). Pixels outside 63 if output_type == 'Single_Points':
77 # of the roi of the input image_processor_copy are not set. No 64 output_type_param = mf.SINGLE_POINTS
78 # output image is created for output types POINT_SELECTION, LIST 65 elif output_type == 'Maxima_Within_Tolerance':
79 # and COUNT. In these cases findMaxima returns null. 66 output_type_param = mf.IN_TOLERANCE
80 byte_processor = mf.findMaxima( image_processor_copy, 67 elif output_type == 'Segmented_Particles':
81 noise_tolerance, 68 output_type_param = mf.SEGMENTED
82 ImageProcessor.NO_THRESHOLD, 69 elif output_type == 'List':
83 output_type_param, 70 output_type_param = mf.LIST
84 exclude_edge_maxima, 71 elif output_type == 'Count':
85 False ) 72 output_type_param = mf.COUNT
86 # Invert the image or ROI. 73 # Get a new byteProcessor with a normal (uninverted) LUT where
87 byte_processor.invert() 74 # the marked points are set to 255 (Background 0). Pixels outside
88 if output_type == 'Segmented_Particles' and not light_background: 75 # of the roi of the input image_processor_copy are not set. No
89 # Invert the values in this image's LUT (indexed color model). 76 # output image is created for output types POINT_SELECTION, LIST
90 byte_processor.invertLut() 77 # and COUNT. In these cases findMaxima returns null.
91 image_plus = ImagePlus( "output", byte_processor ) 78 byte_processor = mf.findMaxima(image_processor_copy,
92 IJ.saveAs( image_plus, output_datatype, tmp_output_path ) 79 noise_tolerance,
93 except Exception, e: 80 ImageProcessor.NO_THRESHOLD,
94 jython_utils.handle_error( error_log, str( e ) ) 81 output_type_param,
82 exclude_edge_maxima,
83 False)
84 # Invert the image or ROI.
85 byte_processor.invert()
86 if output_type == 'Segmented_Particles' and not light_background:
87 # Invert the values in this image's LUT (indexed color model).
88 byte_processor.invertLut()
89 image_plus = ImagePlus("output", byte_processor)
90 IJ.saveAs(image_plus, output_datatype, tmp_output_path)