I have a short matplotlib module where I do some basic image analysis, delete slices, register, sum and save. However, I would like to add a reset button to re-initialise the module, putting the image data back to its initial state.
Basically, if I have a list of 2D images which make up a dynamic data set. I want to be able to delete frames, register the images and finally create a summed image. To do this I manipulate the list raw_dicom_stack. However, if something goes wrong I would like another button reset which resets raw_dicom_stack and restarts the class with the original data. I copy the contents of raw_dicom_stack to reset_stack early on and to reset I try:
I have tried:
def reset(self, event):
self.__init__(self.reset_stack, self.nframes, self.ds)
but this crashes the module and I get no error message. Where am I going wrong?
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()
reset