Mercurial > repos > bgruening > imagej2_crop
comparison imagej2_analyze_particles_binary_jython_script.py @ 0:018144807556 draft default tip
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
| author | bgruening |
|---|---|
| date | Tue, 24 Sep 2024 17:12:52 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:018144807556 |
|---|---|
| 1 import sys | |
| 2 | |
| 3 from ij import IJ, Prefs | |
| 4 from ij.plugin.filter import Analyzer | |
| 5 | |
| 6 OPTIONS = ["edm=Overwrite", "iterations=1", "count=1"] | |
| 7 | |
| 8 # Fiji Jython interpreter implements Python 2.5 which does not | |
| 9 # provide support for argparse. | |
| 10 roi_coordinate_file = sys.argv[-13] | |
| 11 input_file = sys.argv[-12] | |
| 12 black_background = sys.argv[-11] == "yes" | |
| 13 size = sys.argv[-10] | |
| 14 circularity_min = float(sys.argv[-9]) | |
| 15 circularity_max = float(sys.argv[-8]) | |
| 16 show = sys.argv[-7] | |
| 17 display_results = sys.argv[-6] == "yes" | |
| 18 exclude_edges = sys.argv[-5] == "yes" | |
| 19 include_holes = sys.argv[-4] == "yes" | |
| 20 output_filename = sys.argv[-3] | |
| 21 output_datatype = sys.argv[-2] | |
| 22 results_path = sys.argv[-1] | |
| 23 | |
| 24 | |
| 25 if black_background: | |
| 26 Prefs.blackBackground = True | |
| 27 else: | |
| 28 Prefs.blackBackground = False | |
| 29 | |
| 30 # Open the input image file. | |
| 31 input_image_plus = IJ.openImage(input_file) | |
| 32 | |
| 33 # Create a copy of the image. | |
| 34 input_image_plus_copy = input_image_plus.duplicate() | |
| 35 image_processor_copy = input_image_plus_copy.getProcessor() | |
| 36 analyzer = Analyzer(input_image_plus_copy) | |
| 37 | |
| 38 # Set binary options. | |
| 39 options_list = OPTIONS | |
| 40 if black_background: | |
| 41 options_list.append("black") | |
| 42 options = " ".join(options_list) | |
| 43 IJ.run(input_image_plus_copy, "Options...", options) | |
| 44 | |
| 45 if not image_processor_copy.isBinary(): | |
| 46 # Convert the image to binary grayscale. | |
| 47 IJ.run(input_image_plus_copy, "Make Binary", "") | |
| 48 | |
| 49 # Set the options. | |
| 50 options = ["size=%s" % size, "stack"] | |
| 51 circularity_str = "%.3f-%.3f" % (circularity_min, circularity_max) | |
| 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 | |
| 76 if show.find("_") >= 0: | |
| 77 show_str = "[%s]" % show.replace("_", " ") | |
| 78 else: | |
| 79 show_str = show | |
| 80 options.append("show=%s" % show_str) | |
| 81 if display_results: | |
| 82 options.append("display") | |
| 83 # Always run "in_situ". | |
| 84 options.append("in_situ") | |
| 85 | |
| 86 # Run the command. | |
| 87 IJ.run(input_image_plus_copy, "Analyze Particles...", " ".join(options)) | |
| 88 | |
| 89 # Save outputs. | |
| 90 if len(output_filename) > 0: | |
| 91 # Save the ImagePlus object as a new image. | |
| 92 IJ.saveAs(input_image_plus_copy, output_datatype, output_filename) | |
| 93 if len(results_path) > 0: | |
| 94 results_table = analyzer.getResultsTable() | |
| 95 results_table.saveAs(results_path) |
