-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSplitLabeledMask.py
More file actions
86 lines (65 loc) · 2.45 KB
/
Copy pathSplitLabeledMask.py
File metadata and controls
86 lines (65 loc) · 2.45 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
import sys
import numpy as np
from skimage.io import imread, imsave
from skimage import segmentation
import subprocess
"""
Separates labeled objects in a mask (2D or 3D).
For instance, CellPose and StarDist output masks where objects can touch each other.
In Aivia, objects need to be separated to be measurable.
Works only when there is no time dimension (yet).
Doc for boundaries function:
https://scikit-image.org/docs/stable/api/skimage.segmentation.html#skimage.segmentation.find_boundaries
Requirements
------------
numpy (comes with Aivia installer)
scikit-image (comes with Aivia installer)
Parameters
----------
Input channel:
Input channel with labeled mask and touching objects.
Returns
-------
New mask with separated labeled objects
"""
# [INPUT Name:inputImagePath Type:string DisplayName:'Labeled Mask']
# [OUTPUT Name:resultPath Type:string DisplayName:'Split Labeled Mask']
def run(params):
image_location = params['inputImagePath']
result_location = params['resultPath']
tCount = int(params['TCount'])
image_data = imread(image_location)
dims = image_data.shape
print('-- Input dimensions (expected (Z), Y, X): ', np.asarray(dims), ' --')
# Checking image is not 2D+t or 3D+t
if len(dims) > 3 or (len(dims) == 3 and tCount > 1):
message = 'Error: Cannot be applied to timelapses.'
Mbox('No info found', message, 0)
sys.exit(message)
output_data = np.empty_like(image_data)
# Detecting boundaries to be subtracted from original image
boundaries = segmentation.find_boundaries(image_data)
output_data = image_data - image_data * boundaries
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 = {'inputImagePath': 'D:\\python-tests\\test.aivia.tif',
'resultPath': 'D:\\python-tests\\test.tif',
'TCount': 1}
run(params)
# CHANGELOG:
# v1.01: - Changed ctypes popup to Powershell/VB