comparison imagej2_noise_jython_script.py @ 3:5f5b7185bc16 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:14:34 +0000
parents 49b5288dcd8c
children
comparison
equal deleted inserted replaced
2:49b5288dcd8c 3:5f5b7185bc16
1 import sys 1 import sys
2 2
3 from ij import IJ 3 from ij import IJ, ImagePlus
4 4
5 # Fiji Jython interpreter implements Python 2.5 which does not 5 # Fiji Jython interpreter implements Python 2.5 which does not
6 # provide support for argparse. 6 # provide support for argparse.
7 error_log = sys.argv[-19] 7 input_file = sys.argv[-8]
8 input_file = sys.argv[-18] 8 image_datatype = sys.argv[-7]
9 image_datatype = sys.argv[-17] 9 noise = sys.argv[-6]
10 noise = sys.argv[-16] 10 standard_deviation = sys.argv[-5]
11 standard_deviation = sys.argv[-15] 11 radius = sys.argv[-4]
12 radius = sys.argv[-14] 12 threshold = sys.argv[-3]
13 threshold = sys.argv[-13] 13 which_outliers = sys.argv[-2]
14 which_outliers = sys.argv[-12]
15 randomj = sys.argv[-11]
16 trials = sys.argv[-10]
17 probability = sys.argv[-9]
18 # Note the spelling - so things don't get confused due to Python lambda function.
19 lammbda = sys.argv[-8]
20 order = sys.argv[-7]
21 mean = sys.argv[-6]
22 sigma = sys.argv[-5]
23 min = sys.argv[-4]
24 max = sys.argv[-3]
25 insertion = sys.argv[-2]
26 tmp_output_path = sys.argv[-1] 14 tmp_output_path = sys.argv[-1]
27 15
28 # Open the input image file. 16 # Open the input image file.
29 image_plus = IJ.openImage(input_file) 17 image_plus = IJ.openImage(input_file)
30 bit_depth = image_plus.getBitDepth()
31 image_type = image_plus.getType() 18 image_type = image_plus.getType()
19 is32BITS_GREY = image_type == ImagePlus.GRAY32
32 # Create an ImagePlus object for the image. 20 # Create an ImagePlus object for the image.
33 image_plus_copy = image_plus.duplicate() 21 image_plus_copy = image_plus.duplicate()
34 # Make a copy of the image.
35 image_processor_copy = image_plus_copy.getProcessor()
36 22
37 # Perform the analysis on the ImagePlus object. 23 # Perform the analysis on the ImagePlus object.
38 if noise == "add_noise": 24 try:
39 IJ.run(image_plus_copy, "Add Noise", "") 25 if noise == "add_noise":
40 elif noise == "add_specified_noise": 26 IJ.run(image_plus_copy, "Add Noise", "")
41 IJ.run(image_plus_copy, "Add Specified Noise", "standard=&standard_deviation") 27 elif noise == "add_specified_noise":
42 elif noise == "salt_and_pepper": 28 IJ.run(image_plus_copy, "Add Specified Noise...", "standard=%s" % standard_deviation)
43 IJ.run(image_plus_copy, "Salt and Pepper", "") 29 elif noise == "salt_and_pepper":
44 elif noise == "despeckle": 30 IJ.run(image_plus_copy, "Salt and Pepper", "")
45 IJ.run(image_plus_copy, "Despeckle", "") 31 elif noise == "despeckle":
46 elif noise == "remove_outliers": 32 IJ.run(image_plus_copy, "Despeckle", "")
47 IJ.run( 33 elif noise == "remove_outliers":
48 image_plus_copy,
49 "Remove Outliers",
50 "radius=&radius threshold=&threshold which=&which_outliers",
51 )
52 elif noise == "remove_nans":
53 IJ.run(image_plus_copy, "Remove NaNs", "")
54 elif noise == "rof_denoise":
55 IJ.run(image_plus_copy, "ROF Denoise", "")
56 elif noise == "randomj":
57 if randomj == "randomj_binomial":
58 IJ.run( 34 IJ.run(
59 image_plus_copy, 35 image_plus_copy,
60 "RandomJ Binomial", 36 "Remove Outliers...",
61 "trials=&trials probability=&probability insertion=&insertion", 37 "radius=%s threshold=%s which=%s" % (radius, threshold, which_outliers)
62 ) 38 )
63 elif randomj == "randomj_exponential": 39 elif noise == "remove_nans":
64 IJ.run( 40 if is32BITS_GREY:
65 image_plus_copy, 41 IJ.run(image_plus_copy, "Remove NaNs...", "")
66 "RandomJ Exponential", 42 else:
67 "lambda=&lammbda insertion=&insertion", 43 raise Exception("Remove NaNs can only be applied to 32bits grey images.")
68 ) 44 elif noise == "rof_denoise":
69 elif randomj == "randomj_gamma": 45 if is32BITS_GREY:
70 IJ.run(image_plus_copy, "RandomJ Gamma", "order=&order insertion=&insertion") 46 IJ.run(image_plus_copy, "ROF Denoise", "")
71 elif randomj == "randomj_gaussian": 47 else:
72 IJ.run( 48 raise Exception("ROF Denoise can only be applied to 32bits grey images.")
73 image_plus_copy, 49 except Exception as e:
74 "RandomJ Gaussian", 50 # This is due to some operations like remove_outliers and despeckle which block the script
75 "mean=&mean sigma=&sigma insertion=&insertion", 51 print(e)
76 ) 52 exit(1)
77 elif randomj == "randomj_poisson":
78 IJ.run(image_plus_copy, "RandomJ Poisson", "mean=&mean insertion=&insertion")
79 elif randomj == "randomj_uniform":
80 IJ.run(
81 image_plus_copy, "RandomJ Uniform", "min=&min max=&max insertion=&insertion"
82 )
83
84 # Save the ImagePlus object as a new image. 53 # Save the ImagePlus object as a new image.
85 IJ.saveAs(image_plus_copy, image_datatype, tmp_output_path) 54 IJ.saveAs(image_plus_copy, image_datatype, tmp_output_path)
55 # This is due to some operations like remove_outliers and despeckle which block the script
56 exit(0)