comparison imagej2_analyze_particles_binary_jython_script.py @ 3:4409f9eb9433 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 15:58:30 +0000
parents 6a07f71806bb
children
comparison
equal deleted inserted replaced
2:6a07f71806bb 3:4409f9eb9433
1 import sys 1 import sys
2 2
3 from ij import IJ 3 from ij import IJ, Prefs
4 from ij.plugin.filter import Analyzer 4 from ij.plugin.filter import Analyzer
5 5
6 OPTIONS = ["edm=Overwrite", "iterations=1", "count=1"] 6 OPTIONS = ["edm=Overwrite", "iterations=1", "count=1"]
7 7
8 # Fiji Jython interpreter implements Python 2.5 which does not 8 # Fiji Jython interpreter implements Python 2.5 which does not
9 # provide support for argparse. 9 # provide support for argparse.
10 error_log = sys.argv[-14] 10 roi_coordinate_file = sys.argv[-13]
11 input_file = sys.argv[-13] 11 input_file = sys.argv[-12]
12 black_background = sys.argv[-12] == "yes" 12 black_background = sys.argv[-11] == "yes"
13 size = sys.argv[-11] 13 size = sys.argv[-10]
14 circularity_min = float(sys.argv[-10]) 14 circularity_min = float(sys.argv[-9])
15 circularity_max = float(sys.argv[-9]) 15 circularity_max = float(sys.argv[-8])
16 show = sys.argv[-8] 16 show = sys.argv[-7]
17 display_results = sys.argv[-7] == "yes" 17 display_results = sys.argv[-6] == "yes"
18 all_results = sys.argv[-6] == "yes"
19 exclude_edges = sys.argv[-5] == "yes" 18 exclude_edges = sys.argv[-5] == "yes"
20 include_holes = sys.argv[-4] == "yes" 19 include_holes = sys.argv[-4] == "yes"
21 output_filename = sys.argv[-3] 20 output_filename = sys.argv[-3]
22 output_datatype = sys.argv[-2] 21 output_datatype = sys.argv[-2]
23 results_path = sys.argv[-1] 22 results_path = sys.argv[-1]
23
24
25 if black_background:
26 Prefs.blackBackground = True
27 else:
28 Prefs.blackBackground = False
24 29
25 # Open the input image file. 30 # Open the input image file.
26 input_image_plus = IJ.openImage(input_file) 31 input_image_plus = IJ.openImage(input_file)
27 32
28 # Create a copy of the image. 33 # Create a copy of the image.
40 if not image_processor_copy.isBinary(): 45 if not image_processor_copy.isBinary():
41 # Convert the image to binary grayscale. 46 # Convert the image to binary grayscale.
42 IJ.run(input_image_plus_copy, "Make Binary", "") 47 IJ.run(input_image_plus_copy, "Make Binary", "")
43 48
44 # Set the options. 49 # Set the options.
45 options = ["size=%s" % size] 50 options = ["size=%s" % size, "stack"]
46 circularity_str = "%.3f-%.3f" % (circularity_min, circularity_max) 51 circularity_str = "%.3f-%.3f" % (circularity_min, circularity_max)
47 options.append("circularity=%s" % circularity_str) 52 options.append("circularity=%s" % circularity_str)
53 if exclude_edges:
54 options.append("exclude")
55 if include_holes:
56 options.append("include")
57
58 # If you need the coordinates of ROIs we compute it twice
59 if len(roi_coordinate_file) > 0:
60 options2 = list(options)
61 options2.append("show=Overlay")
62 IJ.run(input_image_plus_copy, "Analyze Particles...", " ".join(options2))
63 ov = input_image_plus_copy.getOverlay()
64 with open(roi_coordinate_file, 'w') as fo:
65 fo.write("shape\tpoints\tlabel\tt\tz\n")
66 for i, roi in enumerate(ov):
67 if roi.getName() is None:
68 roi.name = "ROI_%d" % i
69 poly = roi.getPolygon()
70 x_values = poly.xpoints
71 y_values = poly.ypoints
72 points_coo = ",".join(["(%d,%d)" % (x, y) for x, y in zip(x_values, y_values)])
73 fo.write("Polygon\t%s\t%s\t%d\t%d\n" % (points_coo, roi.getName(), roi.getTPosition(), roi.getZPosition()))
74 analyzer.resetCounter()
75
48 if show.find("_") >= 0: 76 if show.find("_") >= 0:
49 show_str = "[%s]" % show.replace("_", " ") 77 show_str = "[%s]" % show.replace("_", " ")
50 else: 78 else:
51 show_str = show 79 show_str = show
52 options.append("show=%s" % show_str) 80 options.append("show=%s" % show_str)
53 if display_results: 81 if display_results:
54 options.append("display") 82 options.append("display")
55 if not all_results:
56 options.append("summarize")
57 if exclude_edges:
58 options.append("exclude")
59 if include_holes:
60 options.append("include")
61 # Always run "in_situ". 83 # Always run "in_situ".
62 options.append("in_situ") 84 options.append("in_situ")
63 85
64 # Run the command. 86 # Run the command.
65 IJ.run(input_image_plus_copy, "Analyze Particles...", " ".join(options)) 87 IJ.run(input_image_plus_copy, "Analyze Particles...", " ".join(options))
66 88
67 # Save outputs. 89 # Save outputs.
68 if len(output_filename) > 0: 90 if len(output_filename) > 0:
69 # Save the ImagePlus object as a new image. 91 # Save the ImagePlus object as a new image.
70 IJ.saveAs(input_image_plus_copy, output_datatype, output_filename) 92 IJ.saveAs(input_image_plus_copy, output_datatype, output_filename)
71 if display_results and len(results_path) > 0: 93 if len(results_path) > 0:
72 results_table = analyzer.getResultsTable() 94 results_table = analyzer.getResultsTable()
73 results_table.saveAs(results_path) 95 results_table.saveAs(results_path)