Home > Mobile >  Why such a big speed difference between 2D gaussian filtering with skimage and scipy compared to MAT
Why such a big speed difference between 2D gaussian filtering with skimage and scipy compared to MAT

Time:05-20

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.

CodePudding user response:

MATLAB's imgaussfilt chose, by the dimensions of data, whether to use spatial domain processing or Fourier domain processing.

In some cases its choice, might create optimized calculation which explains the gains you see.

If you're after the fastest spatial filtering using Gaussian Filter you probably should use Intel IPP.

CodePudding user response:

In summary: The best solution I have found for my use case is to convert from 16 bit to 8 bit and use opencv. DIPlib would be faster if I didn't wish to move to 16 bit. The major reason for slower performance with some Python packages seems to be due to them not being multi-threaded, compared to MATLAB's functions which are.

  • Related