 Classic List Threaded 6 messages Open this post in threaded view
|

 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:           rule(xy)     def rule(self, xy):         x = xy; y = xy         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         else:             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? Thanks, Folkert _______________________________________________ Numpy-discussion mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/numpy-discussion
Open this post in threaded view
|

 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.    Nadav. -----הודעה מקורית----- מאת: [hidden email] בשם Folkert Boonstra נשלח: א 04-מאי-08 12:52 אל: [hidden email] נושא: [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:           rule(xy)     def rule(self, xy):         x = xy; y = xy         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         else:             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? Thanks, Folkert _______________________________________________ Numpy-discussion mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/numpy-discussion_______________________________________________ Numpy-discussion mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/numpy-discussion winmail.dat (4K) Download Attachment
Open this post in threaded view
|

 Nadav Horesh schreef: > 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. > >    Nadav. > >   Thanks, that works ok! However if instead of uint8, uint32 values are used, the result only contains zeros. Or am I doing something wrong? Folkert import numpy import numpy.numarray.nd_image as NI B = numpy.zeros((5,5), dtype=numpy.uint8) C = numpy.zeros((5,5), dtype=numpy.uint32) DC = 4278190280 LC = 4278241280 B[:]   = 0 B[1,1] = 1 B[2,2] = 1 C[:]   = DC C[1,1] = LC C[2,2] = LC ker01 = numpy.array([[0,1,0], \                      [1,1,1], \                      [0,1,0]]) kerCC = numpy.array([[C[0,0],C[1,1],C[0,0]], \                      [C[1,1],C[1,1],C[1,1]], \                      [C[0,0],C[1,1],C[0,0]]]).astype(numpy.uint32) r1 = NI.convolve(B, ker01).astype(numpy.uint8) r2 = (NI.convolve(B, ker01) == 1).astype(numpy.uint8) r3 = NI.convolve(C, kerCC).astype(numpy.uint32) r4 = (NI.convolve(C, kerCC) == C[0,0]).astype(numpy.uint32) _______________________________________________ Numpy-discussion mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/numpy-discussion
Open this post in threaded view
|

 Folkert Boonstra schreef: > Nadav Horesh schreef: >   >> 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. >> >>    Nadav. >> >>   >>     > Thanks, that works ok! > > However if instead of uint8, uint32 values are used, the result only > contains zeros. > Or am I doing something wrong? > Folkert > > import numpy > import numpy.numarray.nd_image as NI > > B = numpy.zeros((5,5), dtype=numpy.uint8) > C = numpy.zeros((5,5), dtype=numpy.uint32) > > DC = 4278190280 > LC = 4278241280 > > B[:]   = 0 > B[1,1] = 1 > B[2,2] = 1 > C[:]   = DC > C[1,1] = LC > C[2,2] = LC > > ker01 = numpy.array([[0,1,0], \ >                      [1,1,1], \ >                      [0,1,0]]) > kerCC = numpy.array([[C[0,0],C[1,1],C[0,0]], \ >                      [C[1,1],C[1,1],C[1,1]], \ >                      [C[0,0],C[1,1],C[0,0]]]).astype(numpy.uint32) > > r1 = NI.convolve(B, ker01).astype(numpy.uint8) > r2 = (NI.convolve(B, ker01) == 1).astype(numpy.uint8) > r3 = NI.convolve(C, kerCC).astype(numpy.uint32) > r4 = (NI.convolve(C, kerCC) == C[0,0]).astype(numpy.uint32) >   It should be: r5 = NI.convolve(C, ker01).astype(numpy.uint32) which results in: [[4211082216 4211133216 4211082216 4211082216 4211082216]  [4211133216 4211133216 4211184216 4211082216 4211082216]  [4211082216 4211184216 4211133216 4211133216 4211082216]  [4211082216 4211082216 4211133216 4211082216 4211082216]  [4211082216 4211082216 4211082216 4211082216 4211082216]] Now I have to find out how convolve works in order to understand why these values are generated.  Are there some good examples / documentation as you know? I found a EECE253_07_Convolution.pdf with lecture notes on convolution for image processing. Thanks, Folkert _______________________________________________ Numpy-discussion mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/numpy-discussion
 I think you have a problem of overflow in r5: You may better use utin64 instead of uint32.   Nadav. -----הודעה מקורית----- מאת: [hidden email] בשם Folkert Boonstra נשלח: ב 05-מאי-08 19:17 אל: Discussion of Numerical Python נושא: Re: [Numpy-discussion] Learn about numpy   Folkert Boonstra schreef: > Nadav Horesh schreef: >   >> 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. >> >>    Nadav. >> >>   >>     > Thanks, that works ok! > > However if instead of uint8, uint32 values are used, the result only > contains zeros. > Or am I doing something wrong? > Folkert > > import numpy > import numpy.numarray.nd_image as NI > > B = numpy.zeros((5,5), dtype=numpy.uint8) > C = numpy.zeros((5,5), dtype=numpy.uint32) > > DC = 4278190280 > LC = 4278241280 > > B[:]   = 0 > B[1,1] = 1 > B[2,2] = 1 > C[:]   = DC > C[1,1] = LC > C[2,2] = LC > > ker01 = numpy.array([[0,1,0], \ >                      [1,1,1], \ >                      [0,1,0]]) > kerCC = numpy.array([[C[0,0],C[1,1],C[0,0]], \ >                      [C[1,1],C[1,1],C[1,1]], \ >                      [C[0,0],C[1,1],C[0,0]]]).astype(numpy.uint32) > > r1 = NI.convolve(B, ker01).astype(numpy.uint8) > r2 = (NI.convolve(B, ker01) == 1).astype(numpy.uint8) > r3 = NI.convolve(C, kerCC).astype(numpy.uint32) > r4 = (NI.convolve(C, kerCC) == C[0,0]).astype(numpy.uint32) >   It should be: r5 = NI.convolve(C, ker01).astype(numpy.uint32) which results in: [[4211082216 4211133216 4211082216 4211082216 4211082216]  [4211133216 4211133216 4211184216 4211082216 4211082216]  [4211082216 4211184216 4211133216 4211133216 4211082216]  [4211082216 4211082216 4211133216 4211082216 4211082216]  [4211082216 4211082216 4211082216 4211082216 4211082216]] Now I have to find out how convolve works in order to understand why these values are generated.  Are there some good examples / documentation as you know? I found a EECE253_07_Convolution.pdf with lecture notes on convolution for image processing. Thanks, Folkert _______________________________________________ Numpy-discussion mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/numpy-discussion_______________________________________________ Numpy-discussion mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/numpy-discussion winmail.dat (5K) Download Attachment