comparison imagej2_find_maxima_jython_script.py @ 0:95fb6fa70c2f draft

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