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 8848439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:25:25+00:00 2026-06-14T12:25:25+00:00

I am a new Python user and would like to do some simple image

  • 0

I am a new Python user and would like to do some simple image processing. Essentially I will have a dynamic medical image – a series of 2D images at different time points which I would like to store as a 3D array. Due to the nature of the scanning technique there is likely to be occasional patient motion during certain imaging frames which makes the data unusable. I would like to delete such frames and recast the array – new dimensions (n-1, 256, 256). After deletion of the frame I would like to update the image display. What is the best way to achieve this goal? Here is the skeleton code I have so far:

import dicom
import numpy as np
import pylab 
from matplotlib.widgets import Slider, Button

ds = dicom.read_file("/home/moadeep/Dropbox/FS1.dcm")
#data = ds.pixel_array
data = np.random.rand(16,256,256)
nframes = data.shape[0]

ax = pylab.subplot(111)
pylab.subplots_adjust(left=0.25, bottom=0.25)

frame = 0
l = pylab.imshow(data[frame,:,:]) #shows 1024x256 imagge, i.e. 0th frame*

axcolor = 'lightgoldenrodyellow'
axframe = pylab.axes([0.35, 0.1, 0.5, 0.03], axisbg=axcolor)

#add slider to scroll image frames
sframe = Slider(axframe, 'Frame', 0, nframes, valinit=0,valfmt='%1d'+'/'+str(nframes))

ax_delete = pylab.axes([0.8,0.025,0.1,0.04], axisbg=axcolor)

#add slider to scroll image frames

#Delete button to delete frame from data set
bDelete = Button(ax_delete, 'Delete')

def update(val):
    frame = np.around(sframe.val)
    pylab.subplot(111)
    pylab.subplots_adjust(left=0.25, bottom=0.25)
    pylab.imshow(data[frame,:,:])

sframe.on_changed(update)

pylab.gray()
pylab.show()
  • 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-14T12:25:26+00:00Added an answer on June 14, 2026 at 12:25 pm

    The short answer to your question is use numpy.delete. E.g.

    import numpy as np
    data = np.arange(1000).reshape((10,10,10))
    
    # Delete the third slice along the first axis 
    # (note that you can delete multiple slices at once)
    data = np.delete(data, [2], axis=0)
    
    print data.shape
    

    However, this is a poor approach if you’re going to be removing individual slices many times.

    The longer answer is to avoid doing this each time you want to delete a slice.

    Numpy arrays have to be contiguous in memory. Therefore, this will make a new copy (and delete the old) each time. This will be relatively slow, and requires you to have twice the free memory space required to store the array.

    In your case, why not store a python list of 2D arrays? That way you can pop the slices you don’t want out without any problems. If you need it as a 3D array afterwards, just use numpy.dstack to create it.

    Of course, if you need to do 3D processing, you’ll need the 3D array. Therefore, another approach would be to store a list of “bad” indicies and remove them at the end using numpy.delete (note that the items to be deleted is a list, so you can just pass in your list of “bad” indicies).


    On a side note, the way you’re updating the image will be very slow.

    You’re creating lots of images, so each one will be redrawn each time and the update will become very slow as you go on.

    You’re better off setting the data of the image (im.set_data(next_slice)) instead of creating a new image each time.

    Better yet, use blitting, but with image data in matplotlib, it’s not as advantageous as it is for other types of plots due to matplotlib’s slow-ish rescaling of images.

    As a quick example:

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.widgets import Slider
    
    def main():
        # Set up 3D coordinates from -10 to 10 over a 200x100x100 "open" grid
        x, y, z = np.ogrid[-10:10:200j, -10:10:100j, -10:10:100j]
    
        # Generate a cube of interesting data
        data= np.sin(x*y*z) / (x*y*z)
    
        # Visualize it
        viewer = VolumeViewer(data)
        viewer.show()
    
    class VolumeViewer(object):
        def __init__(self, data):
            self.data = data
            self.nframes = self.data.shape[0]
    
            # Setup the axes.
            self.fig, self.ax = plt.subplots()
            self.slider_ax = self.fig.add_axes([0.2, 0.03, 0.65, 0.03])
    
            # Make the slider
            self.slider = Slider(self.slider_ax, 'Frame', 1, self.nframes, 
                                valinit=1, valfmt='%1d/{}'.format(self.nframes))
            self.slider.on_changed(self.update)
    
            # Plot the first slice of the image
            self.im = self.ax.imshow(data[0,:,:])
    
        def update(self, value):
            frame = int(np.round(value - 1))
    
            # Update the image data
            dat = self.data[frame,:,:]
            self.im.set_data(dat)
    
            # Reset the image scaling bounds (this may not be necessary for you)
            self.im.set_clim([dat.min(), dat.max()])
    
            # Redraw the plot
            self.fig.canvas.draw()
    
        def show(self):
            plt.show()
    
    if __name__ == '__main__':
        main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a new user of python and would like to try subnets-resolver .
New to Python, have a simple, situational question: Trying to use BeautifulSoup to parse
I'm new to programming, I have some basic python programming from college, I am
I am new to Django. I would like to run some command from some
I have a Python module that I would like to upload to PyPI. So
I am new to computer programming and have some experience programming with python. I
I'm a Matlab user needing to use Python for some things, I would really
I have a program written in python, and I would like to make it
I have some questions about user-defined exceptions in Python and how they should be
Hello I am a new user to Python and I am having problem doing

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.