<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi,<div><br></div><div>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. </div><div><br></div><div>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?</div><div><br></div><div>Cheers,</div><div>Joris</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><div><font class="Apple-style-span" face="Courier">import numpy as np</font></div><div><font class="Apple-style-span" face="Courier">cimport cython</font></div><div><font class="Apple-style-span" face="Courier">cimport numpy as np</font></div><div><font class="Apple-style-span" face="Courier"><br></font></div><div><font class="Apple-style-span" face="Courier">DTYPE = np.int # our image type</font></div><div><font class="Apple-style-span" face="Courier">ctypedef np.int_t DTYPE_t # our compile time image type</font></div><div><font class="Apple-style-span" face="Courier"><br></font></div><div><font class="Apple-style-span" face="Courier">@cython.boundscheck(False)</font></div><div><font class="Apple-style-span" face="Courier">def maxfilter(np.ndarray[DTYPE_t, ndim=2] image, int dx, int dy):</font></div><div><font class="Apple-style-span" face="Courier"><br></font></div><div><font class="Apple-style-span" face="Courier"> cdef int nRow = image.shape[0]</font></div><div><font class="Apple-style-span" face="Courier"> cdef int nCol = image.shape[1]</font></div><div><font class="Apple-style-span" face="Courier"> cdef np.ndarray[DTYPE_t, ndim=2] result = np.zeros((nRow,nCol), dtype=DTYPE)</font></div><div><font class="Apple-style-span" face="Courier"> cdef DTYPE_t maxvalue</font></div><div><font class="Apple-style-span" face="Courier"> </font></div><div><font class="Apple-style-span" face="Courier"> cdef int i,j,k,n</font></div><div><font class="Apple-style-span" face="Courier"> for i from dx <= i < nRow-dx:</font></div><div><font class="Apple-style-span" face="Courier"> for j from dy <= j < nCol-dy:</font></div><div><font class="Apple-style-span" face="Courier"> maxvalue = image[i,j]</font></div><div><font class="Apple-style-span" face="Courier"> for k from -dx <= k <= dx:</font></div><div><font class="Apple-style-span" face="Courier"> for n from -dy <= n <= dy:</font></div><div><font class="Apple-style-span" face="Courier"> if maxvalue < image[i+k,j+n]: maxvalue = image[i+k,j+n]</font></div><div><font class="Apple-style-span" face="Courier"> result[i,j] = maxvalue</font></div><div><font class="Apple-style-span" face="Courier"> return result</font></div></div><BR>
<BR>
<FONT FACE="Arial" SIZE=2>Disclaimer: <A HREF<BR>
<BR>
</body></html>