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

前端 未结 5 707
走了就别回头了
走了就别回头了 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:05

    You can use a Gaussian filter as it gives much sharpness than a pure HPF, for using a simple HPF you can use the following code

    import numpy as np
    import cv2
    from scipy import ndimage
    
    class HPF(object):
        def __init__(self, kernel, image):
            self.kernel = np.array(kernel)
            self.image = image
    
        def process(self):
            return ndimage.convolve(self.image, self.kernel)
    
    
    if __name__ == "__main__":
        #enter ur image location
        image = cv2.imread("images/test2.jpg", 0)
        kernel3x3 = [[-1,-1,-1],[-1,8,-1],[-1,-1,-1]]
        kernel5x5 = [[-1, -1, -1, -1, -1],
        [-1, 1, 2, 1, -1],
        [-1, 2, 4, 2, -1],
        [-1, 1, 2, 1, -1],
        [-1, -1, -1, -1, -1]]
    
        hpf1 = HPF(kernel3x3, image)
        hpfimage1 = hpf1.process()
        hpf2 = HPF(kernel5x5, image)
        hpfimage2 = hpf2.process()
        cv2.imshow("3x3",hpfimage1)
        cv2.imshow("5x5",hpfimage2)
        cv2.waitKey()
        cv2.destroyAllWindows()
    

    To use the Gaussian filter just add the Gaussian blur to your image

    blurred = cv2.GaussianBlur(image, (11, 11), 0)
    

    Then minus it from the original image

    g_hpf = image - blurred
    

    Original code taken from : Image Sharpening by High Pass Filter using Python and OpenCV

提交回复
热议问题