[Numpy-discussion] Learn about numpy

Nadav Horesh nadavh@visionsense....
Sun May 4 05:13:00 CDT 2008

What you do here is a convolution with 

0 1 0
1 1 1
0 1 0

kernel, and thresholding, you can use numpy.numarray.nd_image package:

import numpy.numarray.nd_image as NI
   ker = array([[0,1,0], [1,1,1],[0,1,0]])
   result = (NI.convolve(self.bufbw, ker) == 1).astype(uint8)

for nore general cases you can use the function generic_filter in the same package.


-----הודעה מקורית-----
מאת: numpy-discussion-bounces@scipy.org בשם Folkert Boonstra
נשלח: א 04-מאי-08 12:52
אל: numpy-discussion@scipy.org
נושא: [Numpy-discussion] Learn about numpy
With a python background but new to numpy, I have the following.

Suppose I have a 2-D array and I want to apply a function to each element.
The function needs to access the direct neighbouring elements in order
to set a new value for the element. How would I do that in the most
efficient way with numpy?

Currently I have a uint8 array (self.bufbw) filled with 0 and 1 elements:

    def applyRule(self, rule):
       for xy in self.xydims:

    def rule(self, xy):
        x = xy[0]; y = xy[1]
        sum = self.bufbw[x-1:x+2, y-1:y+2].sum() \
            - self.bufbw[x-1,y-1] - self.bufbw[x+1,y-1] \
            - self.bufbw[x-1,y+1] - self.bufbw[x+1,y+1]
        if sum == 1:
            self.bufbw[x,y] = 1
            self.bufbw[x,y] = 0

I have looked at the documentation online but couldn't find another faster solution yet. 
Does anyone want to share some ideas on a faster solution with numpy?


Numpy-discussion mailing list

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/ms-tnef
Size: 3493 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080504/c549c783/attachment.bin 

More information about the Numpy-discussion mailing list