从医学图像的正视图中获取带有事件的zdata

时间:2018-05-28 17:42:57

标签: python-3.x numpy matplotlib tkinter nibabel

当我加载.nii文件时,我想从导航工具栏中获取zdata。我希望通过事件def _onclick(event):

来做到这一点
import matplotlib
matplotlib.use('TkAgg')
from tkinter import filedialog
from tkinter import *
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from nibabel.loadsave import read_img_data
from nibabel.loadsave import load as load_nii
from viewers import OrthoSlicer3D
from matplotlib.figure import Figure
import numpy as np

import sys
if sys.version_info[0] < 3:
    import Tkinter as Tk
else:
    import tkinter as Tk

root = Tk.Tk()
root.wm_title("Orthoslicer3D for brain segmentation")

f = Figure()

sagital = f.add_subplot(221)
sagital.set_position([0,0,0.5,0.5])
sagital.set_axis_off()

coronal = f.add_subplot(222)
coronal.set_axis_off()
coronal.set_position([0,0.5,0.5,0.5])

axial = f.add_subplot(223)
axial.set_position([0.5,0.5,0.5,0.5])
axial.set_axis_off()

axes = (sagital, coronal, axial)


# a tk.DrawingArea
canvas = FigureCanvasTkAgg(f, master=root)
canvas.get_tk_widget().pack(side=Tk.RIGHT, fill=Tk.BOTH, expand=1)

toolbar = NavigationToolbar2TkAgg(canvas, root)
toolbar.update()
canvas._tkcanvas.pack(side=Tk.RIGHT, fill=Tk.BOTH, expand=0)

def _load():
    data = filedialog.askopenfilename(initialdir = "/", title = "Select file", filetypes = (("nii files","*.nii"),("gz files", "*.gz"),("all files","*.*")))
    data_load = load_nii(data)
    data_read = read_img_data(data_load)
    data_read = np.asanyarray(data_read)
    OrthoSlicer3D(data_read, axes=axes).show()


coords = []


def _onclick(event):
    coords.append((event.xdata, event.ydata, event.zdata))
    return coords

canvas.mpl_connect('button_press_event', _onclick)

buttonLoad = Tk.Button(master=root, text='Load', command=_load)
buttonLoad.pack(side=Tk.LEFT, expand = 1)

Tk.mainloop()

但追溯是:

Traceback (most recent call last): File "C:\Users\migue\Documents\IM\TFG\venv\lib\site-packages\matplotlib\cbook\__init__.py", line 388, in process proxy(*args, **kwargs) File "C:\Users\migue\Documents\IM\TFG\venv\lib\site-packages\matplotlib\cbook\__init__.py", line 228, in __call__ return mtd(*args, **kwargs) File "C:/Users/migue/Documents/IM/TFG/pruebsd.py", line 58, in _onclick coords.append((event.xdata, event.ydata, event.zdata)) AttributeError: 'MouseEvent' object has no attribute 'zdata'

0 个答案:

没有答案