High Pass Filter for image processing in python by using scipy/numpy

前端 未结 5 800
走了就别回头了
走了就别回头了 2021-01-30 04:34

I am currently studying image processing. In Scipy, I know there is one median filter in Scipy.signal. Can anyone tell me if there is one filter similar to high pass filter?

5条回答
  •  清歌不尽
    2021-01-30 05:21

    Here is how we can design a HPF with scipy fftpack

    from skimage.io import imread
    import matplotlib.pyplot as plt
    import scipy.fftpack as fp
    
    im = np.mean(imread('../images/lena.jpg'), axis=2) # assuming an RGB image
    plt.figure(figsize=(10,10))
    plt.imshow(im, cmap=plt.cm.gray)
    plt.axis('off')
    plt.show()
    

    Original Image

    F1 = fftpack.fft2((im).astype(float))
    F2 = fftpack.fftshift(F1)
    plt.figure(figsize=(10,10))
    plt.imshow( (20*np.log10( 0.1 + F2)).astype(int), cmap=plt.cm.gray)
    plt.show()
    

    Frequency Spectrum with FFT

    (w, h) = im.shape
    half_w, half_h = int(w/2), int(h/2)
    
    # high pass filter
    n = 25
    F2[half_w-n:half_w+n+1,half_h-n:half_h+n+1] = 0 # select all but the first 50x50 (low) frequencies
    plt.figure(figsize=(10,10))
    plt.imshow( (20*np.log10( 0.1 + F2)).astype(int))
    plt.show()
    

    Block low Frequencies in the Spectrum

    im1 = fp.ifft2(fftpack.ifftshift(F2)).real
    plt.figure(figsize=(10,10))
    plt.imshow(im1, cmap='gray')
    plt.axis('off')
    plt.show()
    

    Output Image after applying the HPF

提交回复
热议问题