viewing an image cube / sequence with imshow

I would like to view a 3D array (or a python list of 2D arrays) as a
sequence of images using something like imshow. For example, something
like this:

imshow(rand(6,12,12), imagecube=true)

then the display would show a 12x12 image, [0, :, :]. Then I could
scroll through the other frames to display any of the 6 frames in the
same window.

Syntax like this would also be nice:

imshow([a,b,c])

where [a,b,c] is a python list of 2D arrays.

Does matplotlib support anything like this?

Are there other packages that do this?

I looked at using numdisplay with ds9, but that only seemed to handle
2D images even though ds9 can handle image cubes on its own.

Thanks ...

Hi Jason,

I would like to view a 3D array (or a python list of 2D arrays) as a
sequence of images using something like imshow. For example, something
like this:

<snip>

Are there other packages that do this?

I have written an image browser module that does something similar to what
you're trying to do. It's still quite rudimentary, and I hadn't really
planned on sharing it in
its current state, but it may be that you can find some useful ideas
in the code. The image browser is a matplotlib canvas embedded in a wx
window, and I use the WxAgg backend - I've no idea how that will
change things from your setup. I run it interactively from ipython
-pylab as shown below.

Usage is pretty simple:

In [1]: import pyvis
In [2]: pv = pyvis.pyvis()
In [3]: pv.AddImg(arange(10000).reshape(100,100), 'my gradient')

then play around with the menus. More than one image can be loaded at
a time. Feel free to use the code as you like.

Angus.

pyvis.py (17.4 KB)

···

On 27/06/07, Jason Addison <jraddison@...287...> wrote:
--
AJC McMorland, PhD Student
Physiology, University of Auckland

Here is a simple example showing how to do this -- we could add
support for this in a built-in function. It would be nice if we
encapsulated the scroll mouse in our event handling, since the scroll
is the natural way to browse these images

from pylab import figure, show

class IndexTracker:
    def __init__(self, ax, X):
        self.ax = ax
        ax.set_title('use left mouse to advance, right to retreat')

        self.X = X
        rows,cols,self.slices = X.shape
        self.ind = self.slices/2

        self.im = ax.imshow(self.X[:,:,self.ind])
        self.update()

    def onpress(self, event):

        if event.button==1:
            self.ind = numpy.clip(self.ind+1, 0, self.slices-1)
        elif event.button==3:
            self.ind = numpy.clip(self.ind-1, 0, self.slices-1)

        print event.button, self.ind
        self.update()

    def update(self):
        self.im.set_data(self.X[:,:,self.ind])
        ax.set_ylabel('slice %s'%self.ind)
        self.im.axes.figure.canvas.draw()

fig = figure()
ax = fig.add_subplot(111)

X = numpy.random.rand(20,20,40)

tracker = IndexTracker(ax, X)

fig.canvas.mpl_connect('button_press_event', tracker.onpress)

show()

imshowseq.py (976 Bytes)

···

On 6/26/07, Jason Addison <jraddison@...287...> wrote:

I would like to view a 3D array (or a python list of 2D arrays) as a
sequence of images using something like imshow. For example, something
like this:

imshow(rand(6,12,12), imagecube=true)

I just added a scroll_event to backend bases -- it fires a mouse event
setting the button attribute to either 'up' or 'down' and added this
example to examples/image_slices_viewer.py

Developers who use/develop other backends -- if you get a minute it
would be nice to connect up your backend to the new scroll_event. See
backend_gtk.py for example code

Thanks,
JDH

···

On 6/27/07, John Hunter <jdh2358@...287...> wrote:

Here is a simple example showing how to do this -- we could add
support for this in a built-in function. It would be nice if we
encapsulated the scroll mouse in our event handling, since the scroll
is the natural way to browse these images

Thanks John and Angus. I can't wait to try out your recommendations.
I'm upgrading my system at the moment, so I'm not able to try them out
just now. It is promising that matplotlib seems to be able to handle
this pretty easily.