-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathimage_demo.py
182 lines (135 loc) · 4.77 KB
/
image_demo.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# -*- coding: utf-8 -*-
# https://matplotlib.org/3.2.1/gallery/images_contours_and_fields/image_demo.html
import sys
import matplotlib
import matplotlib.cbook as cbook
import matplotlib.cm as cm
import numpy as np
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure
from matplotlib.patches import PathPatch
from matplotlib.path import Path
from PyQt5.QtWidgets import (
QApplication,
QHBoxLayout,
QListWidget,
QStackedWidget,
QWidget,
)
class Widget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.list_widget = QListWidget()
self.stacked_widget = QStackedWidget()
lay = QHBoxLayout(self)
lay.addWidget(self.list_widget)
lay.addWidget(self.stacked_widget)
for name, w in (
("bivariate normal distribution", self.canvas_1()),
("images of pictures", self.canvas_2()),
("Interpolating images 1", self.canvas_3()),
("Interpolating images 2", self.canvas_4()),
("Interpolating images 3", self.canvas_5()),
):
self.list_widget.addItem(name)
self.stacked_widget.addWidget(w)
self.list_widget.selectionModel().currentChanged.connect(
lambda index: self.stacked_widget.setCurrentIndex(index.row())
)
self.list_widget.setFixedWidth(200)
def canvas_1(self):
canvas = FigureCanvas(Figure(figsize=(8, 6)))
delta = 0.025
x = y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-(X ** 2) - Y ** 2)
Z2 = np.exp(-((X - 1) ** 2) - (Y - 1) ** 2)
Z = (Z1 - Z2) * 2
ax = canvas.figure.subplots()
im = ax.imshow( # noqa: F841
Z,
interpolation="bilinear",
cmap=cm.RdYlGn,
origin="lower",
extent=[-3, 3, -3, 3],
vmax=abs(Z).max(),
vmin=-abs(Z).max(),
)
return canvas
def canvas_2(self):
canvas1 = FigureCanvas(Figure(figsize=(8, 6)))
with cbook.get_sample_data("ada.png") as image_file:
image = matplotlib.image.imread(image_file)
ax = canvas1.figure.subplots()
ax.imshow(image)
ax.axis("off") # clear x-axis and y-axis
# And another image
w, h = 512, 512
with cbook.get_sample_data("ct.raw.gz") as datafile:
s = datafile.read()
A = np.frombuffer(s, np.uint16).astype(float).reshape((w, h))
A /= A.max()
canvas2 = FigureCanvas(Figure(figsize=(8, 6)))
ax = canvas2.figure.subplots()
extent = (0, 25, 0, 25)
im = ax.imshow(A, cmap=cm.hot, origin="upper", extent=extent) # noqa: F841
markers = [(15.9, 14.5), (16.8, 15)]
x, y = zip(*markers)
ax.plot(x, y, "o")
ax.set_title("CT density")
w = QWidget()
w.setContentsMargins(0, 0, 0, 0)
lay = QHBoxLayout(w)
lay.setContentsMargins(0, 0, 0, 0)
lay.addWidget(canvas1)
lay.addWidget(canvas2)
return w
def canvas_3(self):
A = np.random.rand(5, 5)
canvas = FigureCanvas(Figure(figsize=(8, 6)))
axs = canvas.figure.subplots(1, 3)
for ax, interp in zip(axs, ["nearest", "bilinear", "bicubic"]):
ax.imshow(A, interpolation=interp)
ax.set_title(interp.capitalize())
ax.grid(True)
return canvas
def canvas_4(self):
canvas = FigureCanvas(Figure(figsize=(8, 6)))
x = np.arange(120).reshape((10, 12))
interp = "bilinear"
axs = canvas.figure.subplots(nrows=2, sharex=True)
axs[0].set_title("blue should be up")
axs[0].imshow(x, origin="upper", interpolation=interp)
axs[1].set_title("blue should be down")
axs[1].imshow(x, origin="lower", interpolation=interp)
return canvas
def canvas_5(self):
canvas = FigureCanvas(Figure(figsize=(8, 6)))
delta = 0.025
x = y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-(X ** 2) - Y ** 2)
Z2 = np.exp(-((X - 1) ** 2) - (Y - 1) ** 2)
Z = (Z1 - Z2) * 2
path = Path([[0, 1], [1, 0], [0, -1], [-1, 0], [0, 1]])
patch = PathPatch(path, facecolor="none")
ax = canvas.figure.subplots()
ax.add_patch(patch)
im = ax.imshow(
Z,
interpolation="bilinear",
cmap=cm.gray,
origin="lower",
extent=[-3, 3, -3, 3],
clip_path=patch,
clip_on=True,
)
im.set_clip_path(patch)
return canvas
def main():
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()