Fastest 2D convolution or image filter in Python

前端 未结 5 1587
遥遥无期
遥遥无期 2020-12-24 03:35

Several users have asked about the speed or memory consumption of image convolutions in numpy or scipy [1, 2, 3, 4]. From the responses and my experience using Numpy, I bel

5条回答
  •  孤独总比滥情好
    2020-12-24 04:14

    Scipy has a function fftconvolve, that can be used for 1D and 2D signals.

    from scipy import signal
    from scipy import misc
    import numpy as np
    import matplotlib.pyplot as plt
    
    face = misc.face(gray=True)
    kernel = np.outer(signal.gaussian(70, 8), signal.gaussian(70, 8))
    blurred = signal.fftconvolve(face, kernel, mode='same')
    
    fig, (ax_orig, ax_kernel, ax_blurred) = plt.subplots(3, 1, figsize=(6, 15))
    ax_orig.imshow(face, cmap='gray')
    ax_orig.set_title('Original')
    ax_orig.set_axis_off()
    ax_kernel.imshow(kernel, cmap='gray')
    ax_kernel.set_title('Gaussian kernel')
    ax_kernel.set_axis_off()
    ax_blurred.imshow(blurred, cmap='gray')
    ax_blurred.set_title('Blurred')
    ax_blurred.set_axis_off()
    fig.show()
    

提交回复
热议问题