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