Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9224357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:17:58+00:00 2026-06-18T04:17:58+00:00

I have a short matplotlib module where I do some basic image analysis, delete

  • 0

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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:17:59+00:00Added an answer on June 18, 2026 at 4:17 am

    Running the following code with Python 2.7.3 works perfectly fine

    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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have many short-lived branches in my workflow and I would like them to
I am using matplotlib inside of a wxpython GUI. In short, I have plotted
I have been using mplot3d (part of matplotlib) for some various 3d plotting, and
I have unsigned short int (from 0 to 65535) and I must save it
I am coding a messaging system and I don't want to have short IDs
I have a short access/mySQL question. I have a mapping table on the format
i have a short script where i'm trying to grab data from a URL
I have a short question i have wrote this in java. Old code: class
I have a short question. Im my current project I'm using LINQ-to-SQl. That is
I have a short version of the question: I start a thread like that:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.