Created
September 30, 2013 22:28
-
-
Save astrojuanlu/6771240 to your computer and use it in GitHub Desktop.
Numba Filter 2D test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy | |
| def filter2d(image, filt): | |
| M, N = image.shape | |
| Mf, Nf = filt.shape | |
| Mf2 = Mf // 2 | |
| Nf2 = Nf // 2 | |
| result = numpy.zeros_like(image) | |
| for i in range(Mf2, M - Mf2): | |
| for j in range(Nf2, N - Nf2): | |
| num = 0.0 | |
| for ii in range(Mf): | |
| for jj in range(Nf): | |
| num += (filt[Mf-1-ii, Nf-1-jj] * image[i-Mf2+ii, | |
| j-Nf2+jj]) | |
| result[i, j] = num | |
| return result | |
| # This kind of quadruply-nested for-loop is going to be quite slow. | |
| # Using Numba we can compile this code to LLVM which then gets | |
| # compiled to machine code: | |
| from numba import double, jit | |
| fastfilter_2d = jit(double[:,:](double[:,:], double[:,:]))(filter2d) | |
| # Now fastfilter_2d runs at speeds as if you had first translated | |
| # it to C, compiled the code and wrapped it with Python | |
| image = numpy.random.random((100, 100)) | |
| filt = numpy.random.random((10, 10)) | |
| res = fastfilter_2d(image, filt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment