Best way to cycle through numpy images using scroll?

Best way to cycle through numpy images using scroll?
I have an array of images stored as an array of numpy arrays. I need to be able to efficiently scroll through that set of images. My first attempt at doing this goes something like this:

–init–

    self.ax = pyplot.imshow(imgdta[0], interpolation='spline36', cmap=cm.gray, picker=True)      # draw the plot @UndefinedVariable

    pyplot.axes().set_axis_off()

    self.fig = self.ax.get_figure()

    self.canvas = FigureCanvasGTKAgg(self.fig)   

–onscroll–

    self.ax.set_array(imdta[n]) # 0 < n < num_images

    self.canvas.draw()

This method of changing the image data does not seem to be very preferment. It takes ~.25 seconds to go from one image to the next. Can anybody suggest a faster way? This also ends up in a canvas that’s much larger than I need, is there a better way to define my view area?

Thank you,

Dave.

I’m also looking into a similar issue, and would be interested to see what approaches others have taken.

Has anyone found a good framework-independent solution?

Keith

···

On Wed, Aug 10, 2011 at 5:15 PM, David Just <Just.David@…3729…21…> wrote:

I have an array of images stored as an array of numpy arrays. I need to be able to efficiently scroll through that set of images. My first attempt at doing this goes something like this:

–init–

    [self.ax](http://self.ax) = pyplot.imshow(imgdta[0], interpolation='spline36', cmap=cm.gray, picker=True)      # draw the plot @UndefinedVariable

    pyplot.axes().set_axis_off()

    self.fig = self.ax.get_figure()

    self.canvas = FigureCanvasGTKAgg(self.fig)   

–onscroll–

    self.ax.set_array(imdta[n]) # 0 < n < num_images

    self.canvas.draw()

This method of changing the image data does not seem to be very preferment. It takes ~.25 seconds to go from one image to the next. Can anybody suggest a faster way? This also ends up in a canvas that’s much larger than I need, is there a better way to define my view area?

Thank you,

Dave.


uberSVN’s rich system and user administration capabilities and model

configuration take the hassle out of deploying and managing Subversion and

the tools developers use with it. Learn more about uberSVN and get a free

download at: http://p.sf.net/sfu/wandisco-dev2dev


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Here is how am solving this problem. It isn't terribly fast either, but it works for me. I wrote something with pygame that was faster, but it had its own set of problems.

Tom

···

---

import numpy as np
import pylab

class plotter:
     def __init__(self, im, i=0):
         self.im = im
         self.i = i
         self.vmin = im.min()
         self.vmax = im.max()
         self.fig = pylab.figure()
         pylab.gray()
         self.ax = self.fig.add_subplot(111)
         self.draw()
         self.fig.canvas.mpl_connect('key_press_event',self.key)

     def draw(self):
         if self.im.ndim is 2:
             im = self.im
         if self.im.ndim is 3:
             im = self.im[...,self.i]
             self.ax.set_title('image {0}'.format(self.i))

         pylab.show()

         self.ax.imshow(im, vmin=self.vmin, vmax=self.vmax, interpolation=None)

     def key(self, event):
         old_i = self.i
         if event.key=='right':
             self.i = min(self.im.shape[2]-1, self.i+1)
         elif event.key == 'left':
             self.i = max(0, self.i-1)
         if old_i != self.i or old_j != self.j:
             self.draw()
             self.fig.canvas.draw()

def show(im, i=0):
     plotter(im, i)

On 08/17/2011 01:26 PM, Keith Hughitt wrote:

I'm also looking into a similar issue, and would be interested to see
what approaches others have taken.

Has anyone found a good framework-independent solution?

Keith

On Wed, Aug 10, 2011 at 5:15 PM, David Just <Just.David@…3721… > <mailto:Just.David@…3721…>> wrote:

    I have an array of images stored as an array of numpy arrays. I
    need to be able to efficiently scroll through that set of images.
       My first attempt at doing this goes something like this:

    --init--

    self.ax <http://self.ax> = pyplot.imshow(imgdta[0],
    interpolation='spline36', cmap=cm.gray, picker=True) # draw the
    plot @UndefinedVariable
             pyplot.axes().set_axis_off()
             self.fig = self.ax.get_figure()
             self.canvas = FigureCanvasGTKAgg(self.fig)

    --onscroll--
             self.ax.set_array(imdta[n]) # 0 < n < num_images
             self.canvas.draw()

    This method of changing the image data does not seem to be very
    preferment. It takes ~.25 seconds to go from one image to the next.
       Can anybody suggest a faster way? This also ends up in a canvas
    that’s much larger than I need, is there a better way to define my
    view area?

    Thank you,
    Dave.

    ------------------------------------------------------------------------------
    uberSVN's rich system and user administration capabilities and model
    configuration take the hassle out of deploying and managing
    Subversion and
    the tools developers use with it. Learn more about uberSVN and get a
    free
    download at: http://p.sf.net/sfu/wandisco-dev2dev

    _______________________________________________
    Matplotlib-users mailing list
    Matplotlib-users@lists.sourceforge.net
    <mailto:Matplotlib-users@lists.sourceforge.net>
    https://lists.sourceforge.net/lists/listinfo/matplotlib-users

------------------------------------------------------------------------------
Get a FREE DOWNLOAD! and learn more about uberSVN rich system,
user administration capabilities and model configuration. Take
the hassle out of deploying and managing Subversion and the
tools developers use with it. http://p.sf.net/sfu/wandisco-d2d-2

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Two issues with your code that should significantly speed things up.

First, by calling imshow() each time for the draw, there is significant overhead caused by this. Instead – (and this is a huge speedup) – save the object returned by the first call to imshow(). That object has a method “.set_array()” that will allow you to just change the data contained within the AxesImage object. This is much faster than calling imshow() repeatedly. Note that the array going into set_array() will have to be of the same shape as the original image.

Second, by setting the “interpolation” kwarg to None, you are merely telling imshow() to use the default interpolation specified in your rcParams file. Instead, you probably want “nearest”. Actually, supposedly, the upcoming release is supposed to support a new value “none” for absolutely no interpolation at all. The idea would be that one would pre-interpolate the image data before sending it to imshow() and have imshow set to do no interpolations at all. Therefore, the images display much faster.

I hope this helps!
Ben Root

Excellent! That sped things up quite a bit. I can now flip through my small images with no perceivable delay. I will look forward to trying out the new interpolation setting when it gets here, since I have some larger images that still lag slightly.

If others want, I can repost my code with Ben's changes.

Thanks a bunch!
Tom

···

On 08/17/2011 03:30 PM, Benjamin Root wrote:

Two issues with your code that should significantly speed things up.

First, by calling imshow() each time for the draw, there is significant
overhead caused by this. Instead -- (and this is a huge speedup) --
save the object returned by the first call to imshow(). That object has
a method ".set_array()" that will allow you to just change the data
contained within the AxesImage object. This is *much* faster than
calling imshow() repeatedly. Note that the array going into set_array()
will have to be of the same shape as the original image.

Second, by setting the "interpolation" kwarg to *None*, you are merely
telling imshow() to use the default interpolation specified in your
rcParams file. Instead, you probably want "nearest". Actually,
supposedly, the upcoming release is supposed to support a new value
"none" for absolutely no interpolation at all. The idea would be that
one would pre-interpolate the image data before sending it to imshow()
and have imshow set to do no interpolations at all. Therefore, the
images display much faster.

I hope this helps!
Ben Root