I am seeking to migrate workflow from MATLAB to Python. I will be doing a lot of filtering of large images and immediately hit a performance roadblock. Filtering a 11587 by 13744 in MATLAB R2022a with a 10 sigma Gaussian filter takes under two seconds:
tic, imgf=imgaussfilt(im,10); toc
Elapsed time is 1.792801 seconds.
I try the same thing with scipy 1.8.0 and skimage 0.19.1 and both are far slower:
%timeit scipy.ndimage.gaussian_filter(im, 10, truncate=2)
4.89 s ± 15.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Slower by 2.7 times.
%timeit skimage.filters.gaussian(im, sigma=10, preserve_range=True, truncate=2)
5.99 s ± 14.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Slower by 3.4 times.
Note that truncate is set to 2, which I understand matches what MATLAB is doing. I have verified that the output images look the same, so nothing basic is wrong.
Is there a solution for specifically speeding up this operation (and similar image processing tasks) in Python? Are some libraries generally considered faster than others? Already, above, I see that scipy is quicker, for instance.
EDIT:
- opencv is faster than the above two but still slower than MATLAB by a factor of 1.4 or so. This is getting into useful territory, however. opencv is multi-threaded, which seems to explain the difference.
- dip (see below) is faster than MATLAB.
dip.Gauss(img,10,truncation=2)executes in just over a second. - Even better, I have found that my images do not have to be 16 bit and opencv is capable of filtering my 8 bit images in 600 ms! MATLAB, weirdly, takes 13 seconds to filter such an image. So I think we have a winner here.