-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprocessing.py
More file actions
193 lines (148 loc) Β· 5.05 KB
/
processing.py
File metadata and controls
193 lines (148 loc) Β· 5.05 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""ImageJ processing utilities for filtering and thresholding images.
This module provides functions to apply various image processing operations
using ImageJ, including filters, background subtraction, and thresholding.
"""
from ij import IJ
from ..log import LOG as log
def filter_options(filter_method, filter_radius, do_3d=False):
"""Build the ImageJ filter command and options strings."""
if do_3d:
filter_name = filter_method + " 3D..."
else:
filter_name = filter_method + "..."
options = (
"sigma="
if filter_method == "Gaussian Blur"
else "radius=" + str(filter_radius) + " stack"
)
return filter_name, options
def threshold_options(threshold_method, do_3d=True):
"""Build the ImageJ threshold option strings."""
auto_threshold_options = (
threshold_method + " " + "dark" + " " + "stack" if do_3d else ""
)
convert_to_binary_options = (
"method=" + threshold_method + " " + "background=Dark" + " " + "black"
)
return auto_threshold_options, convert_to_binary_options
def apply_filter(imp, filter_method, filter_radius, do_3d=False):
"""Make a specific filter followed by a threshold method of choice.
Parameters
----------
imp : ImagePlus
Input ImagePlus to filter and threshold
filter_method : str
Name of the filter method to use. Must be one of:
- Median
- Mean
- Gaussian Blur
- Minimum
- Maximum
filter_radius : int
Radius of the filter filter to use
do_3d : bool, optional
If set to True, will do a 3D filtering, by default False
Returns
-------
ij.ImagePlus
Filtered ImagePlus
"""
log.info("Applying filter %s with radius %d" % (filter_method, filter_radius))
if filter_method not in [
"Median",
"Mean",
"Gaussian Blur",
"Minimum",
"Maximum",
]:
raise ValueError(
"filter_method must be one of: Median, Mean, Gaussian Blur, Minimum, Maximum"
)
filter, options = filter_options(filter_method, filter_radius, do_3d=do_3d)
log.debug("Filter: <%s> with options <%s>" % (filter, options))
imageplus = imp.duplicate()
IJ.run(imageplus, filter, options)
return imageplus
def apply_rollingball_bg_subtraction(
imp,
rolling_ball_radius,
light_background=False,
sliding=False,
disable=False,
do_3d=False,
):
"""Perform background subtraction using a rolling ball method.
Parameters
----------
imp : ij.ImagePlus
Input ImagePlus to filter and threshold
rolling_ball_radius : int
Radius of the rolling ball filter to use
light_background : bool, optional
If set to True, will treat the background as light, by default False
sliding : bool, optional
If set to True, will do a sliding window approach, by default False
disable : bool, optional
If set to True, will disable the smoothing, by default False
do_3d : bool, optional
If set to True, will do a 3D filtering, by default False
Returns
-------
ij.ImagePlus
Filtered ImagePlus
"""
log.info("Applying rolling ball with radius %d" % rolling_ball_radius)
options = rolling_ball_options(
rolling_ball_radius,
light_background=light_background,
sliding=sliding,
disable=disable,
do_3d=do_3d,
)
log.debug("Background subtraction options: %s" % options)
imageplus = imp.duplicate()
IJ.run(imageplus, "Subtract Background...", options)
return imageplus
def rolling_ball_options(
rolling_ball_radius,
light_background=False,
sliding=False,
disable=False,
do_3d=False,
):
"""Generate the options for the "Subtract Background..." macro command."""
parts = ["rolling=" + str(rolling_ball_radius)]
if light_background:
parts.append("light")
if sliding:
parts.append("sliding")
if disable:
parts.append("disable")
if do_3d:
parts.append("stack")
return " ".join(parts)
def apply_threshold(imp, threshold_method, do_3d=True):
"""Apply a threshold method to the input ImagePlus.
Parameters
----------
imp : ij.ImagePlus
Input ImagePlus to filter and threshold
threshold_method : str
Name of the threshold method to use
do_3d : bool, optional
If set to True, the automatic threshold will be done on a 3D stack, by default True
Returns
-------
ij.ImagePlus
Thresholded ImagePlus
"""
log.info("Applying threshold method %s" % threshold_method)
imageplus = imp.duplicate()
auto_threshold_options, convert_to_binary_options = threshold_options(
threshold_method, do_3d=do_3d
)
log.debug("Auto threshold options: %s" % auto_threshold_options)
IJ.setAutoThreshold(imageplus, auto_threshold_options)
log.debug("Convert to binary options: %s" % convert_to_binary_options)
IJ.run(imageplus, "Convert to Mask", convert_to_binary_options)
return imageplus