Repository navigation
Expand file tree
/
Copy pathMinSlices.py
More file actions
99 lines (81 loc) · 2.88 KB
/
Copy pathMinSlices.py
File metadata and controls
99 lines (81 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os.path
import numpy as np
from skimage.io import imread, imsave
import subprocess
import sys
"""
Performs a slicewise minimum intensity projection through Z with a given
width about a slice.
For example, using a width value of 2 means that for every slice, every
voxel in XY will be replaced with the minimum value found within the 2
slices before and after that slice.
Works only in 3D.
Requirements
------------
numpy (comes with Aivia installer)
scikit-image (comes with Aivia installer)
Parameters
----------
Input Image : Aivia channel
Input channel to use for the transform.
Width : int
Number of slices around the current slice to consider for the MIP.
Returns
-------
Aivia channel
Result of the transform
"""
# [INPUT Name:inputImagePath Type:string DisplayName:'Input Image']
# [INPUT Name:width Type:int DisplayName:'Width' Default:3 Min:2 Max:1000]
# [OUTPUT Name:resultPath Type:string DisplayName:'MinimumZ']
def run(params):
image_location = params['inputImagePath']
result_location = params['resultPath']
width = int(params['width'])
tCount = int(params['TCount'])
if not os.path.exists(image_location):
print(f'Error: {image_location} does not exist')
return;
image_data = imread(image_location)
dims = image_data.shape
output_data = np.empty_like(image_data)
if len(dims) == 2 or (len(dims) == 3 and tCount > 1):
error_mes = "Error: Minimum intensity projection cannot be applied to 2D images."
Mbox('Error', error_mes, 0)
sys.exit(error_mes)
if tCount == 1:
max_slice = int(dims[0])
width_eff = min(int(width/2), max_slice)
for i in np.arange(0, max_slice):
output_data[i, :, :] = np.amin(
image_data[i-min(width_eff,i):i+min(width_eff,max_slice-i), :, :], axis=0
)
else:
max_slice = int(dims[1])
width_eff = min(int(width/2), max_slice)
for i in np.arange(0, max_slice):
output_data[:, i, :, :] = np.amin(
image_data[:, i-min(width_eff,i):i+min(width_eff,max_slice-i), :, :], axis=1
)
imsave(result_location, output_data)
def Mbox(title, text, style):
style_tags = ["OkOnly", "OkCancel", "YesNo", "YesNoCancel"]
cmd = [
"powershell",
"-Command",
"Add-Type -AssemblyName Microsoft.VisualBasic; "
f"$x=[Microsoft.VisualBasic.Interaction]::MsgBox('{text}', "
f"[Microsoft.VisualBasic.MsgBoxStyle]::{style_tags[style]}, '{title}');"
"Write-Output $x"
]
result = subprocess.run(cmd, capture_output=True, text=True)
try:
return result.stdout.strip() # return = "Ok" or "Cancel"
except:
return None
if __name__ == '__main__':
params = {}
params['inputImagePath'] = 'test.png'
params['resultPath'] = 'testResult.png'
params['width'] = 2
run(params)