-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImagePlay.py
152 lines (110 loc) · 3.33 KB
/
ImagePlay.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
"""
The purpose of this script is to start playing around with different python image processing tools
"""
import numpy as np
from CIMP import Event as ev
from CIMP import Snapshot as snap
import sunpy.map
from sunpy.net import attrs as a
import matplotlib.pyplot as plt
from skimage import exposure
from skimage.filters.rank import (median, enhance_contrast, enhance_contrast_percentile,
autolevel)
from skimage.morphology import disk
from skimage.restoration import (denoise_tv_chambolle, denoise_nl_means)
plotcase = 3
snapshot = False
if plotcase == 1:
testcase = 1
nrgf = False
idx = 9
scale = (0.0, 1000.0)
elif plotcase == 2:
testcase = 1
nrgf = True
idx = 9
scale = (0.0, 4.0)
elif plotcase == 3:
snapshot = True
testcase = 1
nrgf = True
#scale = (600, 4000)
scale = (0,4)
else:
print("specify a valid plotcase")
exit()
# ===================
if snapshot:
x = snap.snapshot.testcase(testcase)
if nrgf:
x.nrgf()
a = x.data
amap = x.map()
header = x.header
else:
x = ev.event.testcase(testcase)
if nrgf:
x.nrgf()
a = x[idx]
amap = x.map(idx)
header = x.header[idx]
# for experimenting
#timerange = a.Time('2016/09/06 8:00:00', '2016/09/06 12:00:00')
#x = ev.event.fromtime(a.Instrument.lasco, a.Detector.c2, timerange)
print(80*'-')
print(x)
print(80*'-')
print(repr(x))
print(80*'-')
# ===================
amin = np.amin(a)
amax = np.amax(a)
if len(scale) != 2:
scale = (amin, amax)
print(f"Original image range: {amin} to {amax}")
# clipped image
aclip = a.clip(min=scale[0], max=scale[1])
# scaled byte image
#asc = (a - amin)/(amax - amin)
#asc = (255*aclip/np.amax(aclip)).astype('uint8')
asc = aclip/np.amax(aclip)
#======================================================================
# image enhancements
# first remove noise
psc1 = denoise_tv_chambolle(asc, weight = 0.2)
# this doesn't do much
#psc = exposure.equalize_hist(asc)
# adaptive equalization is great
psc2 = exposure.equalize_adapthist(psc1)
# gamma correction
#psc = exposure.adjust_gamma(psc2, gamma=0.9, gain=1.0)
# enhance contrast
psc = enhance_contrast_percentile(psc2,disk(1), p0=.1, p1=.9)
#psc = autolevel(psc2,disk(10))
# rescale
psc = (psc.astype('float') - np.amin(psc))/(np.amax(psc) - np.amin(psc))
p = (scale[1] - scale[0])*psc + scale[0]
#======================================================================
# plot
fig = plt.figure(figsize=[22,10])
print(f"image scale: {scale[0]} to {scale[1]}")
# Optionally plot as a sunpy map
plotasmap = True
if plotasmap:
ax = fig.add_subplot(1,2,1,projection=amap)
amap.plot(vmin = scale[0], vmax = scale[1])
pmap = sunpy.map.Map(p, header)
ax = fig.add_subplot(1,2,2,projection=pmap)
pplot = pmap.plot(vmin = scale[0], vmax = scale[1])
else:
ax = fig.add_subplot(1,2,1)
plt.imshow(a, vmin = scale[0], vmax = scale[1], cmap = amap.cmap)
#plt.imshow(asc, cmap=amap.cmap)
# plot processed image
ax = fig.add_subplot(1,2,2)
pplot = plt.imshow(psc, cmap=amap.cmap)
# see what matplotlib is using for the limits of the color scale
clim = pplot.get_clim()
print(f"Limits used for the color table = {clim[0]} to {clim[1]}")
#======================================================================
plt.show()