annotate imagej2_adjust_threshold_binary_jython_script.py @ 3:f72f8e6787cd 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:01:42 +0000
parents 55bf135d6b0c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
301f24849032 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
1 import sys
1
39b2bc251b2f "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
2
3
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
3 from ij import IJ, Prefs
0
301f24849032 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
4
301f24849032 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
5 # Fiji Jython interpreter implements Python 2.5 which does not
301f24849032 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
6 # provide support for argparse.
3
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
7 input_file = sys.argv[-8]
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
8 threshold_min = float(sys.argv[-7])
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
9 threshold_max = float(sys.argv[-6])
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
10 method = sys.argv[-5]
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
11 display = sys.argv[-4]
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
12 black_background = sys.argv[-3] == "yes"
1
39b2bc251b2f "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
13 output_filename = sys.argv[-2]
39b2bc251b2f "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
14 output_datatype = sys.argv[-1]
0
301f24849032 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
15
301f24849032 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
16 # Open the input image file.
1
39b2bc251b2f "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
17 input_image_plus = IJ.openImage(input_file)
0
301f24849032 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
18
301f24849032 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
19 # Create a copy of the image.
301f24849032 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
20 input_image_plus_copy = input_image_plus.duplicate()
3
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
21
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
22 bit_depth = input_image_plus_copy.getBitDepth()
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
23
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
24 if black_background:
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
25 Prefs.blackBackground = True
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
26 else:
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
27 Prefs.blackBackground = False
0
301f24849032 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
28
3
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
29 if method != "Manual":
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
30 # Set the options.
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
31 if black_background:
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
32 method_str = "%s dark" % method
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
33 suffix = "black"
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
34 else:
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
35 method_str = method
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
36 suffix = ""
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
37 threshold_min = 1
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
38 # Set threshold_max based on image bit-depth
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
39 # For 8-bit images, use 255; for 16-bit images, use 65535
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
40 if bit_depth == 8:
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
41 threshold_max = 255 # Default for 8-bit images
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
42 elif bit_depth == 16:
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
43 threshold_max = 65535 # Default for 16-bit images
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
44 else:
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
45 threshold_max = float('inf') # General fallback if bit depth is unknown
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
46
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
47 IJ.setAutoThreshold(input_image_plus_copy, method_str)
f72f8e6787cd planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 8f49f3c66b5a1de99ec15e65c2519a56792f1d56
imgteam
parents: 2
diff changeset
48 IJ.run(input_image_plus_copy, "Convert to Mask", "calculate %s" % suffix)
1
39b2bc251b2f "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
49 if display == "red":
39b2bc251b2f "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
50 display_mode = "Red"
39b2bc251b2f "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
51 elif display == "bw":
39b2bc251b2f "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
52 display_mode = "Black & White"
39b2bc251b2f "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
53 elif display == "over_under":
39b2bc251b2f "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
54 display_mode = "Over/Under"
39b2bc251b2f "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
55 IJ.setThreshold(input_image_plus_copy, threshold_min, threshold_max, display_mode)
39b2bc251b2f "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
56 # Save the ImagePlus object as a new image.
39b2bc251b2f "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
57 IJ.saveAs(input_image_plus_copy, output_datatype, output_filename)