按键图像切片上的pyQt5 matplotlib
pyQt5 matplotlib on key press image slices
Matplotlib 嵌入 PyQt5 时不响应按键。我已经验证按键可以与 Tkinter 一起使用。这是我第一次体验 PyQt5。我不确定我在这里做错了什么。我试图通过这样做将重点放在 canvas 上。所有这些都是为了显示 3D 体积数据,您可以将其视为一堆图像。它们一起描述了一个 3D 结构。例如,磁共振成像 (MRI)
self.canvas.setFocusPolicy( QtCore.Qt.ClickFocus )
self.canvas.setFocus()
然而 returns 一个错误:
self.canvas.setFocusPolicy(QtCore.Qt.ClickFocus)
AttributeError: 'InputCanvas' object has no attribute 'canvas'
这是我的代码:
import sys
from PyQt5 import QtCore
from skimage import io
from PyQt5.QtWidgets import QMainWindow, QSizePolicy
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from PyQt5.QtWidgets import QApplication
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as
FigureCanvas
class App(QMainWindow):
def __init__(self):
super().__init__()
self.left = 10
self.top = 10
self.title = 'PYQT'
self.width = 600
self.height = 400
self.initialize_ui()
def initialize_ui(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
m = InputCanvas(self, width=3, height=3)
m.move(200, 20)
self.show()
class InputCanvas(FigureCanvas):
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
self.canvas.setFocusPolicy(QtCore.Qt.ClickFocus)
self.canvas.setFocus()
fig.canvas.mpl_connect('key_press_event', self.process_key)
self.input_plot()
def input_plot(self):
volume = io.imread("14.mha", plugin='simpleitk')
ax = self.figure.add_subplot(111)
ax.volume = volume
ax.index = volume.shape[0] // 2
ax.set_title('Ground Truth Image')
plt.set_cmap("gray")
plt.axis('off')
ax.imshow(volume[ax.index])
self.draw()
def process_key(self, event):
fig = event.canvas.figure
ax = fig.axes[0]
if event.key == 'j':
self.previous_slice(ax)
elif event.key == 'k':
self.ext_slice(ax)
self.draw()
def previous_slice(self, ax):
volume = ax.volume
ax.index = (ax.index - 1) % volume.shape[0] # wrap around using %
ax.images[0].set_array(volume[ax.index])
def next_slice(self, ax):
volume = ax.volume
ax.index = (ax.index + 1) % volume.shape[0]
ax.images[0].set_array(volume[ax.index])
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
您忘记告诉 canvas 它应该在按下某个键时执行某些操作。你可以使用 matplotlib 来做到这一点,
self.cid = self.mpl_connect("key_press_event", self.process_key)
要消除错误,请考虑错误消息。 "canvas" has no attribute canvas 告诉你 canvas 没有自己作为属性。因此
self.setFocusPolicy(QtCore.Qt.ClickFocus)
self.setFocus()
Matplotlib 嵌入 PyQt5 时不响应按键。我已经验证按键可以与 Tkinter 一起使用。这是我第一次体验 PyQt5。我不确定我在这里做错了什么。我试图通过这样做将重点放在 canvas 上。所有这些都是为了显示 3D 体积数据,您可以将其视为一堆图像。它们一起描述了一个 3D 结构。例如,磁共振成像 (MRI)
self.canvas.setFocusPolicy( QtCore.Qt.ClickFocus )
self.canvas.setFocus()
然而 returns 一个错误:
self.canvas.setFocusPolicy(QtCore.Qt.ClickFocus)
AttributeError: 'InputCanvas' object has no attribute 'canvas'
这是我的代码:
import sys
from PyQt5 import QtCore
from skimage import io
from PyQt5.QtWidgets import QMainWindow, QSizePolicy
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from PyQt5.QtWidgets import QApplication
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as
FigureCanvas
class App(QMainWindow):
def __init__(self):
super().__init__()
self.left = 10
self.top = 10
self.title = 'PYQT'
self.width = 600
self.height = 400
self.initialize_ui()
def initialize_ui(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
m = InputCanvas(self, width=3, height=3)
m.move(200, 20)
self.show()
class InputCanvas(FigureCanvas):
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
self.canvas.setFocusPolicy(QtCore.Qt.ClickFocus)
self.canvas.setFocus()
fig.canvas.mpl_connect('key_press_event', self.process_key)
self.input_plot()
def input_plot(self):
volume = io.imread("14.mha", plugin='simpleitk')
ax = self.figure.add_subplot(111)
ax.volume = volume
ax.index = volume.shape[0] // 2
ax.set_title('Ground Truth Image')
plt.set_cmap("gray")
plt.axis('off')
ax.imshow(volume[ax.index])
self.draw()
def process_key(self, event):
fig = event.canvas.figure
ax = fig.axes[0]
if event.key == 'j':
self.previous_slice(ax)
elif event.key == 'k':
self.ext_slice(ax)
self.draw()
def previous_slice(self, ax):
volume = ax.volume
ax.index = (ax.index - 1) % volume.shape[0] # wrap around using %
ax.images[0].set_array(volume[ax.index])
def next_slice(self, ax):
volume = ax.volume
ax.index = (ax.index + 1) % volume.shape[0]
ax.images[0].set_array(volume[ax.index])
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
您忘记告诉 canvas 它应该在按下某个键时执行某些操作。你可以使用 matplotlib 来做到这一点,
self.cid = self.mpl_connect("key_press_event", self.process_key)
要消除错误,请考虑错误消息。 "canvas" has no attribute canvas 告诉你 canvas 没有自己作为属性。因此
self.setFocusPolicy(QtCore.Qt.ClickFocus)
self.setFocus()