|
| 1 | +import os.path |
| 2 | +import sys |
| 3 | +import numpy as np |
| 4 | +from tifffile import imread, imsave |
| 5 | +import ctypes |
| 6 | + |
| 7 | +""" |
| 8 | +Various arithmetics to be applied to one channel. |
| 9 | +
|
| 10 | +Requirements |
| 11 | +------------ |
| 12 | +numpy (comes with Aivia installer) |
| 13 | +scikit-image (comes with Aivia installer) |
| 14 | +tifffile (installed with scikit-image) |
| 15 | +
|
| 16 | +Parameters |
| 17 | +---------- |
| 18 | +Input Image : Aivia channel |
| 19 | + Input channel to use for the processing. |
| 20 | +
|
| 21 | +Operation : int |
| 22 | + Number-coded arithmetics action (0=multiply, 1=divide, 2=add, 3=subtract). |
| 23 | +
|
| 24 | +Threshold : int |
| 25 | + Grayvalue for the arithmetics. |
| 26 | +
|
| 27 | +
|
| 28 | +Returns |
| 29 | +------- |
| 30 | +Aivia objects |
| 31 | + Result of the process. |
| 32 | +""" |
| 33 | + |
| 34 | +# [INPUT Name:inputImagePath Type:string DisplayName:'Input Image'] |
| 35 | +# [INPUT Name:value Type:int DisplayName:'Value' Default:0 Min:0 Max:65535] |
| 36 | +# [INPUT Name:actionType Type:int DisplayName:'Operation (0=multiply, 1=divide, 2=add, 3=subtract)' Default:0 Min:0 Max:65535] |
| 37 | +# [OUTPUT Name:resultImagePath Type:string DisplayName:'Processed Image'] |
| 38 | +def run(params): |
| 39 | + image_location = params['inputImagePath'] |
| 40 | + result_location = params['resultImagePath'] |
| 41 | + operation_type = int(params['actionType']) |
| 42 | + value = float(params['value']) |
| 43 | + tCount = int(params['TCount']) |
| 44 | + zCount = int(params['ZCount']) |
| 45 | + |
| 46 | + if not os.path.exists(image_location): |
| 47 | + print(f'Error: {image_location} does not exist') |
| 48 | + return |
| 49 | + |
| 50 | + image_data = imread(image_location) |
| 51 | + d_type = image_data.dtype |
| 52 | + dims = image_data.shape |
| 53 | + |
| 54 | + output_data = np.empty(image_data.shape, dtype=d_type) |
| 55 | + axes = '' |
| 56 | + |
| 57 | + # 3D+T |
| 58 | + if tCount > 1 and zCount > 1: |
| 59 | + print(f"Applying to 3D+T case with dims: {image_data.shape}") |
| 60 | + for t in range(0, dims[0]): |
| 61 | + output_data[t, :, :, :] = process_data(image_data[t, :, :, :], operation_type, value).astype(d_type) |
| 62 | + axes = 'TZYX' |
| 63 | + |
| 64 | + # 2D +/- T and 3D |
| 65 | + else: |
| 66 | + print(f"Applying to 2D/3D case with dims: {image_data.shape}") |
| 67 | + output_data = process_data(image_data, operation_type, value).astype(d_type) |
| 68 | + if zCount > 1: |
| 69 | + axes = 'ZYX' |
| 70 | + else: |
| 71 | + axes = 'TYX' if tCount > 1 else 'YX' |
| 72 | + |
| 73 | + imsave(result_location, output_data, metadata={'axes': axes}, ome=True) |
| 74 | + |
| 75 | + |
| 76 | +def process_data(data, op_type: int, value: int): |
| 77 | + out_data = None |
| 78 | + if op_type == 0: # MULTIPLY |
| 79 | + out_data = np.float32(data) * value |
| 80 | + elif op_type == 1: # DIVIDE |
| 81 | + if value == 0: |
| 82 | + sys.exit('Division by zero is not possible.') |
| 83 | + out_data = np.float32(data) / value |
| 84 | + elif op_type == 2: # ADD |
| 85 | + out_data = np.float32(data) + value |
| 86 | + elif op_type == 3: # SUBTRACT |
| 87 | + out_data = np.float32(data) - value |
| 88 | + elif op_type > 3: |
| 89 | + sys.exit('Wrong selection of operation type') |
| 90 | + |
| 91 | + # Clipping data only if 8 or 16 bit |
| 92 | + if any(data.dtype == dtp for dtp in [np.uint8, np.uint16]): |
| 93 | + if np.max(out_data) > np.iinfo(data.dtype).max or np.min(out_data) < 0: |
| 94 | + out_data = out_data.clip(0, np.iinfo(data.dtype).max) |
| 95 | + print(f'Clipping data to fit the {data.dtype} range.') |
| 96 | + |
| 97 | + return out_data |
| 98 | + |
| 99 | + |
| 100 | +def Mbox(title, text): |
| 101 | + return ctypes.windll.user32.MessageBoxW(0, text, title, 0) |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == '__main__': |
| 105 | + # Commandline arguments: image_path, |
| 106 | + params = {'inputImagePath': r'D:\PythonCode\_tests\3D Object Analysis From Seeds_test_seeds.aivia.tif', |
| 107 | + 'resultImagePath': r'D:\PythonCode\_tests\test.tif', |
| 108 | + 'Calibration': 'XYZT: 0.3225 Micrometers, 0.3225 Micrometers, 1 Micrometers, 1 Default', |
| 109 | + 'ZCount': 8, 'TCount': 1, 'actionType': 2, 'value': 2} |
| 110 | + run(params) |
| 111 | + |
| 112 | +# CHANGELOG |
| 113 | +# v1_00: - From Threshold_for_3DObjects.py. Compatible with 32 bit images |
| 114 | +# v1_10: - Adding ome=True tag with imwrite for Aivia 13.0.0 compatibility |
0 commit comments