annotate imagej2_adjust_threshold_binary_jython_script.py @ 2:666af1279007 draft default tip

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 57a0433defa3cbc37ab34fbb0ebcfaeb680db8d5
author imgteam
date Sun, 05 Nov 2023 10:54:34 +0000
parents 1a7c29d5fc11
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dc3935a0165a "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
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
2
0
dc3935a0165a "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
3 from ij import IJ
dc3935a0165a "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
4
dc3935a0165a "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
dc3935a0165a "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.
1
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
7 error_log = sys.argv[-10]
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
8 input_file = sys.argv[-9]
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
9 threshold_min = float(sys.argv[-8])
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
10 threshold_max = float(sys.argv[-7])
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
11 method = sys.argv[-6]
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
12 display = sys.argv[-5]
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
13 black_background = sys.argv[-4] == "yes"
0
dc3935a0165a "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
14 # TODO: this is not being used.
1
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
15 stack_histogram = sys.argv[-3] == "yes"
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
16 output_filename = sys.argv[-2]
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
17 output_datatype = sys.argv[-1]
0
dc3935a0165a "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
18
dc3935a0165a "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
19 # Open the input image file.
1
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
20 input_image_plus = IJ.openImage(input_file)
0
dc3935a0165a "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
21
dc3935a0165a "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
22 # Create a copy of the image.
dc3935a0165a "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
23 input_image_plus_copy = input_image_plus.duplicate()
dc3935a0165a "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
24 image_processor_copy = input_image_plus_copy.getProcessor()
dc3935a0165a "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
25
1
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
26 # Convert image to binary if necessary.
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
27 if not image_processor_copy.isBinary():
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
28 # Convert the image to binary grayscale.
2
666af1279007 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 57a0433defa3cbc37ab34fbb0ebcfaeb680db8d5
imgteam
parents: 1
diff changeset
29 IJ.run(
666af1279007 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 57a0433defa3cbc37ab34fbb0ebcfaeb680db8d5
imgteam
parents: 1
diff changeset
30 input_image_plus_copy,
666af1279007 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 57a0433defa3cbc37ab34fbb0ebcfaeb680db8d5
imgteam
parents: 1
diff changeset
31 "Make Binary",
666af1279007 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 57a0433defa3cbc37ab34fbb0ebcfaeb680db8d5
imgteam
parents: 1
diff changeset
32 "iterations=1 count=1 edm=Overwrite do=Nothing",
666af1279007 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 57a0433defa3cbc37ab34fbb0ebcfaeb680db8d5
imgteam
parents: 1
diff changeset
33 )
1
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
34 # Set the options.
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
35 if black_background:
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
36 method_str = "%s dark" % method
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
37 else:
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
38 method_str = method
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
39 IJ.setAutoThreshold(input_image_plus_copy, method_str)
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
40 if display == "red":
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
41 display_mode = "Red"
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
42 elif display == "bw":
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
43 display_mode = "Black & White"
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
44 elif display == "over_under":
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
45 display_mode = "Over/Under"
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
46 IJ.setThreshold(input_image_plus_copy, threshold_min, threshold_max, display_mode)
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
47 # Run the command.
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
48 IJ.run(input_image_plus_copy, "threshold", "")
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
49 # Save the ImagePlus object as a new image.
1a7c29d5fc11 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents: 0
diff changeset
50 IJ.saveAs(input_image_plus_copy, output_datatype, output_filename)