-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMaxIntensityProjectionRGB.py
More file actions
106 lines (81 loc) · 3.31 KB
/
MaxIntensityProjectionRGB.py
File metadata and controls
106 lines (81 loc) · 3.31 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
100
101
102
103
104
105
106
import os.path
import numpy as np
from skimage.io import imread, imsave
import shlex, subprocess
import sys
from os.path import dirname as up
from os.path import isfile
"""
Performs a maximum intensity projection through Z for a single channel.
Repeats the operation for the two other channels of the RGB image.
Works only in 3D (not 3D+t yet).
Requirements
------------
numpy (comes with Aivia installer)
scikit-image (comes with Aivia installer)
Parameters
----------
Input channels:
3 input channels to use for the projection.
Returns
-------
New channels in original 3D image:
Returns a binary map of the location of max values detected in volume.
New 3-channel 2D image:
Opens Aivia (again) to display the 2D projection as a new image.
"""
# [INPUT Name:inputRedPath Type:string DisplayName:'Red channel']
# [INPUT Name:inputGreenPath Type:string DisplayName:'Green channel']
# [INPUT Name:inputBluePath Type:string DisplayName:'Blue channel']
# [OUTPUT Name:resultPath Type:string DisplayName:'Empty channel']
def run(params):
image_location = []
image_location.append(params['inputRedPath'])
image_location.append(params['inputGreenPath'])
image_location.append(params['inputBluePath'])
result_location = params['resultPath']
tCount = int(params['TCount'])
for c in range(0, 3):
if not os.path.exists(image_location[c]):
print(f"Error: {image_location[c]} does not exist")
return;
first_ch = imread(image_location[0])
input_dims = np.asarray(first_ch.shape)
print('-- Input dimensions (expected Z, Y, X): ', input_dims, ' --')
new_input_dims = np.insert(input_dims, 0, 3)
image_data = np.zeros(new_input_dims).astype(first_ch.dtype)
for c in range(0, 3):
image_data[c] = imread(image_location[c])
# Checking image is not 2D or 2D+t
if input_dims.size == 2 or (input_dims.size == 3 and tCount > 1):
print('Error: Maximum intensity projection cannot be applied to 2D images.')
return;
output_data = np.empty_like(first_ch)
proj_output = np.zeros([input_dims[1], input_dims[2], 3]) # TO CHECK
for c in range(0, 3):
if tCount == 1: # (image is not 3D+t)
# Generate 2D max projection for each channel
proj_output[:, :, 2-c] = np.amax(image_data[c], axis = 0)
# Saving 3 channel image as single tif
temp_location = result_location.replace('.tif', 'tmp.tif')
imsave(temp_location, proj_output.astype(np.uint8))
imsave(result_location, output_data)
aivia_path = params['CallingExecutable']
# Added for handling testing without opening aivia
if aivia_path == "None":
return
if not os.path.exists(aivia_path):
print(f"Error: {aivia_path} does not exist")
return;
# Run external program
cmdLine = 'start \"\" \"'+ aivia_path +'\" \"'+ temp_location +'\"'
args = shlex.split(cmdLine)
subprocess.run(args, shell=True)
if __name__ == '__main__':
params = {}
params['inputRedPath'] = 'D:\\python-tests\\3Dimage.aivia.tif'
params['inputGreenPath'] = 'D:\\python-tests\\3Dimage.aivia.tif'
params['inputBluePath'] = 'D:\\python-tests\\3Dimage.aivia.tif'
params['resultPath'] = 'D:\\python-tests\\3DMaxMap.tif'
params['TCount'] = 1
run(params)