Mercurial > repos > iuc > imagej2_noise
comparison jython_script.py @ 0:da9a450c85b7 draft
planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
| author | iuc |
|---|---|
| date | Fri, 19 Jun 2015 16:59:22 -0400 |
| parents | |
| children | c8fc3f34cbff |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:da9a450c85b7 |
|---|---|
| 1 import sys | |
| 2 from ij import IJ | |
| 3 from ij import ImagePlus | |
| 4 | |
| 5 def handle_error( error_log, msg ): | |
| 6 elh = open( error_log, 'wb' ) | |
| 7 elh.write( msg ) | |
| 8 elh.close() | |
| 9 | |
| 10 # Fiji Jython interpreter implements Python 2.5 which does not | |
| 11 # provide support for argparse. | |
| 12 error_log = sys.argv[ -19 ] | |
| 13 input = sys.argv[ -18 ] | |
| 14 image_datatype = sys.argv[ -17 ] | |
| 15 noise = sys.argv[ -16 ] | |
| 16 standard_deviation = sys.argv[ -15 ] | |
| 17 radius = sys.argv[ -14 ] | |
| 18 threshold = sys.argv[ -13 ] | |
| 19 which_outliers = sys.argv[ -12 ] | |
| 20 randomj = sys.argv[ -11 ] | |
| 21 trials = sys.argv[ -10 ] | |
| 22 probability = sys.argv[ -9 ] | |
| 23 # Note the spelling - so things don't get confused due to Python lambda function. | |
| 24 lammbda = sys.argv[ -8 ] | |
| 25 order = sys.argv[ -7 ] | |
| 26 mean = sys.argv[ -6 ] | |
| 27 sigma = sys.argv[ -5 ] | |
| 28 min = sys.argv[ -4 ] | |
| 29 max = sys.argv[ -3 ] | |
| 30 insertion = sys.argv[ -2 ] | |
| 31 tmp_output_path = sys.argv[ -1 ] | |
| 32 | |
| 33 error = False | |
| 34 | |
| 35 # Open the input image file. | |
| 36 image_plus = IJ.openImage( input ) | |
| 37 bit_depth = image_plus.getBitDepth() | |
| 38 image_type = image_plus.getType() | |
| 39 # Create an ImagePlus object for the image. | |
| 40 image_plus_copy = image_plus.createImagePlus() | |
| 41 # Make a copy of the image. | |
| 42 image_processor_copy = image_plus.getProcessor().duplicate() | |
| 43 # Set the ImageProcessor on the duplicate ImagePlus object. | |
| 44 image_plus_copy.setProcessor( "image copy", image_processor_copy ) | |
| 45 | |
| 46 # Perform the analysis on the ImagePlus object. | |
| 47 if noise == 'add_noise': | |
| 48 IJ.run( image_plus_copy, "Add Noise", "" ) | |
| 49 elif noise == 'add_specified_noise': | |
| 50 IJ.run( image_plus_copy, "Add Specified Noise", "standard=&standard_deviation" ) | |
| 51 elif noise == 'salt_and_pepper': | |
| 52 IJ.run( image_plus_copy, "Salt and Pepper", "" ) | |
| 53 elif noise == 'despeckle': | |
| 54 IJ.run( image_plus_copy, "Despeckle", "" ) | |
| 55 elif noise == 'remove_outliers': | |
| 56 IJ.run( image_plus_copy, "Remove Outliers", "radius=&radius threshold=&threshold which=&which_outliers" ) | |
| 57 elif noise == 'remove_nans': | |
| 58 if bit_depth == 32: | |
| 59 IJ.run( image_plus_copy, "Remove NaNs", "" ) | |
| 60 else: | |
| 61 # When Galaxy metadata for images is enhanced to include information like this, | |
| 62 # we'll be able to write tool validators rather than having to stop the job in | |
| 63 # an error state. | |
| 64 msg = "Remove NaNs requires a 32-bit image, the selected image is %d-bit" % bit_depth | |
| 65 handle_error( error_log, msg ) | |
| 66 error = True | |
| 67 elif noise == 'rof_denoise': | |
| 68 if image_type == ImagePlus.GRAY32: | |
| 69 IJ.run( image_plus_copy, "ROF Denoise", "" ) | |
| 70 else: | |
| 71 msg = "ROF Denoise requires an image of type 32-bit grayscale, the selected image is %d-bit" % ( bit_depth ) | |
| 72 handle_error( error_log, msg ) | |
| 73 error = True | |
| 74 elif noise == 'randomj': | |
| 75 if randomj == 'randomj_binomial': | |
| 76 IJ.run( image_plus_copy, "RandomJ Binomial", "trials=&trials probability=&probability insertion=&insertion" ) | |
| 77 elif randomj == 'randomj_exponential': | |
| 78 IJ.run( image_plus_copy, "RandomJ Exponential", "lambda=&lammbda insertion=&insertion" ) | |
| 79 elif randomj == 'randomj_gamma': | |
| 80 IJ.run( image_plus_copy, "RandomJ Gamma", "order=&order insertion=&insertion" ) | |
| 81 elif randomj == 'randomj_gaussian': | |
| 82 IJ.run( image_plus_copy, "RandomJ Gaussian", "mean=&mean sigma=&sigma insertion=&insertion" ) | |
| 83 elif randomj == 'randomj_poisson': | |
| 84 IJ.run( image_plus_copy, "RandomJ Poisson", "mean=&mean insertion=&insertion" ) | |
| 85 elif randomj == 'randomj_uniform': | |
| 86 IJ.run( image_plus_copy, "RandomJ Uniform", "min=&min max=&max insertion=&insertion" ) | |
| 87 | |
| 88 if not error: | |
| 89 # Save the ImagePlus object as a new image. | |
| 90 IJ.saveAs( image_plus_copy, image_datatype, tmp_output_path ) |
