Hi,

Below you can find a small cython function that implements a simple max-filter. That is, given an image (a 2d numpy ndarray), each pixel is replaced by the maximum value in a neighborhood [-dx:dx, -dy:dy] around that pixel. For simplicity it just ignores the edges of the image.

I implemented the same function using C++ & ctypes, and the latter function turns out to be about 100 times faster than the cython one, for a 500x500 image, and dx,dy = 5,5. This surprises me, because of the simplicity of the function. I checked the C file, and also the translation of the maxfilter() function in C is quite simple. The bulk of the time is of course spent in the loops, and the only bottleneck that I can see is accessing the numpy arrays. Is there anything I can do in Cython to speed that up?

Cheers,
Joris





import numpy as np
cimport cython
cimport numpy as np

DTYPE = np.int                    # our image type
ctypedef np.int_t DTYPE_t         # our compile time image type

@cython.boundscheck(False)
def maxfilter(np.ndarray[DTYPE_t, ndim=2] image, int dx, int dy):

    cdef int nRow = image.shape[0]
    cdef int nCol = image.shape[1]
cdef np.ndarray[DTYPE_t, ndim=2] result = np.zeros((nRow,nCol), dtype=DTYPE)
    cdef DTYPE_t maxvalue

    cdef int i,j,k,n
    for i from dx <= i < nRow-dx:
        for j from dy <= j < nCol-dy:
            maxvalue = image[i,j]
            for k from -dx <= k <= dx:
                for n from -dy <= n <= dy:
if maxvalue < image[i+k,j+n]: maxvalue = image[i +k,j+n]
            result[i,j] = maxvalue
    return result

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to