forked from CellProfiler/CellProfiler-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variancetransform.py
159 lines (122 loc) · 4.99 KB
/
variancetransform.py
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
__doc__ ="""\
VarianceTransform
=================
**VarianceTransform**
This module allows you to calculate the variance of an image, using a determined window size. It also has
the option to find the optimal window size from a predetermied range to obtain the maximum variance of an image.
============ ============ ===============
Supports 2D? Supports 3D? Respects masks?
============ ============ ===============
YES YES YES
============ ============ ===============
"""
import logging
import numpy
import pandas
import scipy.ndimage
import cellprofiler_core.setting
import cellprofiler_core.module
from cellprofiler_core.image import Image
from cellprofiler_core.setting import Binary
from cellprofiler_core.setting.subscriber import ImageSubscriber
from cellprofiler_core.setting.text import ImageName, Integer
class VarianceTransform(cellprofiler_core.module.ImageProcessing):
module_name = "VarianceTransform"
variable_revision_number = 1
def create_settings(self):
self.image_name = ImageSubscriber(
"Select the input image",
"None",
doc="""Select the image to be smoothed.""",
)
self.output_image_name = ImageName(
"Name the output image",
"FilteredImage",
doc="""Enter a name for the resulting image.""",
)
self.calculate_maximal = Binary(
"Calculate optimal window size to maximize image variance?",
False,
doc="""\
Select "*Yes*" to provide a range that will be used to obtain the window size that will generate
the maximum variance in the input image.
Select "*No*" to give the window size used to obtain the image variance.""",
)
self.window_size = Integer(
"Window size",
5,
minval=1,
doc="""Enter the size of the window used to calculate the variance.""",
)
self.window_min = Integer(
"Window min",
5,
minval=1,
doc="""Enter the minimum size of the window used to calculate the variance.""",
)
self.window_max = Integer(
"Window max",
50,
minval=1,
doc="""Enter the maximum size of the window used to calculate the variance.""",
)
def settings(self):
return [
self.image_name,
self.output_image_name,
self.calculate_maximal,
self.window_size,
self.window_min,
self.window_max,
]
def visible_settings(self):
__settings__ = [self.image_name, self.output_image_name,]
__settings__ += [self.calculate_maximal,]
if not self.calculate_maximal.value:
__settings__ += [self.window_size,]
else:
__settings__ += [self.window_min, self.window_max,]
return __settings__
def run(self, workspace):
image = workspace.image_set.get_image(self.image_name.value, must_be_grayscale=True)
image_pixels = image.pixel_data
window_range = range(self.window_min.value,self.window_max.value,1)
size = self.window_size.value
if self.calculate_maximal.value:
maxvar_dic = {}
for i in window_range:
result = abs(scipy.ndimage.uniform_filter(image_pixels**2, size=i, output=numpy.float64)-(scipy.ndimage.uniform_filter(image_pixels, size=i, output=numpy.float64)**2))
maxvar_dic[i] = {}
maxvar_dic[i]["window_size"] = i
maxvar_dic[i]["max_variance"] = result.max()
df = pandas.DataFrame(data=maxvar_dic)
df = df.transpose()
max_size= int(df["window_size"].loc[df["max_variance"] == df["max_variance"].max()])
size = max_size
output_pixels = abs(scipy.ndimage.uniform_filter(image_pixels**2, size=size, output=numpy.float64)
- (scipy.ndimage.uniform_filter(image_pixels, size=size, output=numpy.float64)**2))
new_image = Image(output_pixels, parent_image=image, dimensions=image.dimensions)
workspace.image_set.add(self.output_image_name.value, new_image)
if self.show_window:
workspace.display_data.pixel_data = image_pixels
workspace.display_data.output_pixels= output_pixels
workspace.display_data.dimensions = image.dimensions
def display(self, workspace, figure):
layout = (2, 1)
figure.set_subplots(
dimensions=workspace.display_data.dimensions, subplots=layout
)
figure.subplot_imshow(
colormap="gray",
image = workspace.display_data.pixel_data,
title = self.image_name.value,
x=0,
y=0,
)
figure.subplot_imshow(
colormap="gray",
image = workspace.display_data.output_pixels,
title = self.output_image_name.value,
x=1,
y=0,
)