重新初始化python matplotlib模块 - 重置按钮

时间:2013-02-01 15:26:08

标签: python

我有一个简短的matplotlib模块,在那里我做一些基本的图像分析,删除切片,注册,求和和保存。但是,我想添加一个重置按钮来重新初始化模块,将图像数据恢复到初始状态。

基本上,如果我有一个构成动态数据集的2D图像列表。我希望能够删除帧,注册图像,最后创建一个求和的图像。为此,我操作列表raw_dicom_stack。但是,如果出现问题,我希望重置另一个按钮,重置raw_dicom_stack并使用原始数据重新启动该类。我在早期将raw_dicom_stack的内容复制到reset_stack并重置我尝试:

我试过了:

def reset(self, event):
    self.__init__(self.reset_stack, self.nframes, self.ds)

但这会导致模块崩溃,我没有收到任何错误消息。我哪里错了?

def main():
    root = Tk()
    root.withdraw()
    #Default DMSA image directory
    IMAGE_DIR = '/home/nm/Python/DMSA/dmsa_examples/dynamics/'
    filename = tkFileDialog.askopenfilename(parent=root,initialdir=IMAGE_DIR, title='Select dicom image')
    if filename == '':
        quit()

    ds = dicom.read_file(filename)
    pix = ds.pixel_array
    #find out size of pixel array
    #print 'dicom image has x,y,z dimensions %d,%d,%d' %(int(pix.shape[0]), int(pix.shape[1]), int(pix.shape[2]))
    raw_dicom_stack = []

    for x in range(pix.shape[0]/2):
        raw_dicom_stack.append(pix[x,:,:])


    nframes = pix.shape[0]/2;
    # Visualize it
    viewer = VolumeViewer(raw_dicom_stack, nframes, ds)
    viewer.show()

class VolumeViewer(object):
    def __init__(self, raw_dicom_stack, nframes, ds):

        self.raw_dicom_stack = raw_dicom_stack
        self.nframes = nframes
        self.summed_image = np.zeros((self.raw_dicom_stack[0].shape[0],self.raw_dicom_stack[0].shape[1]))
        self.frame = 0
        self.frames_delete = 0
        self.ds = ds
        self.reset_stack = raw_dicom_stack


        # Plot the first slice of the image
        self.fig, self.ax = plt.subplots()
        self.im = self.ax.imshow(np.array(raw_dicom_stack[0]), cmap = cm.gray)


    def update(self, value):
        self.frame = int(self.nframes * value)
        # Update the image data
        dat = np.array(self.raw_dicom_stack[self.frame])
        self.im.set_data(dat)
        self.im.set_clim([dat.min(), dat.max()])
        # Redraw the plot
        self.fig.canvas.draw()      

    def add(self,event):
        if self.query_add == True:
            print "Image series has already summed"
        else:
            for x in range(self.nframes):
                self.summed_image += self.raw_dicom_stack[x]
            del self.raw_dicom_stack[:]
            self.raw_dicom_stack.append(self.summed_image[:,:])
            self.nframes = len(self.raw_dicom_stack)

            # Update the image data
            dat = np.array(self.raw_dicom_stack[0])
            self.im.set_data(dat)
            self.slider.reset()
            self.im.set_clim([dat.min(), dat.max()])
            # Redraw the plot
            self.fig.canvas.draw()

            self.query_add = True
            #rezero summed_image
            #summed_image = np.zeros((self.summed_image.shape[0],self.summed_image.shape[1]))


    def show(self):
        plt.show()

    def reset(self, event):

        #self.summed_image = np.zeros((self.raw_dicom_stack[0].shape[0],self.raw_dicom_stack[0].shape[1]))
        #self.query_register = False
        #self.query_add = False
        self.__init__(self.reset_stack, self.nframes, self.ds)

    def quit(self,event):
        quit()

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:1)

使用Python 2.7.3运行以下代码非常正常

import numpy as np

class VolumeViewer(object):
    def __init__(self, raw_dicom_stack, nframes, ds):

        self.raw_dicom_stack = raw_dicom_stack
        self.nframes = nframes
        self.summed_image = np.zeros((self.raw_dicom_stack[0].shape[0],self.raw_dicom_stack[0].shape[1]))
        self.frame = 0
        self.frames_delete = 0
        self.ds = ds
        self.reset_stack = raw_dicom_stack


        # Plot the first slice of the image
        self.fig, self.ax = plt.subplots()
        self.im = self.ax.imshow(np.array(raw_dicom_stack[0]), cmap = cm.gray)


    def update(self, value):
        self.frame = int(self.nframes * value)
        # Update the image data
        dat = np.array(self.raw_dicom_stack[self.frame])
        self.im.set_data(dat)
        self.im.set_clim([dat.min(), dat.max()])
        # Redraw the plot
        self.fig.canvas.draw()      

    def add(self,event):
        if self.query_add == True:
            print "Image series has already summed"
        else:
            for x in range(self.nframes):
                self.summed_image += self.raw_dicom_stack[x]
            del self.raw_dicom_stack[:]
            self.raw_dicom_stack.append(self.summed_image[:,:])
            self.nframes = len(self.raw_dicom_stack)

            # Update the image data
            dat = np.array(self.raw_dicom_stack[0])
            self.im.set_data(dat)
            self.slider.reset()
            self.im.set_clim([dat.min(), dat.max()])
            # Redraw the plot
            self.fig.canvas.draw()

            self.query_add = True
            #rezero summed_image
            #summed_image = np.zeros((self.summed_image.shape[0],self.summed_image.shape[1]))


    def show(self):
        plt.show()

    def reset(self, event):

        #self.summed_image = np.zeros((self.raw_dicom_stack[0].shape[0],self.raw_dicom_stack[0].shape[1]))
        #self.query_register = False
        #self.query_add = False
        self.__init__(self.reset_stack, self.nframes, self.ds)

    def quit(self,event):
        quit()

if __name__ == '__main__':
    viewer = VolumeViewer(np.zeros((2,10,10)), 4, None)
    viewer.reset()