forked from yu02019/BEN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransparent_imshow.py
102 lines (95 loc) · 3.48 KB
/
transparent_imshow.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
"""
This is a simple wrapper along matplotlib's `imshow` function, which
allows to produce images with pixel-dependent transparency.
"""
import matplotlib
import matplotlib.pyplot as plt
def transp_imshow( data, tvmin=None, tvmax=None, tmax=1.,
gam=1., cmap='Blues', **kwargs ) :
"""
Displays the 2d array `data` with pixel-dependent transparency.
Parameters
----------
data: 2d numpy array of floats or ints
Contains the data to be plotted as a 2d map
tvmin, tvmax: floats or None, optional
The values (for the elements of `data`) that will be plotted
with minimum opacity and maximum opacity, respectively.
If no value is provided, this uses by default the arguments
`vmin` and `vmax` of `imshow`, or the min and max of `data`.
tmax: float, optional
Value between 0 and 1. Maximum opacity, which is reached
for pixel that have a value greater or equal to `tvmax`.
Default: 1.
gam: float, optional
Distortion of the opacity with pixel-value.
For `gam` = 1, the opacity varies linearly with pixel-value
For `gam` < 1, low values have higher-than-linear opacity
For `gam` > 1, low values have lower-than-linear opacity
cmap: a string or a maplotlib.colors.Colormap object
Colormap to be used
kwargs: dict
Optional arguments, which are passed to matplotlib's `imshow`.
"""
# Determine the values between which the transparency will be scaled
if 'vmax' in kwargs :
vmax = kwargs['vmax']
else :
vmax = data.max()
if 'vmin' in kwargs :
vmin = kwargs['vmin']
else :
vmin = data.min()
if tvmax is None:
tvmax = vmax
if tvmin is None:
tvmin = vmin
# Rescale the data to get the transparency and color
color = (data-vmin)/(vmax-vmin)
color[color > 1.] = 1.
color[color < 0.] = 0.
transparency = tmax*(data-tvmin)/(tvmax-tvmin)
transparency[transparency > 1.] = 1
transparency[transparency < 0.] = 0.
# Application of a gamma distortion
transparency = tmax * transparency**gam
# Get the colormap
if isinstance( cmap, matplotlib.colors.Colormap ):
colormap = cmap
elif type(cmap) == str:
colormap = getattr( plt.cm, cmap )
else:
raise ValueError('Invalid type for argument `cmap`.')
# Create an rgba stack of the data, using the colormap
rgba_data = colormap( color )
# Modify the transparency
rgba_data[:,:,3] = transparency
sc = plt.imshow( rgba_data, **kwargs )
# test
# plt.colorbar(sc)
return sc
# 2021/06/30 添加,便于绘图
def plot_no_margin(img=None, fig=None,save_path=None):
'''
去除白边
:param img:
:param save_path:
:return:
'''
# fig, ax = plt.subplots()
# # plt.imshow(rawimg, cmap='gray')
# ax.imshow(rawimg, cmap='gray',
# # aspect="equal"
# )
plt.xticks([]), plt.yticks([])
plt.axis("off")
# 去除图像周围的白边
height, width = img.shape
# 如果dpi=300,那么图像大小=height*width
fig.set_size_inches(width / 100.0, height / 100.0)
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.subplots_adjust(top=1, bottom=0, left=0, right=1, hspace=0, wspace=0)
plt.margins(0, 0)
# plt.savefig('{}_output/'.format(patientID) + name_id + '_' + str(slice_id) + '_Raw.png')
# plt.savefig(save_path)