0

我有一个简短的 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()
4

1 回答 1

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()
于 2013-02-01T16:12:41.183 回答