-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMorphPlay.py
More file actions
180 lines (144 loc) · 4.21 KB
/
MorphPlay.py
File metadata and controls
180 lines (144 loc) · 4.21 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
"""
Playing with Mathematical Morphology
"""
import numpy as np
import matplotlib.pyplot as plt
import sunpy.map
import sunpy.visualization.colormaps as cm
from CIMP import Snapshot as snap
from skimage.morphology import opening, closing, disk, erosion, reconstruction
pcase = 7
# dcase = 1: subtract the background
# dcase = 2: take a ratio with the background
dcase = 2
clip = None
scales = None
rmask = None
colormap = 'stereo'
if pcase == 1:
comp = (0,1)
testcase = 1
scales = [(0,.05),(0, 1)]
elif pcase == 2:
comp = (0,2)
testcase = 1
scales = [(0,.05),(0, 0.05)]
elif pcase == 3:
comp = (0,3)
testcase = 1
scales = [(0,.05),(0, 0.05)]
elif pcase == 4:
comp = (0,4)
testcase = 1
scales = [(0,.05),(0, 0.05)]
elif pcase == 5:
comp = (0,5)
testcase = 1
scales = [(0,.05),(0, 0.05)]
elif pcase == 6:
comp = (0,5)
testcase = 2
scales = [(0,.03),(0, 0.03)]
elif pcase == 7:
comp = (0,6)
testcase = 1
scales = [(0,.05),(0.1, 1.0)]
colormap = 'lasco'
else:
print("specify a valid test case")
exit()
#------------------------------------------------------------------------------
def plot_comparison(original, filtered, filter_name, scales, colormap = 'stereo'):
if colormap == 'stereo':
cmap = plt.get_cmap('stereocor2')
else:
cmap = plt.get_cmap('soholasco2')
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(16, 8), sharex=True,
sharey=True)
ax1.imshow(original, cmap=cmap, vmin = scales[0][0], vmax = scales[0][1])
ax1.set_title('original')
ax1.axis('off')
ax2.imshow(filtered, cmap=cmap, vmin = scales[1][0], vmax = scales[1][1])
ax2.set_title(filter_name)
ax2.axis('off')
#------------------------------------------------------------------------------
x = snap.snapshot.testcase(testcase)
# set dcase to 0 if there is no background
if dcase == 1:
x.subtract_background()
elif dcase == 2:
x.background_ratio()
#------------------------------------------------------------------------------
# choose your battle
tag = None
images = []
titles = []
# default scales can be overridden
dscales = []
if comp.count(0) > 0:
titles.append("Base image")
images.append(x.data)
dscales.append((0.0,1.0))
if comp.count(1) > 0:
x.point_filter()
titles.append("Point filter")
images.append(x.data)
dscales.append((0.0,1.0))
if comp.count(2) > 0:
footprint = disk(3)
fim = opening(x.data, footprint)
titles.append("Opening footprint disk(3)")
images.append(fim)
dscales.append((0.0,1.0))
if comp.count(3) > 0:
footprint = disk(3)
fim = opening(x.data, footprint)
fim2 = closing(fim, footprint)
titles.append("Opening and closing")
images.append(fim2)
dscales.append((0.0,1.0))
if comp.count(4) > 0:
footprint = disk(3)
fim = erosion(x.data, footprint)
titles.append("Erosion")
images.append(fim)
dscales.append((0.0,1.0))
if comp.count(5) > 0:
footprint = disk(3)
fim = erosion(x.data, footprint)
fim2 = reconstruction(fim, x.data, method = 'dilation', footprint = footprint)
titles.append("OMR")
images.append(fim2)
dscales.append((0.0,1.0))
if comp.count(6) > 0:
#a = en.omr(x.data, rescaleim = False)
x.enhance(point = 'omr', detail = 'mgn', noise_filter = 'omr')
x.mask_annulus(rmax = 1.05)
titles.append("Enhance")
images.append(x.data)
dscales.append((0.0,1.0))
#------------------------------------------------------------------------------
for image in images:
print(f"range: {np.min(image)} {np.max(image)}")
if scales is None:
scales = dscales
else:
if scales[0] is None:
scales[0] = dscales[0]
if scales[1] is None:
scales[1] = dscales[1]
#------------------------------------------------------------------------------
# plot
plot_comparison(images[0],images[1],titles[1], scales, colormap = colormap)
# ===================
# save to a file
# ==================
dir = '/home/mark.miesch/Products/image_processing/images/Morph/'
fname = f"Morph_t{testcase}_{comp[0]}_vs_{comp[1]}"
if dcase == 2:
fname += "_rat"
if tag is not None:
fname += f"_{tag}"
file = dir + fname + ".png"
plt.savefig(file)
plt.show()