"moving window" in numpy

Hi all, I'm not sure if my terminology is familiar but I'm trying to do a "moving window" analysis (ie a spatial filter or kernel) on a 2-D array representing elevation. For example, a 3x3 window centered on each cell is used to calculate the derivate slope of that cell. Can this easily be implemented using numpy? Currently I have tried implementing in pure python loops (too slow) and c++ (fast but more difficult to compile, distribute, wrap in python calls, etc). I think a good solution would be to leverage numpy which is both fast and and easy package for end users to install. An example of the C++ code I'm trying to emulate is at http://perrygeo.net/download/hillshade.html . Does anyone have any tips or examples out there? Where should I start researching this? -- Matthew T. Perry http://www.perrygeo.net

Hi Matthew, I've been working on "rolling/windowed" libraries for quite a while. I'm currently working on implementations for basic moment and rank statistics - they are pretty much done and I am trying to maneuver them into apache commons math for java. I am also interested in implementing these statistics for python/numpy and R. I have seen a little bit for R, but nothing yet for numpy. /brad On Nov 25, 2007 5:00 PM, Matthew Perry <perrygeo@gmail.com> wrote:
Hi all,
I'm not sure if my terminology is familiar but I'm trying to do a "moving window" analysis (ie a spatial filter or kernel) on a 2-D array representing elevation. For example, a 3x3 window centered on each cell is used to calculate the derivate slope of that cell.
Can this easily be implemented using numpy?
Currently I have tried implementing in pure python loops (too slow) and c++ (fast but more difficult to compile, distribute, wrap in python calls, etc). I think a good solution would be to leverage numpy which is both fast and and easy package for end users to install.
An example of the C++ code I'm trying to emulate is at http://perrygeo.net/download/hillshade.html . Does anyone have any tips or examples out there? Where should I start researching this?
-- Matthew T. Perry http://www.perrygeo.net _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion

On Montag 26 November 2007, Matthew Perry wrote:
Hi all,
I'm not sure if my terminology is familiar but I'm trying to do a "moving window" analysis (ie a spatial filter or kernel) on a 2-D array representing elevation. For example, a 3x3 window centered on each cell is used to calculate the derivate slope of that cell.
Did you have a look at ndimage? http://www.scipy.org/SciPyPackages/Ndimage
Currently I have tried implementing in pure python loops (too slow)
You could probably have a faster, pure python version with some indexing tricks. E.g. if your gradient filter is static (i.e. the weights do not adapt to the local image content), you can apply it to whole images that contain shifted image contents. This way, you're using the fast C loops of NumPy. -- Ciao, / / .o. /--/ ..o / / ANS ooo

Matthew Perry wrote:
Hi all,
I'm not sure if my terminology is familiar but I'm trying to do a "moving window" analysis (ie a spatial filter or kernel) on a 2-D array representing elevation. For example, a 3x3 window centered on each cell is used to calculate the derivate slope of that cell.
Can this easily be implemented using numpy?
If I understand you correctly, scipy.ndimage does exactly what you ask for. See http://www.scipy.org/SciPyPackages/Ndimage. Cheers, VS.

Hi Matthew On Sun, Nov 25, 2007 at 05:00:48PM -0800, Matthew Perry wrote:
I'm not sure if my terminology is familiar but I'm trying to do a "moving window" analysis (ie a spatial filter or kernel) on a 2-D array representing elevation. For example, a 3x3 window centered on each cell is used to calculate the derivate slope of that cell.
Take a look at Anne Archibald's post: http://projects.scipy.org/pipermail/numpy-discussion/2006-November/024760.ht... and its attachment here: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20061120/d0... Regards Stéfan
participants (5)
-
Bradford Cross
-
Hans Meine
-
Matthew Perry
-
Stefan van der Walt
-
Vincent Schut