- Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathimage.py
147 lines (127 loc) · 4.9 KB
/
image.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
importnumpyasnp
importmatplotlibasmpl
importmatplotlib.colorsasmcolors
importmatplotlib.transformsasmtransforms
from .artistimportArtist
from .descriptionimportDesc, desc_like
from .conversion_edgeimportFuncEdge, Graph, CoordinateEdge
def_interpolate_nearest(image, x, y):
magnification=1# TODO
lef, rig=x
width=int(((round(rig) +0.5) - (round(lef) -0.5)) *magnification)
xpix=np.digitize(np.arange(width), np.linspace(0, rig-lef, image.shape[1]))
bot, top=y
height=int(((round(top) +0.5) - (round(bot) -0.5)) *magnification)
ypix=np.digitize(np.arange(height), np.linspace(0, top-bot, image.shape[0]))
out=np.empty((height, width, 4))
out[:, :, :] =image[
ypix[:, None].clip(0, image.shape[0] -1),
xpix[None, :].clip(0, image.shape[1] -1),
:,
]
returnout
classImage(Artist):
def__init__(self, container, edges=None, norm=None, cmap=None, **kwargs):
super().__init__(container, edges, **kwargs)
ifnormisNone:
norm=mcolors.Normalize()
ifcmapisNone:
cmap=mpl.colormaps["viridis"]
self.norm=norm
self.cmap=cmap
arrdesc=Desc(("M", "N"))
self._interpolation_edge=FuncEdge.from_func(
"interpolate_nearest_rgba",
_interpolate_nearest,
{
"image": Desc(("M", "N", 4), coordinates="rgba"),
"x": Desc(("X",), coordinates="display"),
"y": Desc(("Y",), coordinates="display"),
},
{"image": Desc(("O", "P", 4), coordinates="rgba_resampled")},
)
edges= [
CoordinateEdge.from_coords("xycoords", {"x": "auto", "y": "auto"}, "data"),
CoordinateEdge.from_coords(
"image_coords", {"image": Desc(("M", "N"), "auto")}, "data"
),
FuncEdge.from_func(
"image_norm",
lambdaimage: self.norm(image),
{"image": desc_like(arrdesc, coordinates="data_resampled")},
{"image": desc_like(arrdesc, coordinates="norm")},
),
FuncEdge.from_func(
"image_cmap",
lambdaimage: self.cmap(image),
{"image": desc_like(arrdesc, coordinates="norm")},
{"image": Desc(("M", "N", 4), coordinates="rgba")},
),
FuncEdge.from_func(
"image_display",
lambdaimage: (image*255).astype(np.uint8),
{"image": Desc(("O", "P", 4), "rgba_resampled")},
{"image": Desc(("O", "P", 4), "display")},
),
FuncEdge.from_func(
"rgb_rgba",
lambdaimage: np.append(
image, np.ones(image.shape[:-1] + (1,)), axis=-1
),
{"image": Desc(("M", "N", 3), "rgb")},
{"image": Desc(("M", "N", 4), "rgba")},
),
self._interpolation_edge,
]
self._graph=self._graph+Graph(edges, (("data", "data_resampled"),))
defdraw(self, renderer, graph: Graph) ->None:
ifnotself.get_visible():
return
g=graph+self._graph
conv=g.evaluator(
self._container.describe(),
{
"image": Desc(("O", "P", 4), "display"),
"x": Desc(("X",), "display"),
"y": Desc(("Y",), "display"),
},
)
query, _=self._container.query(g)
evald=conv.evaluate(query)
image=evald["image"]
x=evald["x"]
y=evald["y"]
clip_conv=g.evaluator(
self._clip_box.describe(),
{"x": Desc(("N",), "display"), "y": Desc(("N",), "display")},
)
clip_query, _=self._clip_box.query(g)
clipx, clipy=clip_conv.evaluate(clip_query).values()
gc=renderer.new_gc()
gc.set_clip_rectangle(
mtransforms.Bbox.from_extents(clipx[0], clipy[0], clipx[1], clipy[1])
)
renderer.draw_image(gc, x[0], y[0], image) # TODO vector backend transforms
defcontains(self, mouseevent, graph=None):
ifgraphisNone:
returnFalse, {}
g=graph+self._graph
conv=g.evaluator(
self._container.describe(),
{
"x": Desc(("X",), "display"),
"y": Desc(("Y",), "display"),
},
).inverse
query, _=self._container.query(g)
xmin, xmax=query["x"]
ymin, ymax=query["y"]
x, y=conv.evaluate({"x": mouseevent.x, "y": mouseevent.y}).values()
# This checks xmin <= x <= xmax *or* xmax <= x <= xmin.
inside= (
xisnotNone
and (x-xmin) * (x-xmax) <=0
andyisnotNone
and (y-ymin) * (y-ymax) <=0
)
returninside, {}