Learn about numpy

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Learn about numpy

Folkert Boonstra
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[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
        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
Reply | Threaded
Open this post in threaded view
|

Re: Learn about numpy

Nadav Horesh
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[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
        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
Reply | Threaded
Open this post in threaded view
|

Re: Learn about numpy

Folkert Boonstra
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
Reply | Threaded
Open this post in threaded view
|

Re: Learn about numpy

Folkert Boonstra
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
Reply | Threaded
Open this post in threaded view
|

Re: Learn about numpy

Nadav Horesh


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
Reply | Threaded
Open this post in threaded view
|

Re: Learn about numpy

Folkert Boonstra
Nadav Horesh schreef:
> I think you have a problem of overflow in r5: You may better use utin64 instead of uint32.
>
>   Nadav.
>
>  
Nadav,

My problems were due to trying to do two things at once.
The code below does what I want and it is very fast. I see the power of
numpy now:

import numpy as NU
import numpy.numarray.nd_image as NI

BW = NU.zeros((5,5), dtype=NU.uint8)
AR = NU.zeros((5,5), dtype=NU.uint32)
DA = NU.zeros((5,5), dtype=NU.uint32)
LA = NU.zeros((5,5), dtype=NU.uint32)

DC = 4278190280
LC = 4278241280

BW[1,1] = 1
BW[2,2] = 1
AR[:] = DC
AR[1,1] = LC
AR[2,2] = LC
DA[:] = DC
LA[:] = LC

kernel = NU.array([[0,1,0], \
                   [1,1,1], \
                   [0,1,0]]).astype(NU.uint8)

print "AR=\n", AR

#convolve
r1 = NI.convolve(BW, kernel).astype(NU.uint8)
r2 = (NI.convolve(BW, kernel) == 1).astype(NU.uint8)
print "r1=\n",r1
print "r2=\n",r2

# create 32-bit array using results from convolve as mask
AR = NU.array(DA, copy=1)
NU.putmask(AR, r2, LA)

print "AR=\n", AR
------------------------------------------------------------------------------
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion