<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.&nbsp;</div><div><br></div><div>I implemented the same function using C++ &amp; 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 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# our image type</font></div><div><font class="Apple-style-span" face="Courier">ctypedef np.int_t DTYPE_t &nbsp; &nbsp; &nbsp; &nbsp; # 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">&nbsp;&nbsp; &nbsp;cdef int nRow = image.shape[0]</font></div><div><font class="Apple-style-span" face="Courier">&nbsp;&nbsp; &nbsp;cdef int nCol = image.shape[1]</font></div><div><font class="Apple-style-span" face="Courier">&nbsp;&nbsp; &nbsp;cdef np.ndarray[DTYPE_t, ndim=2] result = np.zeros((nRow,nCol), dtype=DTYPE)</font></div><div><font class="Apple-style-span" face="Courier">&nbsp;&nbsp; &nbsp;cdef DTYPE_t maxvalue</font></div><div><font class="Apple-style-span" face="Courier">&nbsp;&nbsp; &nbsp;</font></div><div><font class="Apple-style-span" face="Courier">&nbsp;&nbsp; &nbsp;cdef int i,j,k,n</font></div><div><font class="Apple-style-span" face="Courier">&nbsp;&nbsp; &nbsp;for i from dx &lt;= i &lt; nRow-dx:</font></div><div><font class="Apple-style-span" face="Courier">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;for j from dy &lt;= j &lt; nCol-dy:</font></div><div><font class="Apple-style-span" face="Courier">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;maxvalue = image[i,j]</font></div><div><font class="Apple-style-span" face="Courier">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for k from -dx &lt;= k &lt;= dx:</font></div><div><font class="Apple-style-span" face="Courier">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for n from -dy &lt;= n &lt;= dy:</font></div><div><font class="Apple-style-span" face="Courier">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if maxvalue &lt; image[i+k,j+n]: maxvalue = image[i+k,j+n]</font></div><div><font class="Apple-style-span" face="Courier">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result[i,j] = maxvalue</font></div><div><font class="Apple-style-span" face="Courier">&nbsp;&nbsp; &nbsp;return result</font></div></div><BR>
<BR>
<FONT FACE="Arial" SIZE=2>Disclaimer: <A HREF<BR>
<BR>
</body></html>